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

David-Sarah Hopwood <>
Sun May 11 00:55:18 CEST 2008


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/>.

> On 5/8/08, Gurgen Tumanian <> wrote:
>> Dear all.
>>
>> There is some kind of strange behaviour that i have noticed when dealing
>> with big float values. [...]
>> furthermore
>> 1233333333333333123333333333.12311111111 =
>> 123333333333333312367575676573.92311111111 matches

There is probably a typo here; these do not match.

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).

-- 
David-Sarah Hopwood



More information about the erlang-questions mailing list