Stab
Stab

Reputation: 66

No instance for (Integral Double) arising from a use of ‘floor’

I'm trying to make a Haskell function that calculates x mod y:

fmod :: Double -> Double -> Double
fmod x y = x - y*m
    where m = floor a
          a = x / y

Running this in stack yields:

No instance for (Integral Double) arising from a use of `floor'

Upvotes: 2

Views: 824

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 477210

The return type of a floor :: (RealFrac a, Integral b) => a -> b is an Integral type, a Double is not an Integral type, so the m in your code can not be a Double, this is important, since the (*) :: Num a => a -> a -> a and (-) :: Num a => a -> a -> a functions return the same type as both operands.

You can however make use of fromInteger :: Num a => Integer -> a to convert an Integer to any Num type. So you can work with:

fmod :: Double -> Double -> Double
fmod x y = x - y * fromInteger (floor (x / y))

Upvotes: 5

Related Questions