View previous topic :: View next topic |
Author |
Message |
patrixchee
Joined: 26 Aug 2011 Posts: 2
|
floating point |
Posted: Fri Aug 26, 2011 1:21 am |
|
|
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
|
|
Posted: Fri Aug 26, 2011 5:44 am |
|
|
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.
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
|
|
Posted: Fri Aug 26, 2011 5:54 am |
|
|
MANY THANKS for the Helps
BestRegards _________________ patrixchee |
|
|
|