[Haskell-cafe] about integer and float operations

Max Rabkin max.rabkin at gmail.com
Wed Feb 4 16:26:32 EST 2009


On Wed, Feb 4, 2009 at 1:09 PM, Manlio Perillo <manlio_perillo at libero.it> wrote:
> In Haskell, something like
>
> (/) :: (Num a, Real b) => a -> a -> b

You probably want (Real a, Fractional b) => a -> a -> b. Int is an
instance of Real... Real is the class of types that can be converted
to Rational.

Then we can define
    (/.) :: (Real a1, Real a2, Fractional a) => a1 -> a2 -> a
    x /. y = fromRational $ toRational x / toRational y
whose type is more general than the one I gave above, but we can
restrict it to that by changing the signature, if you like.

> (//) :: (Num a, Integral b) => a -> a -> b

Again, Num is inappropriate, but we can define something similar:

(//) :: (Integral b, Real a, Real a1) => a -> a1 -> b
x // y = floor $ toRational x / toRational y

Hope that helps,
Max


More information about the Haskell-Cafe mailing list