Missing math functions

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Missing math functions

Loïc Hoguin-3
Hello,

While reviewing some functions in SDL2 while traveling yesterday I found
two which have no equivalent in Erlang.

There is copysign which takes two float numbers and apply the second's
sign to the first.

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.

Is there a reason these are not in the math module? I can always add
functions for SDL_copysign and SDL_scalbn in the NIF but if they could
be useful in Erlang itself, all the better.

Cheers,

--
Loïc Hoguin
https://ninenines.eu
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Missing math functions

Jesper Louis Andersen-2
On Thu, Feb 1, 2018 at 12:49 PM Loïc Hoguin <[hidden email]> 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.


_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions