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 CCS Technical Support

floating point

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



Joined: 26 Aug 2011
Posts: 2

View user's profile Send private message

floating point
PostPosted: Fri Aug 26, 2011 1:21 am     Reply with quote

Dear Sir
Please Help

Problem on atof()
This example is from the ccs compiler manual.
Quote:

atof( )

Syntax:
result = atof (string)

Parameters:
string is a pointer to a null terminated string of characters.

Returns:
Result is a floating point number

Function:
Converts the string passed to the function into a floating point representation.
If the result cannot be represented, the behavior is undefined.

Availability:
All devices

Requires:
#include <stdlib.h>

Examples:
char string [10];
float x;

strcpy (string, "123.456");
x = atof(string);
// x is now 123.456

I can never get this result after running this example test, "12.456"
The result I got for x is 123.455

MANY THANKS
_________________
patrixchee
RF_Developer



Joined: 07 Feb 2011
Posts: 839

View user's profile Send private message

PostPosted: Fri Aug 26, 2011 5:44 am     Reply with quote

Its very probably perfectly normal and expected floating point behaviour. Floating point has precision limited by the the format's mantissa length. Single length floats are often, even more so historically, limited to around 6 or 7 significant digits. That depends totally on the floating point format being used. Also the routine may, or may not round.

What's probably wrong in this case is that the manual has got an untested and "wrong" example. There are many other examples. Crying or Very sad

Try "0.1" instead, and see what that gives you, as it cannot be represented precisely in binary floating point...

RF Developer.
patrixchee



Joined: 26 Aug 2011
Posts: 2

View user's profile Send private message

PostPosted: Fri Aug 26, 2011 5:54 am     Reply with quote

MANY THANKS for the Helps

BestRegards
_________________
patrixchee
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