[Haskell-cafe] C's fmod in Haskell

Henning Thielemann lemming at henning-thielemann.de
Wed Sep 26 03:19:11 EDT 2007

On Wed, 26 Sep 2007, ok wrote:

> [Concerning the fact that fmod(x,y) = -fmod(-x,y)]
> I wrote:
>>> Interesting, perhaps.  Surprising, no.  fmod() is basically there for
>>> the sake of sin(), cos(), and tan() (or any other periodic and
>>> either symmetric or antisymmetric function).
> On 25 Sep 2007, at 8:58 pm, Henning Thielemann wrote:
>> Why is this particular behaviour useful in connection with trigonometric 
>> functions?
> Range reduction.
> sin(x) = sin(fmod(x, M_TWOPI)).
> Whether that is the *best* way to handle range reduction is another
> matter.

This would work with any reasonable definition of fmod, not only with the 
one, where fmod(-1, M_TWOPI) = -1. I still think that this particular 
definition is disadvantageous. Say, you want to implement a periodic 
function and you want to implement the core computation only for the 
smallest necessary range (say because of fast convergence), then with 
fmod(x, M_TWOPI) you have to add another case splitting, or you must 
implement it for the range (-M_TWOPI, M_TWOPI). If fmod(x,y) would 
generate the same sign as y, you had 0 <= fmod(x, M_TWOPI) < M_TWOPI.

More information about the Haskell-Cafe mailing list