# Newbie "Instance Of Floating Int" Error Classic List Threaded 5 messages Open this post in threaded view
|

## Newbie "Instance Of Floating Int" Error

 Hi all, I just started working with Haskell and am having some difficulties with its type system. Here is function that is supposed to calculate the distance between two coordinates: distBetween (x1,y1) (x2,y2) = sqrt((x2-x1)^2  + (y2-y1)^2) I am trying to explictly give it a type signature. Here is what I have tried and the errors generated by Hugs: type Point = (Int,Int) distBetween :: Point -> Point -> Float >>ERROR - Type error in explicitly typed binding *** Term           : distBetween *** Type           : Point -> Point -> Int *** Does not match : Point -> Point -> Float distBetween :: Point -> Point -> Int >>Instance of Floating Int required for definition of distBetween Any help is appreciated... Deech _______________________________________________ Haskell-Cafe mailing list [hidden email] http://www.haskell.org/mailman/listinfo/haskell-cafe
Open this post in threaded view
|

## Re: Newbie "Instance Of Floating Int" Error

 One thing to try: type Point = (Float, Float) The problem is that x1, y1, x2, and y2 are Ints, and so (x2-x1)^2  + (y2-y1)^2 is also an Int, but then you try to take the square root, which is an operation only available on floating point values (more specifically, types in the class Floating, which also includes things like Complex Double). Another option is to leave the Point type as-is, but do a conversion just before applying the sqrt, say, by applying the fromIntegral function, which serves to convert from types in the class Integral, like Int and Integer to any numeric type you need. distBetween (x1,y1) (x2,y2) = sqrt . fromIntegral \$ (x2-x1)^2 + (y2-y1)^2 hope this helps,  - Cale On 27/04/06, Aditya Siram <[hidden email]> wrote: > Hi all, > I just started working with Haskell and am having some difficulties with its > type system. > Here is function that is supposed to calculate the distance between two > coordinates: > distBetween (x1,y1) (x2,y2) = sqrt((x2-x1)^2  + (y2-y1)^2) > > I am trying to explictly give it a type signature. Here is what I have tried > and the errors generated by Hugs: > > type Point = (Int,Int) > distBetween :: Point -> Point -> Float > >>ERROR - Type error in explicitly typed binding > *** Term           : distBetween > *** Type           : Point -> Point -> Int > *** Does not match : Point -> Point -> Float > > distBetween :: Point -> Point -> Int > >>Instance of Floating Int required for definition of distBetween > > Any help is appreciated... > Deech > > > _______________________________________________ > Haskell-Cafe mailing list > [hidden email] > http://www.haskell.org/mailman/listinfo/haskell-cafe> _______________________________________________ Haskell-Cafe mailing list [hidden email] http://www.haskell.org/mailman/listinfo/haskell-cafe
Open this post in threaded view
|