|
|
View previous topic :: View next topic |
Author |
Message |
kda406
Joined: 17 Sep 2003 Posts: 97 Location: Atlanta, GA, USA
|
float printing BUG workaround |
Posted: Tue Mar 15, 2005 3:11 pm |
|
|
I just can't get floats to print out correctly with CCS. At first I thought it was a math problem, but after days of messing with it, I believe it is a float or printf problem. To illustrate my point I put together this simple program:
Code: | int32 w = 3600;
float f;
f = w;
fprintf(TERM,"%1.2f\n\r",f); |
I expect it to print 3600.00, but it always prints 3599.99. No matter what value I put in w, it always prints out wrong value in f.
How can I work around this unacceptable problem?
-Kyle |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Tue Mar 15, 2005 3:22 pm |
|
|
Has to do with floating point accuracy. You are not alone and if you search the forum you will find this posted many times. Maybe even within the last week. ttelmah posted a way to round this recently. |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Tue Mar 15, 2005 3:28 pm |
|
|
What happens if you try:
Code: |
int32 w = 3600;
float f;
f = w;
f+= 0.005;
fprintf(TERM,"%1.2f\n\r",f);
|
That moves the truncation point up a little. I would guess that f=3599.9999 or so and it is just truncating at an unfortunate place. To find out what is REALLY happening you should print f in hex and see what the exponent and mantissa are.
Also tell us what compiler version you are using. _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
|
Guest
|
|
Posted: Tue Mar 15, 2005 8:40 pm |
|
|
SherpaDoug wrote: | What happens if you try:
Code: |
int32 w = 3600;
float f;
f = w;
f+= 0.005;
fprintf(TERM,"%1.2f\n\r",f);
|
|
in stead of using f+= 0.005, I would like to use f *= 1.000001
besides, floating point multiplication is faster than addition! |
|
|
Douglas Kennedy
Joined: 07 Sep 2003 Posts: 755 Location: Florida
|
|
Posted: Wed Mar 16, 2005 2:23 am |
|
|
Any whole number less than 2^23 can be stored with perfect precision in the 4 byte floating point format CCS uses. 3600 certainly fits this criteria.
Now if you add .1 base ten or 1 to any negative power of ten then there is inherent differences due the the fundamental differences between the binary and the decimal systems.
The error has to be in the printf conversion to characters for display...its going to do divisions by ten against the float to tease out the digits and the error is accumulating in the sucessive divisions. Did you try casting back the result to a Int32 and printing the int32 out instead of the float? |
|
|
kda406
Joined: 17 Sep 2003 Posts: 97 Location: Atlanta, GA, USA
|
|
Posted: Wed Mar 16, 2005 8:36 am |
|
|
Thanks for all the replies. Adding 0.005 produces a result I might be able to live with. Multiplying like this "f *= 1.000001" didn't work. Perhaps I need to mess with the fudge factor, but I've wasted enough time on this stupid issue already.
Although I appreciate the information about the inherent differences in binary and decimal which I am painfully aware of, it is no excuse for the compiler not doing its job. Not one other C compiler I use has this problem. This is CRAP, CCS.
The number 3600 in the example represents 36.00 degrees farenheight. Likewise, 14278 would be 142.78. I guess I can work out a 10-20 line program to print out this int32 as a pair of unrelated numbers with a dumb period in the middle, but geez, it really shouldn't be that hard.
FWIW, the main processor on this board (the one doing this printing) is an 18F8720. I'm not sure which compiler that uses in the PCWH system, PCH perhaps? Other C compilers are able to comiple highly optimized code for multiple different platforms (not just processors) by simply passing command line flags. I'm not sure why CCS feels like they need three different programs to compile for this one family of processors when others can handle altogether different plaforms from within a single compiler. But that's another shortcoming for another thread....
Again, thanks for the work arounds.
-Kyle |
|
|
bluetooth
Joined: 08 Jan 2005 Posts: 74
|
|
Posted: Wed Mar 16, 2005 8:39 am |
|
|
Perhaps you should save yourself all this frustration and use one of those other compilers! |
|
|
kda406
Joined: 17 Sep 2003 Posts: 97 Location: Atlanta, GA, USA
|
|
Posted: Wed Mar 16, 2005 9:33 am |
|
|
I suppose I am too idealistic in expecting a company I pay for a product to actually do their job and produce a working, comparable product. Hey I do software for a living, I know bugs are inevitable. The difference is, I fix bugs in my code. Printing floating point and doing FP math is not a new feature in CCS, which means they have left it incomplete for years.
As far as your comment about using another compiler, every compiler company has their initial cost and annual fees. With all the other fingers in the pie, my company's budget can only sustain the single PIC compiler I have already purchased, so I am stuck with CCS. Are you suggesting that everybody that has this problem change compilers too? Maybe that would get them to fix it.
Qutoing Mark:
Quote: | You are not alone and if you search the forum you will find this posted many times. Maybe even within the last week. |
I am not out in left field here. Aparently lots of people are having this problem. If other compilers can overcome this problem, then so should CCS. I simply expect them to do their job, but I suppose you feel that is too much to ask.
-Kyle |
|
|
bluetooth
Joined: 08 Jan 2005 Posts: 74
|
|
Posted: Wed Mar 16, 2005 10:01 am |
|
|
Kyle:
Calm down - there's no need to get upset!
No, I am not suggesting that everyone switch compilers... it's just that all the energy that seems to being going in to these posts would be better spent just working around the problem.
As the quote from Mark points out, this is not a new problem. Most folks have a different version of this problem because they don't understand how floats are represented. This one appears to be related to how printf does the conversion. There are lots of ways to deal with this.
All environments have their features and limitations - you just have to weigh these against each other to best fit your needs. The amount of money CCS charges for this environment is, in my opinion, paltry. It performs way beyond what I would expect from such an inexpensive tool.
In the mean time, report the problem to CCS and perhaps they will address it in an upcoming release.
|
|
|
mvaraujo
Joined: 20 Feb 2004 Posts: 59 Location: Brazil
|
|
Posted: Wed Mar 16, 2005 11:33 am |
|
|
Kyle,
Out of the compiler discussion, is it really necessary going for float on the code?
I always keep it as simplest integer and use integer operations to get decimal parts.
It goes quick in most cases. And it can get quicker if you represent in power of 2 numbers.
Best regards,
Marcus |
|
|
Guest
|
|
Posted: Wed Mar 16, 2005 12:00 pm |
|
|
kda406 wrote: | I suppose I am too idealistic in expecting a company I pay for a product to actually do their job and produce a working, comparable product. Hey I do software for a living, I know bugs are inevitable. The difference is, I fix bugs in my code. Printing floating point and doing FP math is not a new feature in CCS, which means they have left it incomplete for years.
As far as your comment about using another compiler, every compiler company has their initial cost and annual fees. With all the other fingers in the pie, my company's budget can only sustain the single PIC compiler I have already purchased, so I am stuck with CCS. Are you suggesting that everybody that has this problem change compilers too? Maybe that would get them to fix it.
Qutoing Mark:
Quote: | You are not alone and if you search the forum you will find this posted many times. Maybe even within the last week. |
I am not out in left field here. Aparently lots of people are having this problem. If other compilers can overcome this problem, then so should CCS. I simply expect them to do their job, but I suppose you feel that is too much to ask.
-Kyle |
Hey Microsoft make a huge bussiness doing this. If everything was perfect, there would be no real reason to upgrade. |
|
|
Guest
|
|
Posted: Wed Mar 16, 2005 12:11 pm |
|
|
bluetooth wrote: | Kyle:
Calm down - there's no need to get upset!
No, I am not suggesting that everyone switch compilers... it's just that all the energy that seems to being going in to these posts would be better spent just working around the problem.
As the quote from Mark points out, this is not a new problem. Most folks have a different version of this problem because they don't understand how floats are represented. This one appears to be related to how printf does the conversion. There are lots of ways to deal with this.
All environments have their features and limitations - you just have to weigh these against each other to best fit your needs. The amount of money CCS charges for this environment is, in my opinion, paltry. It performs way beyond what I would expect from such an inexpensive tool.
In the mean time, report the problem to CCS and perhaps they will address it in an upcoming release.
|
Maybe not in the situation of a PIC compiler, but I have seen freeware work just as good (even better) than payware. To say "you really can't expect more" is a cop-out and is why companies can get away with not fixing it.
I guess this problem is just another example of why open source is becoming so much more popular these days. Instead of waiting for a company to get their butt is gear with a fix, a community could have created a fix for it. Not just a hack! |
|
|
bluetooth
Joined: 08 Jan 2005 Posts: 74
|
|
Posted: Wed Mar 16, 2005 1:44 pm |
|
|
To the 1:11 pm Guest:
No cop out - you get what you pay for. I think CCS gives you a LOT of value for what you pay for - if you wanted to, there are hundreds of little things with this (and any) tool suite that you could complain about. Make no mistake - compilers are TOUGH to implement, and good ones are even tougher.
Maybe someone will organize an effort to make a free, open source PIC environment that's superior to CCS, Microchip, Hi-Tech et. al. For starters, it better handle 256 bit floats perfectly and better darn well have pointers to constants that use NO extra code space to implement . And it better work equally well on a 12C508 as an 18F8720! And don't forget - FREE!!!!!
I can't wait!
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Mar 16, 2005 2:23 pm |
|
|
I think there might be a bug.
According to Microchip AN575, if a decimal number consists
of the sums of powers of 2, then it can be represented exactly
in their floating point notation. This is true for 3600:
Code: |
2048
+ 1024
+ 512
+ 16
--------
3600
|
To find out what the floating point representation of 3600 is,
go to this page, and download the file called floatconv10.zip.
http://www.sxlist.com/techref/microchip/math/fpconvert.htm
Unzip the executable program called floatconv.exe and run it.
Select "Microchip 32 bit" on the left side and type in 3600 in the
top box. Then click "Convert to Byte". The results are 8A 61 00 00.
Also, compile the following test program with PCM 3.221, and look
at the .LST file. It also has 8A 61 00 00.
Code: | #include <16F877.H>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock = 4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
//============================
main()
{
float f;
f = 3600.00;
printf("%f \n\r", f);
while(1);
} |
Here is part of the .LST file. It's storing 8A 61 00 00.
Code: | .................... f = 3600.00;
*
017B: CLRF 24 // 00
017C: CLRF 23 // 00
017D: MOVLW 61 // 61
017E: MOVWF 22
017F: MOVLW 8A // 8A
0180: MOVWF 21 |
So the number 3600 is being stored correctly. The problem must
be in the CCS runtime code for printf, that converts from floating point
to ASCII. Microchip has AN670, which shows ASM code to convert
from floating point to ASCII base 10. I haven't gone through that code
and compared it to the CCS routines, but that would be the next step. |
|
|
|
|
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
|