[lexical-cast] double not converted as it is to a string

Hello, I am seeing this behavious with boost::lexical_caststd::string(double) occasionaly numbers like 76.32 are being converted to strings of the form 76.319999999 The expected behavior is seen for almost all doubles except for a few which are reproducible e.g. 76.32.. here is a simple program, int main() { double d=76.32; std::string s = boost::lexical_caststd::string(d); cout << s << endl; } output 76.319999999... a) am i doing something wrong ?? b) if this is not wrong s this standard behavior ?? Thanks Digz

-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Digvijoy Chatterjee schrieb:
... here is a simple program, int main() { double d=76.32; std::string s = boost::lexical_caststd::string(d); cout << s << endl; }
output 76.319999999...
a) am i doing something wrong ??
I don't think so.
b) if this is not wrong s this standard behavior ??
I'd say so. Due to the binary representation not all numbers with a representation of a fixed amount of digits in our decimal system have such a representation in the computer. Lexical_cast internally uses standard streams, if they don't correct such things (and I don't know of any implemementation that'd do so), your output will look garbaged. Try adding std::cout << d << std::endl. - -- - -ness- -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.6 (GNU/Linux) iD8DBQFGD3V8vD/ijq9JWhsRAqaBAJ9bIJh5erQWZcPGz3Xbgl1xsqtVsACfc2R4 eJj/WLx3HHGkOcahvg7reyg= =zDxW -----END PGP SIGNATURE-----

Digvijoy Chatterjee schrieb:
... here is a simple program, int main() { double d=76.32; std::string s = boost::lexical_caststd::string(d); cout << s << endl; }
output 76.319999999...
a) am i doing something wrong ??
I don't think so.
b) if this is not wrong s this standard behavior ??
I'd say so. Due to the binary representation not all numbers with a representation of a fixed amount of digits in our decimal system have such a representation in the computer. Lexical_cast internally uses standard streams, if they don't correct such things (and I don't know of any implemementation that'd do so), your output will look garbaged. Try adding std::cout << d << std::endl.
adding std::cout << d << std::endl; does prints it out correctly, it calls setprecision with a default value of 6 which does rounding , I assume there is no way to play with setprecision and lexical_cast , or to a more general problem of rounding doubles to certain places of decimals while converting them to strings, as u say
not all numbers with a representation of a fixed amount of digits in our decimal system have such a representation in the computer.
I switched to ostringstream ,which does the job for the moment, because of setprecision (6) but the code now is not as succinct as lexical_cast. Thx Digz

In article <2f7228250703312202y4cce5ac0o870fe26b59acfa7e@mail.gmail.com>,
"Digvijoy Chatterjee"
I am seeing this behavious with boost::lexical_caststd::string(double) occasionaly numbers like 76.32 are being converted to strings of the form 76.319999999
http://www.lahey.com/float.htm -- I changed my name: http://periodic-kingdom.org/People/NameChange.php
participants (3)
-
Ben Artin
-
Digvijoy Chatterjee
-
Tom Bachmann