article

How to print floating-point numbers accurately

Published:01 April 2004Publication History
Skip Abstract Section

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-format out-put 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. Boughton, G. A., editor. Computation Structures Group Progress Report 1990-91. CSG Memo 337. MIT Laboratory for Computer Science (Cambridge, MA, June 1991).Google ScholarGoogle Scholar
  2. Burger, Robert G., and Dybvig, R. Kent. Printing floating-point numbers quickly and accurately. In Proc. ACM SIGPLAN '96 Conf. Prog. Lang. Design and Implementation. ACM (Philadelphia, PA, June 1996), 108--116. Google ScholarGoogle ScholarDigital LibraryDigital Library
  3. Clinger, William D. How to read floating point numbers accurately. In Proc. ACM SIGPLAN '90 Conf. Prog. Lang. Design and Implementation. ACM (White Plains, NY, June 1990), 92--101. ACM SIGPLAN Notices25, 6 (June 1990). Google ScholarGoogle ScholarDigital LibraryDigital Library
  4. Darcy, Joseph D. Borneo 1.0.2: Adding IEEE 754 floating point support to Java. U. California, Berkeley, May 1998.Google ScholarGoogle Scholar
  5. ECMAScript Language Specification, third edition. ECMA (December 1999). Standard ECMA-262.Google ScholarGoogle Scholar
  6. Gansner, Emden R., and Reppy, John H. The Standard ML Basis Manual. Cambridge University Press (New York, 2003). Not yet published---available from October 2003.Google ScholarGoogle Scholar
  7. Gardner, Martin. Mathematical games. Scientific American 216, 3 (March 1967), 124--125; 216, 4 (April 1967), 118--120; 217, 1 (July 1967), 115.Google ScholarGoogle Scholar
  8. Gardner, Martin. Mathematical Magic Show. Vintage (New York, 1978), 207--209, 215--220.Google ScholarGoogle Scholar
  9. Gay, David M. Correctly Rounded Binary-Decimal and Decimal-Binary Conversions. Numerical Analysis Manuscript 90-10. AT&T Bell Laboratories (Murray Hill, NJ, November 1990).Google ScholarGoogle Scholar
  10. Gay, David M. Symbolic-Algebraic Computations in a Modeling Language for Mathematical Programming. Technical Report 00-3-02. Computing Sciences Research Center, Bell Laboratories (Murray Hill, NJ, July 2000).Google ScholarGoogle Scholar
  11. Gosling, James, Joy, Bill, and Steele, Guy. The Java Language Specification. Addison-Wesley (Reading, MA, 1996). Google ScholarGoogle ScholarDigital LibraryDigital Library
  12. Gries, David. Binary to decimal, one more time. In Feijen, W. H. J., van Gasteren, A. J. M., Gries, D., and Misra, J., eds., Beauty is our business: a birthday salute to Edsger W. Dijkstra. Springer-Verlag (Berlin, 1990), 141--148. Google ScholarGoogle ScholarDigital LibraryDigital Library
  13. Grosse, Eric. Real Inferno. In Boisvert, Ronald F., editor, The Quality of Numerical Software: Assessment and Enhancement: Proc. IFIP TC2/WG 2.5 Working Conf., Oxford, United Kingdom, 8--12 July 1996. Chapman Hall on behalf of IFIP (London, 1997), 270--279. Google ScholarGoogle ScholarDigital LibraryDigital Library
  14. Horning, Jim, Kalsow, Bill, McJones, Paul, and Nelson, Greg. Some Useful Modula-3 Interfaces. Memo 113. Digital Equipment Corporation Systems Research Center (Palo Alto, CA, December 1993).Google ScholarGoogle Scholar
  15. IEEE Standard for Binary Floating-Point Arithmetic, ANSI/IEEE Std 754--1985 edition. IEEE (New York, 1985).Google ScholarGoogle Scholar
  16. Standard libraries for the Haskell 98 programming language. http://www.haskell.org/definiton/haskell98-library.pdf, February 1999.Google ScholarGoogle Scholar
  17. Kelsey, Richard, Rees, Jonathan, Clinger, William, et al. The revised5 report on the algorithmic language Scheme. ACM SIGPLAN Notices 33, 9 (September 1998), 26--76. Google ScholarGoogle ScholarDigital LibraryDigital Library
  18. Knuth, Donald E. Seminumerical Algorithms. Addison-Wesley (Reading, MA, 1969).Google ScholarGoogle Scholar
  19. Knuth, Donald E. Seminumerical Algorithms (Second Edition). Addison-Wesley (Reading, MA, 1981).Google ScholarGoogle Scholar
  20. Knuth, Donald E. A simple program whose proof isn't. In Feijen, W. H. J., van Gasteren, A. J. M., Gries, D., and Misra, J., eds., Beauty is our business: a birthday salute to Edsger W. Dijkstra. Springer-Verlag (Berlin, 1990), 233--242. Google ScholarGoogle ScholarDigital LibraryDigital Library
  21. Knuth, Donald E. Seminumerical Algorithms (Third Edition). Addison-Wesley (Reading, MA, 1998). Google ScholarGoogle ScholarDigital LibraryDigital Library
  22. Matula, David W. In-and-out conversions. Communications of the ACM 11, 1 (January 1968), 47--50. Google ScholarGoogle ScholarDigital LibraryDigital Library
  23. Moon, David A. MacLISP Reference Manual. MIT Project MAC (Cambridge, MA, April 1974).Google ScholarGoogle Scholar
  24. Rees, Jonathan, Clinger, William, et al. The revised4 report on the algorithmic language Scheme. ACM SIGPLAN Lisp Pointers 4, 3 (July---September 1991), 1--55. Google ScholarGoogle ScholarDigital LibraryDigital Library
  25. Reppy, John H., et al. http://cm.bell-labs.com/cm/cs/what/smlnj/doc/basis/pages/real.html, October 1997. To be published as {6}.Google ScholarGoogle Scholar
  26. Schulte, M. J., Zelov, V. A., Akkas, A., and Burley, J. C. The interval-enhanced GNU Fortran compiler. In Csendes, Tibor, ed., Developments in Reliable Computing. Kluwer (Dordrecht, Netherlands, 1999), 311--322.Google ScholarGoogle ScholarCross RefCross Ref
  27. Steele, Jr., Guy L., and Gabriel, Richard P. The evolution of Lisp, pages 233--330. In History of Programming Languages. ACM Press (New York, 1996), 233--330. Google ScholarGoogle ScholarDigital LibraryDigital Library
  28. Steele, Jr., Guy L., and White, Jon L. How to print floating-point numbers accurately. In Proc. ACM SIGPLAN '90 Conf. Prog. Lang. Design and Implementation. ACM (White Plains, NY, June 1990), 112--126. ACM SIGPLAN Notices25, 6 (June 1990). Google ScholarGoogle ScholarDigital LibraryDigital Library
  29. Steele, Guy L., Jr., Fahlman, Scott E., Gabriel, Richard P., Moon, David A., and Weinreb, Daniel L. Common Lisp: The Language. Digital Press (Burlington, MA, 1984). Google ScholarGoogle ScholarDigital LibraryDigital Library
  30. Reference Guide to Symbolics-Lisp. Symbolics, Inc. (Cambridge, MA, March 1985).Google ScholarGoogle Scholar
  31. Taranto, Donald. Binary conversion, with fixed decimal precision, of a decimal fraction. Communications of the ACM 2, 7 (July 1959), 27. Google ScholarGoogle ScholarDigital LibraryDigital Library
  32. Weinreb, Daniel, and Moon, David. LISP Machine Manual, Third Edition. MIT Artificial Intelligence Laboratory (Cambridge, MA, March 1981). Google ScholarGoogle ScholarDigital LibraryDigital Library
  33. {ANSI76} American National Standards Institute. Draft proposed ANS Fortran (BSR X3.9). Reprinted as ACM SIGPLAN Notices 11, 3 (March 1976). Google ScholarGoogle ScholarDigital LibraryDigital Library
  34. {Clinger90} Clinger, William D. How to read floating point numbers accurately. Proc. ACM SIGPLAN '90 Conference on Programming Language Design and Implementation (White Plains, New York, June 1990). Google ScholarGoogle ScholarDigital LibraryDigital Library
  35. {DEC73} Digital Equipment Corporation. DecSystem 10 Assembly Language Handbook. Third edition. (Maynard, Massachusetts, 1973).Google ScholarGoogle Scholar
  36. {Dijkstra76} Dijkstra, Edsger W. A Discipline of Programming. Prentice-Hall (Englewood Cliffs, New Jersey, 1976). Google ScholarGoogle ScholarDigital LibraryDigital Library
  37. {Gardner77} Gardner, Martin. "The Dragon Curve and Other Problems." In Mathematical Magic Show. Knopf (New York, 1977), 203--222.Google ScholarGoogle Scholar
  38. {Hoare78} Hoare, C. A. R. "Communicating Sequential Processes." Communications of the ACM 21, 8 (August 1978), 666--677. Google ScholarGoogle ScholarDigital LibraryDigital Library
  39. {IEEE81} IEEE Computer Society Standard Committee, Microprocessor Standards Subcommittee, Floating-Point Working Group. "A Proposed Standard for Binary Floating-Point Arithmetic." Computer14, 3 (March 1981), 51--62.Google ScholarGoogle Scholar
  40. {IEEE85} IEEE. IEEE Standard for Binary Floating-Point Arithmetic. ANSI/IEEE Std 754--1985 (New York, 1985).Google ScholarGoogle Scholar
  41. {Jensen74} Jensen, Kathleen, and Wirth, Niklaus. PASCAL User Manual and Report. Second edition. Springer-Verlag (New York, 1974). Google ScholarGoogle ScholarDigital LibraryDigital Library
  42. {Knuth68} Knuth, Donald E. The Art of Computer Programming, Volume 1: Fundamental Algorithms. Addison-Wesley (Reading, Massachusetts, 1968). Google ScholarGoogle ScholarDigital LibraryDigital Library
  43. {Knuth69} Knuth, Donald E. The Art of Computer Programming, Volume 2: Seminumerical Algorithms. First edition. Addison-Wesley (Reading, Massachusetts, 1969). Google ScholarGoogle ScholarDigital LibraryDigital Library
  44. {Knuth74} Knuth, Donald E. "Structured Programming with GO TO Statements." Computing Surveys 6, 4 (December 1974). Google ScholarGoogle ScholarDigital LibraryDigital Library
  45. {Knuth81} Knuth, Donald E. The Art of Computer Programming, Volume 2: Seminumerical Algorithms. Second edition. Addison-Wesley (Reading, Massachusetts, 1981). Google ScholarGoogle ScholarDigital LibraryDigital Library
  46. {Matula68} Matula, David W. "In-and-Out Conversions." Communications of the ACM11, 1 (January 1968), 47--50. Google ScholarGoogle ScholarDigital LibraryDigital Library
  47. {Matula70} Matula, David W. "A Formalization of Floating-Point Numeric Base Conversion." IEEE Transactions on ComputersC-19, 8 (August 1970), 681--692.Google ScholarGoogle Scholar
  48. {Moon74} Moon, David A. MacLisp Reference Manual, Revision 0. Massachusetts Institute of Technology, Project MAC (Cambridge, Massachusetts, April 1974).Google ScholarGoogle Scholar
  49. {Taranto59} Taranto, Donald. "Binary Conversion, with Fixed Decimal Precision, of a Decimal Fraction." Communications of the ACM2, 7 (July 1959), 27. Google ScholarGoogle ScholarDigital LibraryDigital Library

Index Terms

(auto-classified)
  1. How to print floating-point numbers accurately

    Comments

    Login options

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

    Sign in

    Full Access

    • Published in

      cover image ACM SIGPLAN Notices
      ACM SIGPLAN Notices  Volume 39, Issue 4
      20 Years of the ACM SIGPLAN Conference on Programming Language Design and Implementation 1979-1999: A Selection
      April 2004
      673 pages
      ISSN:0362-1340
      EISSN:1558-1160
      DOI:10.1145/989393
      Issue’s Table of Contents

      Copyright © 2004 Authors

      Publisher

      Association for Computing Machinery

      New York, NY, United States

      Publication History

      • Published: 1 April 2004

      Check for updates

      Qualifiers

      • article

    PDF Format

    View or Download as a PDF file.

    PDF

    eReader

    View online with eReader.

    eReader
    About Cookies On This Site

    We use cookies to ensure that we give you the best experience on our website.

    Learn more

    Got it!