View previous topic :: View next topic |
Author |
Message |
stewart
Joined: 28 Nov 2005 Posts: 12
|
Help me with strtof Please |
Posted: Sun Aug 01, 2010 4:10 pm |
|
|
Hi all, I am trying to use strtof. Can anyone point out what I am doing wrong???
Here is the code:
Code: |
strcpy(scale_buf, "143.49");
ret_float = strtof(scale_buf, end_ptr);
fprintf(DEBUG, "Float value of %s is %18.7f\r\n", scale_buf, ret_float );
strcpy(scale_buf, "-143.49");
ret_float = strtof(scale_buf, end_ptr);
fprintf(DEBUG, "Float value of %s is %18.7f\r\n", scale_buf, ret_float );
|
Here is the output:
Float value of 143.49 is 143.4899902
Float value of -143.49 is -143.4899902
V4.104 and V4.093 get the same results
Thanks,
Stewart |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Aug 01, 2010 4:47 pm |
|
|
Quote: | Float value of 143.49 is 143.4899902
|
Download the FloatConv program in this zip file.
Drag the FloatConv.exe file onto your desktop and click on it.
http://www.piclist.com/images/floatconv10.zip
1. Select "Microchip 32 bit" as the float type, because that's what CCS uses.
2. Type 143.49 into the top Float box.
3. Press the button "Convert to Byte".
4. Now press the button "Convert to Float".
What do you get ? (and what do you learn). |
|
|
stewart
Joined: 28 Nov 2005 Posts: 12
|
|
Posted: Sun Aug 01, 2010 5:51 pm |
|
|
Thanks,
I should have known, it is a simple matter of precision. I somehow thought that it was a bit more precise, but, given the huge range that can be represented, it really is quite impressive.
Sorry for the bother. |
|
|
Douglas Kennedy
Joined: 07 Sep 2003 Posts: 755 Location: Florida
|
|
Posted: Mon Aug 02, 2010 8:52 am |
|
|
This is not precision in the sense that notation floating point binary is less precise than decimal notation. It is just notation. The rational 1/3 can't be written in decimal notation precisely either. Whenever a base in this case 10 is changed to binary notation it will not always neatly follow the decimal notation except for a value that is a power series of 2. This is a fundamental fact about number notation. |
|
|
|