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.
- 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. doi: 10.1145/249069.231397. Google ScholarDigital Library
- J. T. Coonen. An implementation guide to a proposed standard for floating-point arithmetic. Computer, 13(1): 68--79, 1980. ISSN: 0018--9162. doi: 10.1109/MC.1980.1653344. Google ScholarDigital Library
- 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.Google Scholar
- D. Goldberg. What every computer scientist should know about floating-point arithmetic. ACM Computing Surveys, 23(1): 5--48, 1991. ISSN: 0360-0300. doi: 10.1145/103162.103163. Google ScholarDigital Library
- D. E. Knuth. The Art of Computer Programming, Volume II: Seminumerical Algorithms, 2nd Edition. Addison-Wesley, 1981. ISBN: 0-201-03822-6.Google Scholar
- I. T. P754. ANSI/IEEE 754-1985, Standard for Binary Floating-Point Arithmetic. IEEE, New York, Aug. 12 1985.Google Scholar
- 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. doi: 10.1145/93542.93559. Google ScholarDigital Library
Index Terms
- Printing floating-point numbers quickly and accurately with integers
Recommendations
Printing floating-point numbers: a faster, always correct method
POPL '16: Proceedings of the 43rd Annual ACM SIGPLAN-SIGACT Symposium on Principles of Programming LanguagesFloating-point numbers are an essential part of modern software, recently gaining particular prominence on the web as the exclusive numeric format of Javascript. To use floating-point numbers, we require a way to convert binary machine representations ...
Printing floating-point numbers quickly and accurately with integers
PLDI '10We 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 ...
Printing floating-point numbers quickly and accurately
This paper presents a fast and accurate algorithm for printing floating-point numbers in both free- and fixed-format modes. In free-format mode, the algorithm generates the shortest, correctly rounded output string that converts to the same number when ...
Comments