|
Boost : |
From: Matthias Troyer (troyer_at_[hidden])
Date: 2002-02-05 02:06:50
On Tuesday, February 5, 2002, at 05:10 AM, Jon Wang wrote:
>
> Maybe I've made a mistake. For double, the proper precision should be
> 15 rather than 16.
>
> Mr Andy Koenig said "15 is not portable across all floating-point
> implementations. However, it is portable across all implementations
> that support IEEE floating-point arithmetic, which is most computers
> that are in common use today. If you want to do better than that, you
> might consider using numeric_limits<double>::digits10, which is the
> number of significant base-10 digits that can be accurately represented
> in a double." I really appreciate his help.
>
> So maybe we can make such improvements:
>
> #include <boost/limits.hpp>
> //...
> template<typename Target, typename Source>
> Target lexical_cast(Source arg) {
> //...
> Target result;
> interpreter.precision(std::numeric_limits<Source>::digits10);
> if( !(interpreter << arg) ||
> !(interpreter >> result) ||
> !(interpreter >> std::ws).eof())
> //...
> }
>
> And we can get the right result.
>
Actually I believe not even that is sufficient. Due to roundoff errors
the
last bit can still be incorrect. To be really accurate we might need
a non-decimal format.
Matthias
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk