skip to main content
10.1145/93542.93559acmconferencesArticle/Chapter ViewAbstractPublication PagespldiConference Proceedingsconference-collections
Article
Free Access

How to print floating-point numbers accurately

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

Index Terms

  1. How to print floating-point numbers accurately

        Recommendations

        Comments

        Login options

        Check if you have access through your login credentials or your institution to get full access on this article.

        Sign in
        • Published in

          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

          Copyright © 1990 ACM

          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: 1 June 1990

          Permissions

          Request permissions about this article.

          Request Permissions

          Check for updates

          Qualifiers

          • Article

          Acceptance Rates

          Overall Acceptance Rate406of2,067submissions,20%

          Upcoming Conference

          PLDI '24

        PDF Format

        View or Download as a PDF file.

        PDF

        eReader

        View online with eReader.

        eReader