ABSTRACT
We present algorithms for accurately converting floating-point numbers to decimal representation. The key idea is to carry along with the computation an explicit representation of the required rounding accuracy.
We begin with the simpler problem of converting fixed-point fractions. A modification of the well-known algorithm for radix-conversion of fixed-point fractions by multiplication explicitly determines when to terminate the conversion process; a variable number of digits are produced. The algorithm has these properties:
No information is lost; the original fraction can be recovered from the output by rounding.
No “garbage digits” are produced.
The output is correctly rounded.
It is never necessary to propagate carries on rounding.
We then derive two algorithms for free-formal output of floating-point numbers. The first simply scales the given floating-point number to an appropriate fractional range and then applies the algorithm for fractions. This is quite fast and simple to code but has inaccuracies stemming from round-off errors and oversimplification. The second algorithm guarantees mathematical accuracy by using multiple-precision integer arithmetic and handling special cases. Both algorithms produce no more digits than necessary (intuitively, the “1.3 prints as 1.2999999” problem does not occur).
Finally, we modify the free-format conversion algorithm for use in fixed-format applications. Information may be lost if the fixed format provides too few digit positions, but the output is always correctly rounded. On the other hand, no “garbage digits” are ever produced, even if the fixed format specifies too many digit positions (intuitively, the “4/3 prints as 1.333333328366279602” problem does not occur).
- ANSI76.American National Standards Institute. Draft proposed AND Fortran (BSR X3.9). Reprinted as A CM SiGPLAN Notices 11, 3 (March 1976). Google ScholarDigital Library
- Clinger90.Clinger, William D. How to read floating point numbers accurately. Proc. A CM SIGPLAN '90 Conference on Programming Language Design and Implementation (White Plains, New York, June 1990). Google ScholarDigital Library
- DEC73.Digital Equipment Corporation. DecS!lstern 10 Assembly Language Handbook. Third edition. (Maynard, Massachusetts, 1973).Google Scholar
- Dijkstra76.Dijkstra, Edsger W. A Discipline of Programming. Prentice-Hall (Englewood Cliffs, New Jersey, 1976). Google ScholarDigital Library
- Gardner77.Gardner, Martin. "The Dragon Curve and Other Problems." In Mathematical Magic Show. Knopf (New York, 1977), 203-222.Google Scholar
- Hoare78.Hoare, C. A. R. "Communicating Sequential Processes." Communications of the A CM 21, 8 (August 1978), 666-677. Google ScholarDigital Library
- IEEE81.IEEE Computer Society Standard Committee, Microprocessor Standards Subcommittee, Floating-Point Working Group. "A Proposed Standard for Binary Floating-Point Arithmetic." Computer 14, 3 (March 1981), 51-62.Google ScholarDigital Library
- IEEE85.IEEE. IEEE Standard for Binary Floating-Point Arithmetic. ANSI/iEEE Std 754-1985 (New York, 1985).Google Scholar
- Jensen74.Jensen, Kathleen, and Wirth, Niklaus. PAS- CAL User Manual and Report. Second edition. Springer- Verlag (New York, 1974). Google ScholarDigital Library
- Knuth68.Knuth, Donald E. The Art of Computer Programming, Volume 1: Fundamental Algorithms. Addison- Wesley (Reading, Massachusetts, 1968). Google ScholarDigital Library
- Knuth69.Knuth, Donald E. The Art of Computer Programming, Volume 2: seminumevical Algorithms. First edition. Addison-Wesley (Reading, Massachusetts, 1969). Google ScholarDigital Library
- Knuth74.Knuth, Donald E. "Structured Programming with GO TO Statements." Computing Surveys 6, 4 (December 1974). Google ScholarDigital Library
- Knuth81.Knuth, Donald E. The Art of Computer Programmlng, Volume 2: Seminumerical Algorithms. Second edition. Addison-Wesley (Reading, Massachusetts, 1981). Google ScholarDigital Library
- Matula68.Matula, David W. "In-and-Out Conversions." Communications of the ACM 11, 1 (January 1968), 47- 50. Google ScholarDigital Library
- Matula70.Matula, David W. "A Formalization of Floating-Point Numeric Base Conversion." IEEE Transactions on Computers C-19, 8 (August 1970), 681-692.Google ScholarDigital Library
- Moon74.Moon, David A. MacLizp Reference Manual, Revision 0. Massachusetts institute of Technology, Project MAC (Cambridge, Massachusetts, April 1974).Google Scholar
- Taranto59.Taranto, Donald. "Binary Conversion, with Fixed Decimal Precision, of a Decimal Fraction." Communicationz of the A CM 2, 7 (July 1959), 27. Google ScholarDigital Library
Index Terms
- How to print floating-point numbers accurately
Recommendations
How to read floating point numbers accurately
PLDI '90: Proceedings of the ACM SIGPLAN 1990 conference on Programming language design and implementationConsider 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 ...
How to print floating-point numbers accurately
We present algorithms for accurately converting floating-point numbers to decimal representation. The key idea is to carry along with the computation an explicit representation of the required rounding accuracy.
We begin with the simpler problem of ...
How to print floating-point numbers accurately
20 Years of the ACM SIGPLAN Conference on Programming Language Design and Implementation 1979-1999: A SelectionWe present algorithms for accurately converting floating-point numbers to decimal representation. The key idea is to carry along with the computation an explicit representation of the required rounding accuracy.We begin with the simpler problem of ...
Comments