Why is "Infinity" not allowed in Erlang floats?

Erlang (and by extension Elixir) supports floating point numbers.

Some possible floats:

  • 1.2345
  • 1.0e10
  • 1.0e-42

Erlang supports NaN ( nan. In Erlang) (however, I have yet to find a method that nan itself displays).

Erlang, however, does not have Infinity support. While common standards such as IEEE-754 claim to need to return Infinity when performing actions like 1.0/0.0 , Erlang throws a bad arithmetic error instead.

The same thing happens when trying to make the floats β€œtoo big”, like 1.0e400 .

There is probably some (historical?) Reason.

+8
floating-point precision erlang elixir
source share
2 answers

Erlang does all kinds of "bad" things with float (not quite IEEE 754 binary64): conflates -0 and +0 to 0, doesn't + -SNaNs, + -QNaNs, + -denormals or + -infinity.

For real scientific calculations with accurate error handling, it is probably worth switching to another language either using Port , or making RPC, or writing C / C ++ NIF.

Link: http://steve.hollasch.net/cgindex/coding/ieeefloat.html

Update: I wrote a trivial ieee754 module here that classifies float binary64 and converts those that Erlang supports.

+3
source share

Browse. / Erts / emulator / sys / unix / sys_float.c

It seems to me that Erlang uses the basic strtod implementation to convert to "standard" C floats.

On most unix systems, a floating point was not implemented for this standard at the time Erlang was created, but the values ​​that Infinity generates instead generate fpe signal errors.

+5
source share

All Articles