Subject: Re: [boost] [serialization] round-trip serialization of float
From: Adam Lerer (adam.lerer_at_[hidden])
Date: 2013-01-11 14:04:15
>> a) not every binary floating number has en exact representation when
>> rendered as decimal.
True, but if max_digits10 digits is used, you can at least guarantee
that the binary representation will deserialize as the same float.
This is very helpful for common use cases of the serialization library
I understand that float serialization cannot be guaranteed to work
correctly in all cases. That said, the precision used for float
serialization is a choice, and some choices are better than others. It
seems to me that the choice of max_digits10 (or even better, Paul's
alternative formula to work around the VS bug) is superior to
It might also be helpful to note that lexical_cast uses exactly the
formula that Paul recommends. In lcast_precision.hpp:
BOOST_STATIC_CONSTANT(unsigned long, precision_bin =
2UL + limits::digits * 30103UL / 100000UL
On Thu, Jan 10, 2013 at 3:07 PM, Robert Ramey <ramey_at_[hidden]> wrote:
> Olaf van der Spek wrote:
>> On Thu, Jan 10, 2013 at 5:28 PM, Robert Ramey <ramey_at_[hidden]> wrote:
>>> So, If you feel you need to preserver the exact set of bits - don't
>>> use floating
>>> point - use something else.
>> Is it really unreasonable to expect a round trip to result in the same
>> value on the same platform with the same code/binary?
> but don't take my word for it - ask an author of a standard
> floating point library.
> btw - if it's the same code/binary you can use binary_archive
> which saves/loads the bits - not really the value. In this circumstance
> the issue never arises. Maybe that's what you really want.
> even with a text base archive, you would have the option
> of saving your data as binary object. This would load
> bit for bit the same. But the archive wouldn't be portable
> accross platforms - thus defeating the motivating purpose
> for a text base archive in the first place.
> can't have it both ways.
> Robert Ramey
> Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk