CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Absolutely stumped - float subtraction

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
evsource



Joined: 21 Nov 2006
Posts: 129

View user's profile Send private message

Absolutely stumped - float subtraction
PostPosted: Sun Jun 17, 2007 12:05 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sun Jun 17, 2007 12:52 am     Reply with quote

Post a small, compilable test program. Show all variable declarations.
Ttelmah
Guest







PostPosted: Sun Jun 17, 2007 2:51 am     Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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