![]() |
|
#1
|
|||
|
|||
C incorrectly calculating an equationI'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:
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
|
||||
|
||||
Re: C incorrectly calculating an equationQuote:
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
|
|||
|
|||
Re: C incorrectly calculating an equationQuote:
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:
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:
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
|
|||
|
|||
Re: C incorrectly calculating an equationI 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:
Code:
|
|
#5
|
|||
|
|||
Re: C incorrectly calculating an equationYou 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
|
|||
|
|||
Re: C incorrectly calculating an equationQuote:
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
|
|||
|
|||
Re: C incorrectly calculating an equationCould be an order of operation error if it goes through BEDMAS
|
Recent GIDBlog
Problems with the Navy (Chiefs) by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
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