Boost logo

Boost :

From: Paul A Bristow (pbristow_at_[hidden])
Date: 2006-08-04 07:16:03


| -----Original Message-----
| From: boost-bounces_at_[hidden]
| [mailto:boost-bounces_at_[hidden]] On Behalf Of Alexander Nasonov
| Sent: 03 August 2006 19:53
| To: boost_at_[hidden]
| Subject: Re: [boost] Spurious warning in lexical_cast.hpp
| Gennaro Prota wrote:
| > On Wed, 2 Aug 2006 14:11:20 +0100, "Paul A Bristow"
| > <pbristow_at_[hidden]> wrote:
| >
| > >But this is STILL the WRONG formula for the number of digits!
| > >
| > >It should be max_digits10.
| > >
| > >stream.precision(2 + std::numeric_limits<Source>::digits
| * 301/1000);
| > >
| > >PLEASE can we get at long last get this right so that
| floats can be
| > >serialized without losing a few bits.
| I have not get a chance to study the subject yet but I
| wonder why don't
| you just set a precision to some huge number, say 32767 or INT_MAX?

This would be hugely wasteful as it would convert everything with 32767
decimal digits,
of which, even with 256-bit floating point, 32267 digits would be noise!

And in rely to Gennaro's question,

| stream.precision(2 + std::numeric_limits<Source>::digits * 301/1000);

| Does that work for non IEEE 754 floating points too?
| I guess not. And in that case we have to check.

It should be as good as you can get - certainly MUCH better than just using
the default of 6 decimal digits.
This is because numeric_limits<>::digits is the precision of the significand
in bits, and the formula divides by log2 ~= 3 to convert binary to decimal
and adds 2 to make sure there are enough to ensure a 1 least significant bit
change shows up.

For example, it works well with NTL quad-type (also used with Darwin long
double) which is not an IEEE754 type. And it is used with Boost.Test and
works fine with quad_float.

But one of the reasons for introducing numeric_limits<>::max_digits10 (apart
from making it easy and efficient for ordinary users) is to allow
specialisation of this value to suit user defined types.

For example, with arbitrary precision types, one could tie it to the
precision you have chosen, say 100 decimal digits with NTL RR type or the
current length an exact real type.

Sadly AKAIK, we can't just add max_digits10 to numerical_limits now - we
have to wait until the standard is accepted by library suppliers. So
lexical_cast will have to have another upgtrade later, but meanwhile we
should using this formula, perhaps internally defining a private
max_digits10 to ease this change later.
We certainly need a TODO comment.


Paul A Bristow
Prizet Farmhouse, Kendal, Cumbria UK LA8 8AB
+44 1539561830 & SMS, Mobile +44 7714 330204 & SMS

Boost list run by bdawes at, gregod at, cpdaniel at, john at