
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? Thanks, Peter

Peter Verswyvelen 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?
There's one in Data.Fixed called mod' Yes, that is an odd place for it :) Jules

Odd place it is, indeed! QUOTE: "Data.Fixed... DESCRIPTION... This module defines a Fixed file:///D:/app/ghc-6.6.1/doc/html/libraries/base/Fixed.html type for fixed-precision arithmetic...*This module also contains generalisations of div, mod, and divmod to work with any Real instance.*" Thanks a lot. I wouldn't have found that myself ;-) Jules Bean wrote:
Peter Verswyvelen 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?
There's one in Data.Fixed called mod'
Yes, that is an odd place for it :)
Jules

On 9/17/07, Peter Verswyvelen
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@gmail.com Silence is the perfectest herald of joy: I were but little happy, if I could say how much.

On Sun, 23 Sep 2007, Thomas Conway wrote:
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
Unfortunately, properFraction doesn't work well with negative numbers. I get Prelude> properFraction (-0.3) (0,-0.3) but usually I need Prelude> properFraction (-0.3) (-1,0.7) Thus I defined a work-around:
splitFraction x = let (n,f) = properFraction x in if x>=0 || f==0 then (fromInteger n, f) else (fromInteger n-1, f+1)
See also http://www.haskell.org/haskellwiki/Things_to_avoid#Forget_about_quot_and_rem

On 9/24/07, Henning Thielemann
Unfortunately, properFraction doesn't work well with negative numbers.
This old chestnut! It's a common problem in practice. As I recall, the behaviour of C's % operator allows implementations to yield either behaviour. I just checked ISO 9899:1999 which defines fmod. It specifies that the result of fmod(x,y) should have the same sign as x. Interesting. cheers, T. -- Thomas Conway drtomc@gmail.com Silence is the perfectest herald of joy: I were but little happy, if I could say how much.

On 25 Sep 2007, at 10:55 am, Thomas Conway wrote:
This old chestnut! It's a common problem in practice. As I recall, the behaviour of C's % operator allows implementations to yield either behaviour. I just checked ISO 9899:1999 which defines fmod. It specifies that the result of fmod(x,y) should have the same sign as x. Interesting.
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 Tue, 25 Sep 2007, ok wrote:
On 25 Sep 2007, at 10:55 am, Thomas Conway wrote:
This old chestnut! It's a common problem in practice. As I recall, the behaviour of C's % operator allows implementations to yield either behaviour. I just checked ISO 9899:1999 which defines fmod. It specifies that the result of fmod(x,y) should have the same sign as x. Interesting.
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).
Why is this particular behaviour useful in connection with trigonometric functions?

[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. (Amongst other things, there are modern algorithms that get a result as if the range reduction had been done with an infinitely precise pi. I have no idea how they do that.)

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.

Henning Thielemann wrote:
See also
http://www.haskell.org/haskellwiki/Things_to_avoid#Forget_about_quot_and_rem
OTOH, since quot/rem are the primitives in GHC, and div/mod are implemented in terms of them, then you might prefer to use quot/rem all other things being equal. Cheers, Simon
participants (6)
-
Henning Thielemann
-
Jules Bean
-
ok
-
Peter Verswyvelen
-
Simon Marlow
-
Thomas Conway