Error message:Floating Int, RealFrac Int required

fn :: Int->(Int,Int)
fn n = (hlpDown n x , hlpUp n y)
where {x = floor(sqrt(n)) ;
y = ceiling(sqrt(n))}

Also hlpUp and hlpDown have the following type:
hlpUp :: Int->Int->Int
hlpDown :: Int->Int->Int

Im getting the following error in the second line:
ERROR-Instances of (Floating Int, RealFrac Int) required for definition of fn.
Where is the mistake?

P.S. hlpUp and hlpDown compile without errors

sqrt requires its argument to be Floating (e.g. Float or Double) but you give it n which is an Int. You can convert Int to a Floating type with the fromIntegral function (you have to manually specify the Floating type that you want to use in this case):

fn :: Int -> (Int, Int)
fn n = (hlpDown n x, hlpUp n y)
  where
    sqrtn :: Double
    sqrtn = sqrt (fromIntegral n)
    x = floor sqrtn
    y = ceiling sqrtn
1 Like

thank you that was helpful:)

I remember these error messages being confusing to me when i was learning. It seems like you should be defining instances of Floating and RealFrac for Int to resolve it, but that’s rarely the solution to missing instances! One rule of thumb: Since you didn’t define Floating/RealFrac or Int yourself then you should not write the instance as it would lead to orphans. In this case @jaror’s suggestion sounds like the best one, but the alternative is to define your own type, use that instead, and implement the instances for that.

1 Like