[erlang-questions] Missing math functions

Jesper Louis Andersen jesper.louis.andersen@REDACTED
Thu Feb 1 14:32:22 CET 2018


On Thu, Feb 1, 2018 at 12:49 PM Loïc Hoguin <essen@REDACTED> wrote:

>
> And there is scalbn (also known as ldexp, apparently) which does x*2^i
> where x is a double and i is an integer. I'm not sure what it's used for
> to be honest.
>
>
ldexp is used together with its cousin, frexp. The frexp function can
"split" a floating point number in its exponent and mantissa parts. And
ldexp recombines them back. This allows you to have explicit control over a
situation where overlfow or underflow might happen: you can get at the
underlying FP representation without bitwise tricks (so that it is
portable), manipulate your number and reassemble it.

Erlang's floating point numbers are handled differently than in most
languages. Some of this is a trade-off: in Erlang, we prefer errors to
occur when you do something bad, rather than a propagation of the error.
The latter is a NaN. As a result, Erlang doesn't allow certain things:

* -0.0 is eliminated to 0.0
* No NaNs
* No Infty/-Infty. This is a pure headache in the eministat library
currently.

It does produce the need for you to map external fp numbers into a way
which Erlang accepts internally. OTOH, it does eliminate some subtle bugs
where a NaN is propagated way too long in the program and you don't know
where the error occurred in the first place.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20180201/dd62cffa/attachment.htm>


More information about the erlang-questions mailing list