GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 13-Jul-2009, 02:59
jecco9 jecco9 is offline
Awaiting Email Confirmation
 
Join Date: Sep 2007
Posts: 12
jecco9 is on a distinguished road
Unhappy

C incorrectly calculating an equation


I'm writing a code to analyze some data. I pretty much finished writing it, but at least one of the calculations is being done incorrectly. It then cascades down to the rest. The line that is giving me trouble is as follows:

CPP / C++ / C Code:
Sa0 = V0*(1+(R*T0)/(V0*V0))*(1/32.17); /* Stream thrust function at 0 */

I tried typing into my calculator as it appears in the code, and the answer comes out correctly.

The only information needed for that particular equation to calculate is:
V0 = 10 000
R = 1730
T0 = 400

The variables above are all floats. I'm using the stdio.h, stdlib.h, math.h libraries.

Thanks in advance for any help.
  #2  
Old 13-Jul-2009, 05:56
Mexican Bob's Avatar
Mexican Bob Mexican Bob is offline
Member
 
Join Date: Mar 2008
Location: Chicxulub, Yucatán
Posts: 226
Mexican Bob is a jewel in the roughMexican Bob is a jewel in the roughMexican Bob is a jewel in the rough

Re: C incorrectly calculating an equation


Quote:
Originally Posted by jecco9
I tried typing into my calculator as it appears in the code, and the answer comes out correctly.


You probably are not a compiler. Did you type it into your calculator L-R or R-L? The compiler works from R-L. Try it again, working from the inner set of parentheses and R-L.


MxB
  #3  
Old 13-Jul-2009, 09:18
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: C incorrectly calculating an equation


Quote:
Originally Posted by Mexican Bob
...The compiler works from R-L. ...
Huh? R-L? L-R? What the heck does that have to do with anything in this particular problem?

Since there is no chance of overflow or underflow for this particular set of input values and there is no question of undefined behavior due to side-effects in the expression, the precedence rules guarantee the same result for this particular problem anywhichway you do it.

Anyhow, I entered the problem left-handed with my trusty Casio fx115/MS (while standing on my head---try it some time!) and I got the same answer as I did with my trusty C compilers from GNU, Borland, and Microsoft. (See Footnote.) In my experience, other calculators that have parentheses also have the same MDAS precedence rules where Multiplication and Division have precedence over addition and subtraction, and parentheses affect precedence the same way that a C or C++ compiler sees it, so that (other than roundoff error) the results should be the same.

Here is what the compiled program tells me:

Code:
Sa0 = 2151.383301

Since floats are probably only good for about six or seven significant decimal digits. and I wanted ten to be able to compare with my calculator's ten-digit output (and to be consistent with the number of my toes), I changed floats to doubles and got
Code:
Sa0 = 2151.383276

Note that, in general, there may be roundoff error in floating point calculations, but careful analysis of the values involved in this calculation leads me to believe that the computer result is correct to (at least) ten significant decimal digits.

Notes to Original Poster:

What calculator are you using?

What did you get from your calculator?

What compiler are you using?

What did you get from your compiler?

Finally, and here's the biggie: How do you know either one is "correct?"


Regards,

Dave

Footnote: I was going to try with my abacus, but I broke the decimal point when I tried to enter the problem with my left foot. (For this one I wimped out and was sitting in a chair, not standing on my head.) Muscle cramp due to old age makes my leg muscles twitch from time to time while using the left little toe.
Last edited by davekw7x : 13-Jul-2009 at 10:44.
  #4  
Old 13-Jul-2009, 14:29
Howard_L Howard_L is offline
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 802
Howard_L is a jewel in the roughHoward_L is a jewel in the roughHoward_L is a jewel in the rough

Re: C incorrectly calculating an equation


I don't know what I've done wrong but I don't get that answer. What is:
V0 = 10 000
10 space 000? (. . and something new to learn?)
If I use 10,000 for that I get: 312.999689
CPP / C++ / C Code:
#include <stdio.h>

