View previous topic :: View next topic |
Author |
Message |
cfernandez
Joined: 18 Oct 2003 Posts: 145
|
Float Question or BUG |
Posted: Mon Jun 14, 2004 3:53 pm |
|
|
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
|
|
Posted: Mon Jun 14, 2004 7:22 pm |
|
|
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
|
|
Posted: Mon Jun 14, 2004 7:50 pm |
|
|
Yes, your are correct. But, why printf show a diferent value??????? |
|
|
Douglas Kennedy
Joined: 07 Sep 2003 Posts: 755 Location: Florida
|
|
Posted: Tue Jun 15, 2004 8:30 am |
|
|
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
|
|
Posted: Tue Jun 15, 2004 8:42 am |
|
|
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
|
|
Posted: Tue Jun 15, 2004 10:55 am |
|
|
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!!! |
|
|
|