[erlang-questions] Printing of floating-point numbers (was: Strange arithmetic behaviour)

Raimo Niskanen <>
Mon May 12 14:12:52 CEST 2008


On Sat, May 10, 2008 at 11:55:18PM +0100, David-Sarah Hopwood wrote:
> Gleb Peregud wrote:
> > These are results from Python interactive shell:
> > 
> >>>> 123123123123123123123.0 - 123123123123123123121.0
> > 0.0
> >>>> print '%f' % math.floor(1233333333333333123333333333.12311111111);
> > 1233333333333333065680814080.000000
> >>>> print '%f' % 1233333333333333123333333333.12311111111
> > 1233333333333333065680814080.000000
> > 
> > As you can see results are the same. I think it is because of internal
> > IEEE-something floating point number representation. As you can see in
> > the last example, number "1233333333333333123333333333.12311111111" is
> > converted to "1233333333333333065680814080.000000" without applying to
> > it any operations. Probably it is because internal float
> > representation does not allow this precision in this particular
> > situation
> 
> However, these examples show that neither Erlang nor Python are using
> "parsimonious printing" of floating-point numbers. The results under
> parsimonious printing would have the minimum number of digits of
> precision needed to reproduce the floating-point number exactly.
> There is a fair degree of concensus that this is the Right Thing for
> any programming language. See for example
> <http://drj11.wordpress.com/2007/07/03/python-poor-printing-of-floating-point/>.

Erlang/OTP is using this since R12B-2 for print operators
~p ~P ~w and ~W (functions io_lib:write/1,2 and io_lib:print/1,4).

Eshell V5.6.2  (abort with ^G)
1> io:format("~p~n", [1233333333333333065680814080.000000]).
1.233333333333333e27
ok


:
> 
> Raimo Niskanen wrote:
> > And I would not want to write the library that does e.g
> > sin, cos, tan, asin, acos, atan, pow, exp, erf
> > for a given arbitrary rounding strategy.
> > Someone else?
> 
> Google "arbitrary precision floating point". (Some of the listed libraries
> do arbitrary rounding strategies, some don't.) IMHO arbitrary-precision
> floating point belongs in the standard library of any modern programming
> language (excluding library profiles targetted to embedded systems).

Then we are just waiting for someone to write an interface
to e.g MPFR [http://www.mpfr.org]...

> 
> -- 
> David-Sarah Hopwood
> _______________________________________________
> erlang-questions mailing list
> 
> http://www.erlang.org/mailman/listinfo/erlang-questions

-- 

/ Raimo Niskanen, Erlang/OTP, Ericsson AB



More information about the erlang-questions mailing list