skip to main content
article
Free access

How to print floating-point numbers accurately

Published: 01 June 1990 Publication History

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).

References

[1]
American National Standards Institute. Draft proposed AND Fortran (BSR X3.9). Reprinted as A CM SiGPLAN Notices 11, 3 (March 1976).
[2]
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).
[3]
Digital Equipment Corporation. DecS!lstern 10 Assembly Language Handbook. Third edition. (Maynard, Massachusetts, 1973).
[4]
Dijkstra, Edsger W. A Discipline of Programming. Prentice-Hall (Englewood Cliffs, New Jersey, 1976).
[5]
Gardner, Martin. "The Dragon Curve and Other Problems." In Mathematical Magic Show. Knopf (New York, 1977), 203-222.
[6]
Hoare, C. A. R. "Communicating Sequential Processes." Communications of the A CM 21, 8 (August 1978), 666-677.
[7]
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.
[8]
IEEE. IEEE Standard for Binary Floating-Point Arithmetic. ANSI/iEEE Std 754-1985 (New York, 1985).
[9]
Jensen, Kathleen, and Wirth, Niklaus. PAS- CAL User Manual and Report. Second edition. Springer- Verlag (New York, 1974).
[10]
Knuth, Donald E. The Art of Computer Programming, Volume 1: Fundamental Algorithms. Addison- Wesley (Reading, Massachusetts, 1968).
[11]
Knuth, Donald E. The Art of Computer Programming, Volume 2: seminumevical Algorithms. First edition. Addison-Wesley (Reading, Massachusetts, 1969).
[12]
Knuth, Donald E. "Structured Programming with GO TO Statements." Computing Surveys 6, 4 (December 1974).
[13]
Knuth, Donald E. The Art of Computer Programmlng, Volume 2: Seminumerical Algorithms. Second edition. Addison-Wesley (Reading, Massachusetts, 1981).
[14]
Matula, David W. "In-and-Out Conversions." Communications of the ACM 11, 1 (January 1968), 47- 50.
[15]
Matula, David W. "A Formalization of Floating-Point Numeric Base Conversion." IEEE Transactions on Computers C-19, 8 (August 1970), 681-692.
[16]
Moon, David A. MacLizp Reference Manual, Revision 0. Massachusetts institute of Technology, Project MAC (Cambridge, Massachusetts, April 1974).
[17]
Taranto, Donald. "Binary Conversion, with Fixed Decimal Precision, of a Decimal Fraction." Communicationz of the A CM 2, 7 (July 1959), 27.

Cited By

View all

Recommendations

Comments

Information & Contributors

Information

Published In

cover image ACM SIGPLAN Notices
ACM SIGPLAN Notices  Volume 25, Issue 6
Jun. 1990
343 pages
ISSN:0362-1340
EISSN:1558-1160
DOI:10.1145/93548
Issue’s Table of Contents
  • cover image ACM Conferences
    PLDI '90: Proceedings of the ACM SIGPLAN 1990 conference on Programming language design and implementation
    June 1990
    351 pages
    ISBN:0897913647
    DOI:10.1145/93542
Permission to make digital or hard copies of all or part of this work for personal or classroom use is granted without fee provided that copies are not made or distributed for profit or commercial advantage and that copies bear this notice and the full citation on the first page. Copyrights for components of this work owned by others than ACM must be honored. Abstracting with credit is permitted. To copy otherwise, or republish, to post on servers or to redistribute to lists, requires prior specific permission and/or a fee. Request permissions from [email protected]

Publisher

Association for Computing Machinery

New York, NY, United States

Publication History

Published: 01 June 1990
Published in SIGPLAN Volume 25, Issue 6

Check for updates

Qualifiers

  • Article

Contributors

Other Metrics

Bibliometrics & Citations

Bibliometrics

Article Metrics

  • Downloads (Last 12 months)752
  • Downloads (Last 6 weeks)84
Reflects downloads up to 09 Feb 2025

Other Metrics

Citations

Cited By

View all

View Options

View options

PDF

View or Download as a PDF file.

PDF

eReader

View online with eReader.

eReader

Login options

Figures

Tables

Media

Share

Share

Share this Publication link

Share on social media