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

Float Question or BUG

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



Joined: 18 Oct 2003
Posts: 145

View user's profile Send private message

Float Question or BUG
PostPosted: Mon Jun 14, 2004 3:53 pm     Reply with quote

Hi,

I am use the 3.202 version and I have a question about the float.

Code:
main()
{
float fTmp = 1920002.0;

printf("%f\n\r", fTmp );           //Output is: 190001.986920
fTmp += 0.5;
printf("%f\n\r", fTmp );           //Output is: 190002.486109
printf("%f\n\r", floor(fTmp) );    //Output is: 190001.986920
sleep();
}


This is Ok???

I expect and think that the correct values are:

Code:
main()
{
float fTmp = 1920002.0;

printf("%f\n\r", fTmp );           //Output is: 190002.000000
fTmp += 0.5;
printf("%f\n\r", fTmp );           //Output is: 190002.500000
printf("%f\n\r", floor(fTmp) );    //Output is: 190002.000000
sleep();
}


Please explain me the solution or the problem about this. I need to calculate the Julian Date and is impossible with this error.

Thank you very much!!

Best Regards,
Guest








PostPosted: Mon Jun 14, 2004 7:22 pm     Reply with quote

CCS --> HELP --> COMMON QUESTIONS AND ANSWERS --> What is the format of floating point numbers?

the accuracy for 23 bit Mantisa is 1/2^23 ~ 1.2e-7

so, ( 190002 - 190001.986920 ) / 190002 ~ 7e-8 is not bad.
cfernandez



Joined: 18 Oct 2003
Posts: 145

View user's profile Send private message

PostPosted: Mon Jun 14, 2004 7:50 pm     Reply with quote

Yes, your are correct. But, why printf show a diferent value???????
Douglas Kennedy



Joined: 07 Sep 2003
Posts: 755
Location: Florida

View user's profile Send private message AIM Address

PostPosted: Tue Jun 15, 2004 8:30 am     Reply with quote

This comes up often. The unexpected results come from two sources .
First
The representation of numbers has to end with a finite precision to conserve memory. Specifically it is 23 bits in CCS float. Now, that doesn't mean that every number isn't accurate it's just that numbers requiring more than 23 bits of precision are clipped.
Second
The base of a number system ( base ten or base two) influences how accurately a number can be represented. Ex 0.1 in decimal is unable to be represented in the binary system ( base two) without some precision error due to clipping after a certain number of bits Ex 23 bits.
However there is no loss of accuracy for the number 0.5 in decimal or binary.

Nothing bad is going on nor can it be avoided unless you go to doing what a pocket calculator does namely it does everything in binary coded decimal
( it never leaves the decimal system ) so is always accurate in decimal as far as the display of digits allow. Now even a pocket calculator cannot express 1/3 without some clipping after a certain number of digits. You'd have to go to base 3 arithmetic to represent 1/3 accurately.
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

PostPosted: Tue Jun 15, 2004 8:42 am     Reply with quote

For the Julian date I suggest sticking with integers. and adding a decimal point if need be using print formatting. Never use floating point for anything that has to be accurate, banks don't!
_________________
The search for better is endless. Instead simply find very good and get the job done.
cfernandez



Joined: 18 Oct 2003
Posts: 145

View user's profile Send private message

PostPosted: Tue Jun 15, 2004 10:55 am     Reply with quote

The only solution is the ROUND( X, N ) function? for round the decimal and display correctly?. Any have this function? or Any have a function to add days to date?

Thank you!!!
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