Archive for April 2015
Printing floating-point numbers
Everyone knows that floating-point numbers, being discrete, cannot possibly exactly represent every real number. In particular, the usual binary (IEEE 754) floating point numbers cannot even exactly store all numbers exactly representable in decimal (e.g. 0.3 or 0.1, which are not dyadic rationals).
But what about printing them?
Just because the number stored internally is not 0.1 but the closest approximation to it (say as 0.100000001490116119384765625) doesn’t mean it should be printed as a long string, when “0.1” means exactly the same number.
This is a solved problem since 1990.
TODO: Write rest of this post.
Bryan O’Sullivan (of Real World Haskell fame):
http://www.serpentine.com/blog/2011/06/29/here-be-dragons-advances-in-problems-you-didnt-even-know-you-had/
Steel & White paper How to Print Floating-Point Numbers Accurately: https://lists.nongnu.org/archive/html/gcl-devel/2012-10/pdfkieTlklRzN.pdf
Their retrospective: http://grouper.ieee.org/groups/754/email/pdfq3pavhBfih.pdf
Burger & Dybvig:
Click to access FP-Printing-PLDI96.pdf
Florian Loitsch:
Russ Cox: http://research.swtch.com/ftoa
http://www.ryanjuckett.com/programming/printing-floating-point-numbers/
https://labix.org/python-nicefloat
http://people.csail.mit.edu/jaffer/III/EZFPRW
Edit (Thanks Soma!): Printing Floating-Point Numbers: A Faster, Always Correct Method from POPL’16. Revised to Printing Floating-Point Numbers: An Always Correct Method (see github).
Edit: An example in a programming language. Even though this is a problem solved since 1990 (albeit with developments until this year 2016), even Python until 2.6 had suboptimal printing of floating-point numbers, e.g. it would print float(‘2.2’) as ‘2.2000000000000002’. It was fixed only in Python 2.7. See https://docs.python.org/2/whatsnew/2.7.html#python-3-1-features and https://docs.python.org/3/whatsnew/2.7.html#other-language-changes (specifically https://bugs.python.org/issue7117 and discussion at https://mail.python.org/pipermail/python-dev/2009-October/092958.html).