[Haskell-cafe] Fractional sqrt
Novák Zoltán
neez at freemail.hu
Thu Jan 18 18:15:36 EST 2007
Hello,
I would like to use the sqrt function on Fractional numbers.
(mysqrt :: (Fractional a) => a->a)
Half of the problem is solved by:
Prelude> :t (realToFrac.sqrt)
(realToFrac.sqrt) :: (Fractional b, Real a, Floating a) => a -> b
For the other half I tried:
Prelude> :t (realToFrac.sqrt.realToFrac)
(realToFrac.sqrt.realToFrac) :: (Fractional b, Real a) => a -> b
Prelude> :t (realToFrac.sqrt.fromRational.toRational)
(realToFrac.sqrt.fromRational.toRational) :: (Fractional b, Real a) => a -> b
Prelude> :t (realToFrac.sqrt.realToFrac.fromRational.toRational)
(realToFrac.sqrt.realToFrac.fromRational.toRational) :: (Fractional b,
Real a) => a -> b
I have to admit that I do not fully understand the Haskell numerical tower...
Now I'm using the Newton method:
mysqrt :: (Fractional a) => a -> a
mysqrt x = (iterate (\y -> (x / y + y) / 2.0 ) 1.0) !!2000
But I want a faster solution. (Not by replacing 2000 with 100... :)
Zoltan
More information about the Haskell-Cafe
mailing list