Converting decimal scientific notation into binary floating point is nontrivial, but this conversion can be performed with the best possible accuracy without sacrificing efficiency.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.
[1]
D. H. Bartley,et al.
Revised4 report on the algorithmic language scheme
,
1991,
LIPO.
[2]
W. H. J. Feijen,et al.
Beauty Is Our Business
,
1990,
Texts and Monographs in Computer Science.
[3]
I. Bennett Goldberg.
27 bits are not enough for 8-digit accuracy
,
1967,
CACM.
[4]
Jerome T. Coonen,et al.
Special Feature an Implementation Guide to a Proposed Standard for Floating-Point Arithmetic
,
1980,
Computer.
[5]
Ansi Ieee,et al.
IEEE Standard for Binary Floating Point Arithmetic
,
1985
.
[6]
David W. Matula,et al.
In-and-out conversions
,
1968,
CACM.
[7]
G. Hardy,et al.
An Introduction To The Theory Of Numbers Fourth Edition
,
1968
.
[8]
W. H. J. Feijen,et al.
Beauty Is Our Business: A Birthday Salute to Edsger W.Dijkstra
,
1990
.
[9]
Donald E. Knuth,et al.
A simple program whose proof isn't
,
1990
.
[10]
David Goldberg,et al.
What every computer scientist should know about floating-point arithmetic
,
1991,
CSUR.
[11]
Harold Abelson,et al.
Revised5 report on the algorithmic language scheme
,
1998,
SIGP.
[12]
Donald Ervin Knuth,et al.
The Art of Computer Programming
,
1968
.
[13]
David Gries.
Binary to decimal, one more time
,
1990
.
[14]
Guy L. Steele,et al.
How to print floating-point numbers accurately
,
1990,
SIGP.
[15]
David W. Matula,et al.
A Formalization of Floating-Point Numeric Base Conversion
,
1970,
IEEE Transactions on Computers.