# Puzzling type error Classic List Threaded 5 messages Open this post in threaded view
|

## Puzzling type error

 I've written the following implementation of the algorithm in this article http://www.afjarvis.staff.shef.ac.uk/maths/jarvisspec02.pdfsqRoot n scale = sqRoot' (5*n) 5   where   sqRoot' a b     | floor (logBase 10 b) >= scale = div b 10     | a >= b                        = sqRoot' (a-b) (b+10)     | otherwise                     = sqRoot' (a*100) ((100 * (div b 10)) + (mod b 10)) Since this involves whole numbers only, I was surprised by the following run-time error. *Main> sqRoot 2 5 :1:0:     Ambiguous type variable `t' in the constraints:       `RealFrac t' arising from a use of `sqRoot' at :1:0-9       `Floating t' arising from a use of `sqRoot' at :1:0-9       `Integral t' arising from a use of `sqRoot' at :1:0-9     Probable fix: add a type signature that fixes these type variable(s) What am I missing? Logesh Pillay
Open this post in threaded view
|

## Puzzling type error

 Am Sonntag, 24. August 2008 19:38 schrieb Logesh Pillay: > I've written the following implementation of the algorithm in this > article http://www.afjarvis.staff.shef.ac.uk/maths/jarvisspec02.pdf> > sqRoot n scale = sqRoot' (5*n) 5 >   where >   sqRoot' a b > >     | floor (logBase 10 b) >= scale = div b 10 >     | a >= b                        = sqRoot' (a-b) (b+10) >     | otherwise                     = sqRoot' (a*100) ((100 * (div b > > 10)) + (mod b 10)) > > Since this involves whole numbers only, I was surprised by the following > run-time error. > > *Main> sqRoot 2 5 > > :1:0: >     Ambiguous type variable `t' in the constraints: >       `RealFrac t' arising from a use of `sqRoot' at :1:0-9 >       `Floating t' arising from a use of `sqRoot' at :1:0-9 >       `Integral t' arising from a use of `sqRoot' at :1:0-9 >     Probable fix: add a type signature that fixes these type variable(s) > > What am I missing? > > Logesh Pillay > There is no automatic conversion between different numeric types, you must do that explicitly, except for numeric literals.
Open this post in threaded view
|

## Puzzling type error

 In reply to this post by Logesh Pillay-3 Logesh Pillay wrote: > Since this involves whole numbers only, I was surprised by the following > run-time error... The type of logBase is Floating a => a -> a -> a So by using b as an argument to logBase, you are forcing it to be in a type that is an instance of Floating. Use "fromIntegral b". Regards, Yitz