[erlang-questions] Where is fmod in Erlang?

Tue Oct 25 06:34:58 CEST 2016

>> How to calculate fmod in Erlang ?
> fmod(a, b) ->
>     a - trunc(a/b) * b.

This is a bit like saying that you can implement fma(x, y, z)
as x*y + z.  The point of fma() is that it does one rounding,
not two.  In the same way we expect fmod(a, b) should NOT
involve three roundings, as the code above does.  I've seen
more than one implementation of fmod() for C, and they are
not simple code.

The Erlang function above *almost* computes the same results
as C's fmod() but not quite.  The relative error in one run
was within +/- 1e-12.  (Another run which I didn't
capture got as far as 7e-12 away.)

Isn't it time the Erlang 'math' module caught up with C89?
(Or Unix V7, come to that.)

More information about the erlang-questions mailing list