From: Paul A. Bristow (boost_at_[hidden])
Date: 2003-08-17 16:33:53
Sorry about the long post, but that seemed the easiest way.
numeric_limits::digits10 does what it says - the number that are _guaranteed_ to
be correct on output,
but that isn't what one usually wants for all _significant_ on input (and I
suggest again another numeric_limits::significant_digits10 which is what you get
from the formaula below).
But you are right that it would be better to check that numeric_limits::digits
exists and isn't something silly before using the formula. With all the
built-in floating point types it should be fine, and for other (well) User
defined floating point types too. (I will look at this).
But using +3 instead would be 1 decimal digit too many for most of the FP types.
Since it is done at compile time, getting it 'just right' with the formula below
shouldn't cost anything at run-time.
Incidentally a suitable sort of test is
float const aFloat = any_value;
if (aFloat != lexical_cast<float>(lexical_cast<string>(aFloat )))
I think from previous experience that this can be shown to be true for ALL
possible float values (for 32 bit representations) but it would take too long to
test all doubles (assuming 64 bit representation).
Paul A Bristow, Prizet Farmhouse, Kendal, Cumbria, LA8 8AB UK
+44 1539 561830 Mobile +44 7714 33 02 04
| -----Original Message-----
| From: boost-bounces_at_[hidden]
| [mailto:boost-bounces_at_[hidden]]On Behalf Of Daryle Walker
| Sent: Tuesday, August 12, 2003 8:56 AM
| To: Boost
| Subject: [boost] Re: Insufficient significant digits using lexical_cast
| [Paul claims that lexical_cast prints (floating-point) numbers with too
| few precision digits. Currently, limits::digits10 + 1 is used. Paul
| wants to change it to "int significant_digits10 = int(ceil(1 +
| limits::digits * log10Two));" where "limits" is the numeric-limits
| traits class and "log10Two" is the base-10 logarithm of 2.]
| You used an article on IEEE 754 arithmetic. Shouldn't you check if the
| number type is compliant with that format? Or can this work for any
| suitable type? You also have to check if "limits::digits" is 2!
| Looking at the numbers you had, couldn't you just change
| "limits::digits10 + 1" to "limits::digits10 + 3"? It could be argued
| that there's just a bug in the definitions of "limits::digits10" (it's
| too low).
| On Saturday, August 9, 2003, at 5:48 PM, Paul A. Bristow wrote:
| [CHOP OFF really long article, with long attachments]
| 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