Convert a decimal floating-point number into a binary floating-point number Let's say I have a decimal floating-point number, i.e. a mantissa and precision (negative exponent), both represented as integers. How do I convert this into a binary floating point number (of the sort you find in most programming languages, with only the standard operations available). This should be done with maximum precision, ideally. The naive method of simply calculating $m \cdot 10^e$, where $m$ is the mantissa and $e$ the exponent, turns out to be very imprecise when $e$ is large, unfortunately.

I was also wondering how to do the reverse. I suspect the algorithm can't simply be reversed, due to the different representations.
I am not an expert in this area, but the following is what I know about this problem. This problem was solved in the following paper:


William D. Clinger. How to read floating point numbers accurately, ACM SIGPLAN Notices, 25(6) [Proceedings of the ACM SIGPLAN '90 Conference on Programming Language Design and Implementation], pages 92–101, June 1990 (A PDF, you'll find other versions online)


This is also the reference cited by Knuth in TAOCP Volume 2, Exercise 17 in 4.4 (Radix Conversion).

In a retrospective published in 2004, the author looks back and cites the following additional references:


David Gay. Correctly rounded binary-decimal and decimal-binary conversions. Technical Report 90-10, AT&amp;T Bell Laboratories, November 1990. At http://www.ampl.com/REFS/ (Direct link)


In particular, see function strtod in David Gay's dtoa.c available at http://www.netlib.org/fp/



The reverse problem (printing floating point numbers) is harder. It should probably be a separate question, but here are some references:


Steele and White, How to Print Floating-Point Numbers Accurately (also 1990, at the same conference)
Their retrospective
Burger and Dybvig, Printing Floating-Point Numbers Quickly and Accurately (1996)
Florian Loitsch, Printing Floating-Point Numbers Quickly and Accurately with
Integers
Printing Floating-Point Numbers: A Faster, Always Correct Method from POPL’16. Revised to Printing Floating-Point Numbers: An Always Correct Method (see github) (2016)


Popular expositions and other implementations:


Bryan Sullivan, Here be dragons: advances in problems you didn’t even know you had (2011)
Russ Cox, Floating Point to Decimal Conversion is Easy (2011)
Ryan Juckett(?), Printing Floating-Point Numbers (2014)
python-nicefloat
Aubrey Jaffer, Easy Accurate Reading and Writing of Floating-Point Numbers (2013)


Aside/Example: Even though this is a problem solved since 1990 (albeit with developments even last year), 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).