skip to main content
research-article

Printing floating-point numbers quickly and accurately with integers

Published: 05 June 2010 Publication History

Abstract

We present algorithms for accurately converting floating-point numbers to decimal representation. They are fast (up to 4 times faster than commonly used algorithms that use high-precision integers) and correct: any printed number will evaluate to the same number, when read again.
Our algorithms are fast, because they require only fixed-size integer arithmetic. The sole requirement for the integer type is that it has at least two more bits than the significand of the floating-point number. Hence, for IEEE 754 double-precision numbers (having a 53-bit significand) an integer type with 55 bits is sufficient. Moreover we show how to exploit additional bits to improve the generated output.
We present three algorithms with different properties: the first algorithm is the most basic one, and does not take advantage of any extra bits. It simply shows how to perform the binary-to-decimal transformation with the minimal number of bits. Our second algorithm improves on the first one by using the additional bits to produce a shorter (often the shortest) result.
Finally we propose a third version that can be used when the shortest output is a requirement. The last algorithm either produces optimal decimal representations (with respect to shortness and rounding) or rejects its input. For IEEE 754 double-precision numbers and 64-bit integers roughly 99.4% of all numbers can be processed efficiently. The remaining 0.6% are rejected and need to be printed by a slower complete algorithm.

References

[1]
R. G. Burger and R. K. Dybvig. Printing Floating-Point Numbers Quickly and Accurately. In Proceedings of the ACM SIGPLAN 1996 Conference on Programming Language Design and Implementation, PLDI 1996, pages 108--116, New York, NY, USA, June 1996. ACM.
[2]
J. T. Coonen. An implementation guide to a proposed standard for floating-point arithmetic. Computer, 13(1): 68--79, 1980. ISSN: 0018--9162.
[3]
D. M. Gay. Correctly rounded binary-decimal and decimal-binary conversions. Technical Report 90--10, AT&T Bell Laboraties, Murray Hill, NJ, USA, Nov. 1990.
[4]
D. Goldberg. What every computer scientist should know about floating-point arithmetic. ACM Computing Surveys, 23(1): 5--48, 1991. ISSN: 0360-0300.
[5]
D. E. Knuth. The Art of Computer Programming, Volume II: Seminumerical Algorithms, 2nd Edition. Addison-Wesley, 1981. ISBN: 0-201-03822-6.
[6]
I. T. P754. ANSI/IEEE 754-1985, Standard for Binary Floating-Point Arithmetic. IEEE, New York, Aug. 12 1985.
[7]
G. L. Steele Jr. and J. L. White. How to print floating-point numbers accurately. In Proceedings of the ACM SIGPLAN 1994 Conference on Programming Language Design and Implementation, PLDI 1994, pages 112--126, New York, NY, USA, 1990. ACM. ISBN: 0-89791-364-7.

Cited By

View all
  • (2017)Infra: structure all the way down: structured data as a visual programming languageProceedings of the 2017 ACM SIGPLAN International Symposium on New Ideas, New Paradigms, and Reflections on Programming and Software10.1145/3133850.3133852(180-197)Online publication date: 25-Oct-2017
  • (2015)Rethinking the human-readability infrastructureProceedings of the Workshop on Future Programming10.1145/2846656.2846657(1-6)Online publication date: 26-Oct-2015
  • (2021)Number parsing at a gigabyte per secondSoftware: Practice and Experience10.1002/spe.298451:8(1700-1727)Online publication date: 11-May-2021
  • Show More Cited By

Index Terms

  1. Printing floating-point numbers quickly and accurately with integers

      Recommendations

      Comments

      Information & Contributors

      Information

      Published In

      cover image ACM SIGPLAN Notices
      ACM SIGPLAN Notices  Volume 45, Issue 6
      PLDI '10
      June 2010
      496 pages
      ISSN:0362-1340
      EISSN:1558-1160
      DOI:10.1145/1809028
      Issue’s Table of Contents
      • cover image ACM Conferences
        PLDI '10: Proceedings of the 31st ACM SIGPLAN Conference on Programming Language Design and Implementation
        June 2010
        514 pages
        ISBN:9781450300193
        DOI:10.1145/1806596
      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: 05 June 2010
      Published in SIGPLAN Volume 45, Issue 6

      Check for updates

      Author Tags

      1. dtoa
      2. floating-point printing

      Qualifiers

      • Research-article

      Contributors

      Other Metrics

      Bibliometrics & Citations

      Bibliometrics

      Article Metrics

      • Downloads (Last 12 months)21
      • Downloads (Last 6 weeks)4
      Reflects downloads up to 17 Feb 2025

      Other Metrics

      Citations

      Cited By

      View all
      • (2017)Infra: structure all the way down: structured data as a visual programming languageProceedings of the 2017 ACM SIGPLAN International Symposium on New Ideas, New Paradigms, and Reflections on Programming and Software10.1145/3133850.3133852(180-197)Online publication date: 25-Oct-2017
      • (2015)Rethinking the human-readability infrastructureProceedings of the Workshop on Future Programming10.1145/2846656.2846657(1-6)Online publication date: 26-Oct-2015
      • (2021)Number parsing at a gigabyte per secondSoftware: Practice and Experience10.1002/spe.298451:8(1700-1727)Online publication date: 11-May-2021
      • (2019)Ryū revisited: printf floating point conversionProceedings of the ACM on Programming Languages10.1145/33605953:OOPSLA(1-23)Online publication date: 10-Oct-2019
      • (2018)Ryū: fast float-to-string conversionACM SIGPLAN Notices10.1145/3296979.319236953:4(270-282)Online publication date: 11-Jun-2018
      • (2018)Ryū: fast float-to-string conversionProceedings of the 39th ACM SIGPLAN Conference on Programming Language Design and Implementation10.1145/3192366.3192369(270-282)Online publication date: 11-Jun-2018
      • (2018)Basic Properties and AlgorithmsHandbook of Floating-Point Arithmetic10.1007/978-3-319-76526-6_4(97-162)Online publication date: 3-May-2018
      • (2016)Printing floating-point numbers: a faster, always correct methodACM SIGPLAN Notices10.1145/2914770.283765451:1(555-567)Online publication date: 11-Jan-2016
      • (2016)Printing floating-point numbers: a faster, always correct methodProceedings of the 43rd Annual ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages10.1145/2837614.2837654(555-567)Online publication date: 11-Jan-2016

      View Options

      Login options

      View options

      PDF

      View or Download as a PDF file.

      PDF

      eReader

      View online with eReader.

      eReader

      Figures

      Tables

      Media

      Share

      Share

      Share this Publication link

      Share on social media