[erlang-questions] Strange arithmetic behaviour

Mikael Pettersson <>
Thu May 8 11:31:22 CEST 2008


Gurgen Tumanian writes:
 > Dear all.
 > 
 > There is some kind of strange behaviour that i have noticed when dealing
 > with big float values.
 > For example:
 > 
 >  123123123123123123123.0 - 123123123123123123121.0 is 0.00000e+0  when i
 > expect 2.0 or  something like that in e notation.
 > I found this kind of errors with round() and trunc(). for example:
 > trunc(1233333333333333123333333333.12311111111) is
 > 1233333333333333065680814080.
 > or
 > round(1233333333333333123333333333.12311111111) is
 > 1233333333333333065680814080.
 > 
 > furthermore
 > 1233333333333333123333333333.12311111111 =
 > 123333333333333312367575676573.92311111111 matches
 > 
 > 
 > I have tested this on R11-B5 and on R12-B1
 > 
 > Is this a big nasty bug, or am i missing something?

You're missing that
a) Erlang floats have finite precision, per IEEE 64-bit FP standard, and
b) the Erlang reader accepts overspecified floats and maps them to its
   internal finite precision without warning when loss of precision occurs

One could argue that the reader should warn when precision loss occurs,
but that's just one of many cases where floating-point numbers may
experience precision losses, and the other cases also do not warn.



More information about the erlang-questions mailing list