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).
- Boughton, G. A., editor. Computation Structures Group Progress Report 1990-91. CSG Memo 337. MIT Laboratory for Computer Science (Cambridge, MA, June 1991).Google Scholar
- 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 Scholar
Digital Library
- 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 Scholar
Digital Library
- Darcy, Joseph D. Borneo 1.0.2: Adding IEEE 754 floating point support to Java. U. California, Berkeley, May 1998.Google Scholar
- ECMAScript Language Specification, third edition. ECMA (December 1999). Standard ECMA-262.Google Scholar
- 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 Scholar
- Gardner, Martin. Mathematical games. Scientific American 216, 3 (March 1967), 124--125; 216, 4 (April 1967), 118--120; 217, 1 (July 1967), 115.Google Scholar
- Gardner, Martin. Mathematical Magic Show. Vintage (New York, 1978), 207--209, 215--220.Google Scholar
- 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 Scholar
- 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 Scholar
- Gosling, James, Joy, Bill, and Steele, Guy. The Java Language Specification. Addison-Wesley (Reading, MA, 1996). Google Scholar
Digital Library
- 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 Scholar
Digital Library
- 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 Scholar
Digital Library
- 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 Scholar
- IEEE Standard for Binary Floating-Point Arithmetic, ANSI/IEEE Std 754--1985 edition. IEEE (New York, 1985).Google Scholar
- Standard libraries for the Haskell 98 programming language. http://www.haskell.org/definiton/haskell98-library.pdf, February 1999.Google Scholar
- 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 Scholar
Digital Library
- Knuth, Donald E. Seminumerical Algorithms. Addison-Wesley (Reading, MA, 1969).Google Scholar
- Knuth, Donald E. Seminumerical Algorithms (Second Edition). Addison-Wesley (Reading, MA, 1981).Google Scholar
- 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 Scholar
Digital Library
- Knuth, Donald E. Seminumerical Algorithms (Third Edition). Addison-Wesley (Reading, MA, 1998). Google Scholar
Digital Library
- Matula, David W. In-and-out conversions. Communications of the ACM 11, 1 (January 1968), 47--50. Google Scholar
Digital Library
- Moon, David A. MacLISP Reference Manual. MIT Project MAC (Cambridge, MA, April 1974).Google Scholar
- 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 Scholar
Digital Library
- 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 Scholar
- 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 Scholar
Cross Ref
- 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 Scholar
Digital Library
- 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 Scholar
Digital Library
- 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 Scholar
Digital Library
- Reference Guide to Symbolics-Lisp. Symbolics, Inc. (Cambridge, MA, March 1985).Google Scholar
- Taranto, Donald. Binary conversion, with fixed decimal precision, of a decimal fraction. Communications of the ACM 2, 7 (July 1959), 27. Google Scholar
Digital Library
- Weinreb, Daniel, and Moon, David. LISP Machine Manual, Third Edition. MIT Artificial Intelligence Laboratory (Cambridge, MA, March 1981). Google Scholar
Digital Library
- {ANSI76} American National Standards Institute. Draft proposed ANS Fortran (BSR X3.9). Reprinted as ACM SIGPLAN Notices 11, 3 (March 1976). Google Scholar
Digital Library
- {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 Scholar
Digital Library
- {DEC73} Digital Equipment Corporation. DecSystem 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 Scholar
Digital 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 ACM 21, 8 (August 1978), 666--677. Google Scholar
Digital Library
- {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 Scholar
- {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. PASCAL User Manual and Report. Second edition. Springer-Verlag (New York, 1974). Google Scholar
Digital Library
- {Knuth68} Knuth, Donald E. The Art of Computer Programming, Volume 1: Fundamental Algorithms. Addison-Wesley (Reading, Massachusetts, 1968). Google Scholar
Digital Library
- {Knuth69} Knuth, Donald E. The Art of Computer Programming, Volume 2: Seminumerical Algorithms. First edition. Addison-Wesley (Reading, Massachusetts, 1969). Google Scholar
Digital Library
- {Knuth74} Knuth, Donald E. "Structured Programming with GO TO Statements." Computing Surveys 6, 4 (December 1974). Google Scholar
Digital Library
- {Knuth81} Knuth, Donald E. The Art of Computer Programming, Volume 2: Seminumerical Algorithms. Second edition. Addison-Wesley (Reading, Massachusetts, 1981). Google Scholar
Digital Library
- {Matula68} Matula, David W. "In-and-Out Conversions." Communications of the ACM11, 1 (January 1968), 47--50. Google Scholar
Digital Library
- {Matula70} Matula, David W. "A Formalization of Floating-Point Numeric Base Conversion." IEEE Transactions on ComputersC-19, 8 (August 1970), 681--692.Google Scholar
- {Moon74} Moon, David A. MacLisp 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." Communications of the ACM2, 7 (July 1959), 27. Google Scholar
Digital Library
Index Terms
(auto-classified)How to print floating-point numbers accurately





Comments