int main(void)
{
  double Sa0, V0 = 10000, R = 1730, T0 = 400;

  /* verbatum copy-paste */
  Sa0 = V0*(1+(R*T0)/(V0*V0))*(1/32.17);

  printf("Sa0= %f \n\n", Sa0);
  
  return 0;
}
Code:
> gcc -Wall -W -pedantic equation_090713-1.c -o equation_090713-1 > ./equation_090713-1 Sa0= 312.999689 dave got 2151.383276 , so of course I've gotta say: what the heck... Is this a real problem? So I try by hand (well, with a little help from bc): Sa0 = V0*(1+(R*T0)/(V0*V0))*(1/32.17); Sa0 = V0 * ( 1 + ( R * T0 ) / ( V0 * V0 ) ) * ( 1 / 32.17); Sa0 = 10000 * ( 1 + ( 1730 * 400 ) / ( 10000 * 10000 ) ) * ( 1 / 32.17); Sa0 = 10000 * ( 1 + ( 692000 ) / ( 100,000,000 ) ) * ( .0310848 ); Sa0 = 10000 * ( 1 + ( .0069200 ) ) * ( .0310848 ); Sa0 = 10000 * ( 1.0069200 ) * ( .0310848 ); Sa0 = 10000 * ( .031299906816 ); Sa0 = 312.999068160000 Well, it's close to the same answer as the program but slightly different. . . Show work: > bc -q scale=20 1730 * 400 692000 10000 * 10000 100000000 692000 / 100000000 .00692000000000000000 1.00692 * .0310848 .031299906816 10000 * .031299906816 312.999068160000
So what am I doing wrong?
  #5  
Old 13-Jul-2009, 14:43
fairytales247 fairytales247 is offline
New Member
 
Join Date: Jul 2009
Posts: 15
fairytales247 has a little shameless behaviour in the past

Re: C incorrectly calculating an equation


You never now what goes wrong when you have a long equation. I suggest you break each thing down into smaller equations and that will take the load off your head and the compiler's too. And pay attention if you need to cast anywhere.
  #6  
Old 13-Jul-2009, 15:14
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: C incorrectly calculating an equation


Quote:
Originally Posted by Howard_L
...
If I use 10,000 for that I get: 312.999689

So do I.

One point that the Original Poster might have gleaned from my verbiage could have been that telling us exactly what the user obtained from the two efforts might be more instructive than simply saying, "I got the wrong answer," from one of them

The fact that I got the same answer from calculator and computer does not mean I got the "correct" answer, since I set V0 to 10.0 (As you pointed out, the expression shown in the original post wouldn't compile.) I was hoping to get a response showing how important it is to ask for help in the "right" way. See Footnote.

Thanks for posting the exact complete program that you were using. Now people might even test it and conclude that compilers are consistent and give results consistent with input values. Whether the output is the "correct" answer, well, I don't agree with the title of the thread that says "C incorrectly calculating equation" and I hope that specific information about the program will confirm that "C" doesn't calculate incorrectly.

My other point was to try to reassure users that writing legal arithmetic expressions in C and C++ will give results consistent with precedence rules, regardless of "L-R" or "R-L" or whatever.

Regards,

Dave

Footnote:

"Learning to ask the right (often hard) questions is an essential part of learning to be a programmer."

---Bjarne Stroustrup
Programming Principles and Practices using C++
Addison Wesley, December, 2008
Last edited by davekw7x : 13-Jul-2009 at 15:52.
  #7  
Old 22-Aug-2009, 10:57
science_man_88 science_man_88 is offline
New Member
 
Join Date: Aug 2009
Posts: 4
science_man_88 is an unknown quantity at this point

Re: C incorrectly calculating an equation


Could be an order of operation error if it goes through BEDMAS
 
 

Recent GIDBlogProblems with the Navy (Chiefs) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Solve Linear equation using VB deedex Miscellaneous Programming Forum 1 30-Nov-2008 15:59
Heat Equation degoman C Programming Language 1 20-Jun-2007 15:53
MPI with C (Solving Linear Equation using Gauss-Seidel Iteration) trainz13 C Programming Language 7 01-May-2007 23:38
How to print an equation in order of precedence of operators shoegoe C Programming Language 7 02-Nov-2006 15:14
::Need help Please:: using If & Switch in Quadratic equation. internetbug C Programming Language 6 07-Apr-2005 18:36

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 17:59.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.