float_to_list question

Kent Boortz <>
Sun Dec 31 02:25:54 CET 2000


> > I must be missing something but here is what I get from the float_to_list
> > built-in function:
> ...
> >  13> float_to_list(1.1).
> >  "1.10000000000000008882e+00"
> > 
> > Oh Oh. I was expecting:
> > "1.10000000000000000000e+00"
> 
> 1.1 and 1.2 are not representable with the standard floating point
> formats.  The strings you get are (hopefully) the decimal representations 
> of the floats closest to 1.1 and 1.2.

Yes, this is true but we should maybe do better. We rely on the C call

        sprintf(buf, "%.20e", fp)

to do the conversion. I think there are standard C libraries (glibc2?)
that implement sprintf() to give shorter string representation with
the same precision but many (most?) don't.

There is an algorithm called "Dragon4" and there may be newer algorithms
to print floats.

kent


Ref:

   "How to Print Floating-Point Numbers Accurately" by
    * Guy L. Steele, Jr. and Jon L. White [Proc. ACM SIGPLAN '90, pp. 92-101].

   http://www.kric.ac.kr:8080/pubs/articles/proceedings/pldi/93542/p112-steele/p112-steele.pdf

   http://rexx.hursley.ibm.com/decimal/decimal.htm

   http://www.codiciel.fr/netlib/ampl/solvers/dtoa.c

   Mozilla uses some code similar to this.
   http://rdf.mozilla.org/docs/refList/refNSPR/prdtoa.html

   http://puffin.external.hp.com/lxr/source/gcc/libio/floatconv.c

   SICStus Prolog has some code to print floats as short as possible.



More information about the erlang-questions mailing list