[Conversion] imprecision with lexical_cast from double to std::string

Hi, I'm using the borland toolset 5.8.2 under Win XP and have a problem doing the following: double val = 795.6352; string result = boost::lexical_cast <string> (val); the resulting string is: "795.63520000000005" with boost 1_35_0 the resulting string is: "795.6352000000001" with boost_1_34_0 Can anybody help?

Andreas Ries wrote:
Hi, I'm using the borland toolset 5.8.2 under Win XP and have a problem doing the following:
double val = 795.6352; string result = boost::lexical_cast <string> (val);
the resulting string is: "795.63520000000005" with boost 1_35_0 the resulting string is: "795.6352000000001" with boost_1_34_0 Can anybody help?
This describes a bug I just found as well. I didn't see any follow up to this message when I looked through my archived boost mail. Looks like boost::lexical_cast for double and float got broken between boost 1_33 and boost 1_34 and now changes the precision of the cast and introduces error, and this was never fixed. This could cause havoc on the application I'm maintaining, which uses lexical _cast extensively and expects a precision of 2 so our users don't ever have to look at a number like the ones above. Unfortunately, I can't revert to boost 1_33 because we switched to Visual Studio 9 for our windows port. We build on gcc 4.1.2 on Unix platforms and, as I already mentioned VS9 on Windows, and we get this problem everywhere. Is there a workaround for this or a fix in the pipeline? If not, I guess it's time to write our own string wrappers again, so we know what it's going to do >hurghle<. mike

on Wed Jul 30 2008, Michael Linck
Andreas Ries wrote:
Hi, I'm using the borland toolset 5.8.2 under Win XP and have a problem doing the following:
double val = 795.6352; string result = boost::lexical_cast <string> (val);
the resulting string is: "795.63520000000005" with boost 1_35_0 the resulting string is: "795.6352000000001" with boost_1_34_0 Can anybody help?
This describes a bug I just found as well. I didn't see any follow up to this message when I looked through my archived boost mail. Looks like boost::lexical_cast for double and float got broken between boost 1_33 and boost 1_34 and now changes the precision of the cast and introduces error, and this was never fixed. This could cause havoc on the application I'm maintaining, which uses lexical _cast extensively and expects a precision of 2 so our users don't ever have to look at a number like the ones above. Unfortunately, I can't revert to boost 1_33 because we switched to Visual Studio 9 for our windows port. We build on gcc 4.1.2 on Unix platforms and, as I already mentioned VS9 on Windows, and we get this problem everywhere. Is there a workaround for this or a fix in the pipeline? If not, I guess it's time to write our own string wrappers again, so we know what it's going to do >hurghle<.
Please enter a ticket for this at http://svn.boost.org so it doesn't get lost; thanks! -- Dave Abrahams BoostPro Computing http://www.boostpro.com
participants (3)
-
Andreas Ries
-
David Abrahams
-
Michael Linck