atof() precision

Started by
13 comments, last by iMalc 17 years, 11 months ago
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?

I know only that which I know, but I do not know what I know.
Advertisement
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
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.

I know only that which I know, but I do not know what I know.
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)

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

I know only that which I know, but I do not know what I know.
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;}


[edit]
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.
moe.ron
The numbers you posted simply cannot be represented by floats or doubles. It has nothing to do with atof.
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.
Quote:Original post by Puzzler183
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.


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.

I know only that which I know, but I do not know what I know.

This topic is closed to new replies.

Advertisement