"Instance of Integral Float required" error

2k Views Asked by At

The file with following function:

type Point = (Float, Float)
type Circle = (Float, Float, Float)
getCircle :: Point -> Point -> Point -> Circle
getCircle (a, b) (c, d) (e, f) = (x, y, r)
    where
        x = ((a^2+b^2)*(f-d) + (c^2+d^2)*(b-f) + (e^2+f^2)*(d-b)) `div` (a*(f-d)+c*(b-f)+e*(d-b)) `div` 2
        y = ((a^2+b^2)*(e-c) + (c^2+d^2)*(a-e) + (e^2+f^2)*(c-a)) `div` (b*(e-c)+d*(a-e)+f*(c-a)) `div` 2
        r = sqrt ((a-x)^2 + (b-y)^2)

Throws an error when I try to load it to Hugs:

ERROR "/Users/ak/Desktop/1.hs":4 - Instance of Integral Float required for definition of getCircle

What's the essence of the problem and how can it be fixed? Thanks.

2

There are 2 best solutions below

0
On BEST ANSWER

You should use / in place of div -- 5 / 2, not 5 div 2. The reason is that haskell treats integral and floating point types differently -- they are instances of different typeclasses.

(/) is declared in typeclass Fractional, whereas div is declared in typeclass Integral. Those type classes have a common ancestor Num, but they do not have any subtyping relationships apart from that.

0
On

div is integer division and thus just works on Integral instanves. Simply use /