[erlang-questions] : Bug? Pretty-printing floating point values

Bob Ippolito <>
Mon Dec 10 09:40:18 CET 2007


On 11/22/07, Pierpaolo Bernardi <> wrote:
> On Nov 22, 2007 9:05 AM, Raimo Niskanen
> <> wrote:
> > I guess float_to_list/1 produces 20 digits because previously the
> > external term format for floats was that kind of printout -
> > 20 decimal digits with exponent. Nowdays it is the IEEE format.
> >
> > And I guess it was chosen so you kind of could guarantee to be able to
> > recreated exactly the right number after a roundtrip to external format.
> > Therefore significantly more than 16 digits was needed.
> >
> > I do not know if 20 digits can be proved to be always enough, though...
>
> The problem of printing and reading floating point numbers with the
> minimal number of digits
> to guarantee round trip invariance has been investigated and solved.
>
> See the papers and the public domain code already posted by Doug Currie.
>
> These algorithms are used in all serious common lisp and scheme implementations.
>
> It would be very smart for the Erlang implementors to borrow this
> public domain code.

I spent some time today writing up an Erlang implementation of Bob
Burger's "Printing Floating-Point Numbers Quickly and Accurately"
algorithm. It's in the mochiweb repository here:

http://mochiweb.googlecode.com/svn/trunk/src/mochinum.erl

It's probably not as quick as the scheme implementation because it
doesn't use a look-up table for powers of 10, but it's probably fast
enough for most uses.

-bob



More information about the erlang-questions mailing list