|
|
View previous topic :: View next topic |
Author |
Message |
evsource
Joined: 21 Nov 2006 Posts: 129
|
Absolutely stumped - float subtraction |
Posted: Sun Jun 17, 2007 12:05 am |
|
|
As the subject line says, I'm so completely stumped on this problem.
I'm trying to do a simple subtraction with two floats. I can output the separate values that I'm working with, and they are what they are, but when I try to do a subtraction, I get "0.00" for the answer.
Here's the code:
Code: | temp = (float)0.00946 * (float)cable_lengths[i] * current_calibrated[i];
fprintf(external,"unit: %u, temp: %5.2f\r\nvoltage: %5.2f, calc: %5.2f\r\n",i,temp, voltage_calibrated[i],voltage_calibrated[i] - temp); |
Example values: voltage: 11.88, temp: 0.00, calc: 0.00 (calc should obviously be 11.88 in this case).
What am I missing? BTW, if I hard-code in 2.0 in place of temp in the calculation, it would output 9.88 (as expected). What's wrong with that temp value when it tries to do the subtraction? BTW, voltage_calibrated and current_calibrated are floats.
Very stumped.
Oh, on a 18F2520, PCH V4.030. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Jun 17, 2007 12:52 am |
|
|
Post a small, compilable test program. Show all variable declarations. |
|
|
Ttelmah Guest
|
|
Posted: Sun Jun 17, 2007 2:51 am |
|
|
Also, 'what compiler version'?.
However as a comment, try the subtraction outside the printf.
There was a problem on some compilers, where if certain maths operations are performed inside the printf, a 'scratch' variable used in the arithmetic, gets destroyed, and you get screwy results. Your fault could be an example of this.
Best Wishes |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|