# atof() precision

This topic is 4458 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm running into a problem with atof() that is as follows. I am using strtok() to seperate 3 floating point values in a string delimited by a space. When I plug the values into atof(), I get a value that is slightly off from the value passed in. For example, if I pass in a string that contains "-0.384499" atof returns "-0.38449901" when casted to a float and "-0.38449899999999998" as an unchanged double. Is there something goofy with the MS atof() function or am I overlooking something?

##### Share on other sites
I know its not what you're looking for, but have you tried to use something like sscanf instead?

Edit: or even strtod()? I'm also pretty sure boost has something to handle convertions

Hope this helps

##### Share on other sites
Everything is in C++, so I'm trying to avoid using scanf() if possible, but if I can't figure out what the deal is with atof(), then I just might have to resort to it.

##### Share on other sites
You might wanna look into

float f;
std::string s;

f = boost::lexical_cast<float>(s);
s = boost::lexical_cast<std::string>(f);

(boost is available at www.boost.org)

##### Share on other sites

I'm not realy a pro, so i'd like to know why not to use std::istringstream or std::istrstream in such situation? Thank you.

##### Share on other sites
I am using ifstream to buffer the entire file into a string, becuase I need to make multiple passes through the file data. I then use istringstream to retrieve data line by line. I then dump the string floating point values into atof(), which is where the problem lies.

##### Share on other sites
If your already using stringsreams I don't understand why you want to use atof

#include <stream>#include <sstream>int main(int,char**){	char const szFloat[]="1.2234";	char const szFloat2[] = "112.5";	std::stringstream convert;	float fval;	convert << szFloat;	convert >> fval;	std::cout << fval << std::endl;		// must clear the state prior to using insertion operator again	// you need to do this each time after you've switched between 	// insertion and extraction operators	convert.clear( );	// I'm also going to empty the character stream	convert.str("");	convert << szFloat2;	convert >> fval;	std::cout << fval << std::endl;		return 0;}

But if you don't want to switch to using the pure C++ way - I guess you could search on the internet for ways of setting the floating point precision for the C runtime library. I don't know if its possible off the top of my head, but that seems to be the issue your fighting against.

##### Share on other sites
The numbers you posted simply cannot be represented by floats or doubles. It has nothing to do with atof.

##### Share on other sites
Doubles don't have infinite precision. Doubles are binary, not decimal. These two facts make them generally incapable of exactly representing most things that have a finite number of decimal digits.

##### Share on other sites
Quote:
 Original post by Puzzler183Doubles don't have infinite precision. Doubles are binary, not decimal. These two facts make them generally incapable of exactly representing most things that have a finite number of decimal digits.

Yes, this I understand. However, I was under the assumption that these values were once represented in a binary fashion, since they are an export from a modeling program. However, after closer inspection, these numbers cannot be represented exactly in float/double form. So it's back to the drawing board on this one.

1. 1
2. 2
Rutin
19
3. 3
JoeJ
16
4. 4
5. 5

• 30
• 21
• 13
• 13
• 17
• ### Forum Statistics

• Total Topics
631700
• Total Posts
3001799
×