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):
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:
Russ Cox: http://research.swtch.com/ftoa
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).