Subject: Re: [Boost-bugs] [Boost C++ Libraries] #5796: Casting from float to string using "lexical_cast<string>" gives inaccurate values.
From: Boost C++ Libraries (noreply_at_[hidden])
Date: 2011-08-22 15:38:32
#5796: Casting from float to string using "lexical_cast<string>" gives inaccurate
values.
-----------------------------------------------------+----------------------
Reporter: Mikko Partonen <mikkopartonen@â¦> | Owner: nasonov
Type: Bugs | Status: new
Milestone: To Be Determined | Component: lexical_cast
Version: Boost 1.47.0 | Severity: Cosmetic
Resolution: | Keywords: lexical_cast, float
-----------------------------------------------------+----------------------
Comment (by apolukhin):
Replying to [ticket:5796 Mikko Partonen <mikkopartonen@â¦>]:
> When casting floats to strings with lexical_cast<string>() for use in
wxTextCtrl, the new boost versions adds a lot of nines or zeroes along
with some random numbers at the end of the float values.
> For example, with an older boost version, I get "0.96" and "0.8" and
with new versions, I get "0.959999979" and "0.800000012".[[BR]] [[BR]]
It is not a random number, it is the exact representation of float number:
http://www.parashift.com/c++-faq-lite/newbie.html#faq-29.16 [[BR]] [[BR]]
> This can easily be fixed by using an older version of lexical_cast.hpp
file. Wouldn't it be better to fix this issue, so that I wouldn't need to
use the old version of lexical_cast.hpp? [[BR]] [[BR]]
Think about all the people, who used an updated version of this library
(years passed since 2005, so there will be plenty of them). They relay on
current behavior, and such changes will break their code. [[BR]] [[BR]]
Current behavior was chosen because more people wanted to see precise
numbers. When you convert number "1.000000099" in most cases you would
like to see number "1.000000099" in output, not "1".
You can always use std::stringstream to convert numbers with specified
precision.
-- Ticket URL: <https://svn.boost.org/trac/boost/ticket/5796#comment:1> Boost C++ Libraries <http://www.boost.org/> Boost provides free peer-reviewed portable C++ source libraries.
This archive was generated by hypermail 2.1.7 : 2017-02-16 18:50:07 UTC