 # Boost :

From: Andras Erdei (aerdei_at_[hidden])
Date: 2006-05-03 16:23:59

On 5/2/06, Paul A Bristow <pbristow_at_[hidden]> wrote:
>
> IMO The real issue here is not whether round-tripping should work, but
> that
> input from a long enough decimal digit string should always give you the
> nearest floating-point representation.
>
> For float this is true, and for double is almost (but not quite) true.

[i've only read the abstract below, not the whole article, but if you want
to pursue
your goal (always getting the nearest floating point representation), you
should
check it first -- my impression from the abstract that it is not generally
possible
(using fixed precision arithmetic). of course it might still be possible to
find an
algorithm for any given precision, but most likely that isn't trivial
either: at least
the author seems to be happy with his result of getting the closest floating
point
number about 99% of the time]

William D. Clinger
How to read floating point numbers accurately
Proceedings of the ACM SIGPLAN 1990 conference on Programming language
design and implementation

Abstract

Consider the problem of converting decimal scientific notation for a number
into the best binary floating point approximation to that number, for some
fixed precision. This problem cannot be solved using arithmetic of any fixed
precision. Hence the IEEE Standard for Binary Floating-Point Arithmetic does
not require the result of such a conversion to be the best approximation. This
paper presents an efficient algorithm that always finds the best
approximation. The algorithm uses a few extra bits of precision to compute
an IEEE-conforming approximation while testing an intermediate result to
determine whether the approximation could be other than the best. If the
approximation might not be the best, then the best approximation is
determined by a few simple operations on multiple-precision integers, where
the precision is determined by the input. When using 64 bits of precision to
compute IEEE double precision results, the algorithm avoids higher-precision
arithmetic over 99% of the time. The input problem considered by this paper
is the inverse of an output problem considered by Steele and White: Given a
binary floating point number, print a correctly rounded decimal
representation of it using the smallest number of digits that will allow the
number to be read without loss of accuracy. The Steele and White algorithm
assumes that the input problem is solved; an imperfect solution to the input
problem, as allowed by the IEEE standard and ubiquitous in current practice,
defeats the purpose of their algorithm.

br,
andras