Boost logo

Boost :

Subject: Re: [boost] [lexical_cast] Assumed internal double conversion of float?
From: Stephan T. Lavavej (stl_at_[hidden])
Date: 2019-01-29 21:29:39


Note that there are two numbers of digits that are of interest:

If you take a decimal number, convert it to float, and convert it back to decimal, how many decimal digits are guaranteed to survive unchanged?

If you take a float, convert it to decimal, and convert it back to float, how many decimal digits are required to always get all bits of the original float correct?

For 32-bit float, the answers are 6 and 9, respectively. For 64-bit double, it's 15 and 17.

See: https://www.exploringbinary.com/number-of-digits-required-for-round-trip-conversions/

As an aside: when converting a string to a floating-point number, it is incorrect to round twice - i.e. you cannot implement strtof by calling strtod and then rounding double to float.

STL

-----Original Message-----
From: Boost <boost-bounces_at_[hidden]> On Behalf Of Peter Dimov via Boost
Sent: Tuesday, January 29, 2019 9:42 AM
To: boost_at_[hidden]
Cc: Peter Dimov <lists_at_[hidden]>
Subject: Re: [boost] [lexical_cast] Assumed internal double conversion of float?

Brian Kuhl wrote:

> Where does boost::detail::lcast_get_precision<float>() come from, and
> why is it 9 ?
> Is it perhaps assuming some intermediate double conversion internally?

boost::detail::lcast_get_precision<double>() is 17, so no.

My guess is that this is the precision that guarantees a perfect roundtrip.

_______________________________________________
Unsubscribe & other changes: https://nam06.safelinks.protection.outlook.com/?url=http%3A%2F%2Flists.boost.org%2Fmailman%2Flistinfo.cgi%2Fboost&amp;data=02%7C01%7Cstl%40exchange.microsoft.com%7Ce8d933dc57634660657d08d686112ef9%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C636843805695126880&amp;sdata=mXgKJMlGxrXod%2BSb8A3HX4F1rFByOZNGZnN4NpeII3w%3D&amp;reserved=0


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk