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

Help me with strtof Please

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



Joined: 28 Nov 2005
Posts: 12

View user's profile Send private message

Help me with strtof Please
PostPosted: Sun Aug 01, 2010 4:10 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Sun Aug 01, 2010 4:47 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Sun Aug 01, 2010 5:51 pm     Reply with quote

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

View user's profile Send private message AIM Address

PostPosted: Mon Aug 02, 2010 8:52 am     Reply with quote

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.
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