[Haskell-cafe] C's fmod in Haskell

Thomas Conway drtomc at gmail.com
Sun Sep 23 06:13:58 EDT 2007


On 9/17/07, Peter Verswyvelen <bf3 at telenet.be> wrote:
>
>  Maybe this is a stupid question, but I don't find something similar in
> Haskell.
>
>  I find mod and rem, which work on integers. But I'm looking for a function
> similar to C's fmod.
>
>  Of course I can write it myself, but I guess it must already exist under a
> different name?

This one worried me for a while but, help is at hand with the std library.

In Prelude there is a function properFraction which splits a RealFrac
into its integer part and its fractional part. You can use this to
implement fmod. Assuming properFraction is efficient (which it
probably isn't), you can implement fmod really quite efficiently.

In particular, x `fmod` 1.0 == (snd . properFraction) x

cheers,
T.
-- 
Thomas Conway
drtomc at gmail.com

Silence is the perfectest herald of joy:
I were but little happy, if I could say how much.


More information about the Haskell-Cafe mailing list