[Haskell-cafe] Fractional sqrt

Yitzchak Gale gale at sefer.org
Thu Jan 18 22:06:50 EST 2007

Lennart Augustsson wrote:
> I don't see a much better way than using something like Newton-
> Raphson and testing for some kind of convergence.  The Fractional
> class can contain many things; for instance it contains rational
> numbers.  So your mysqrt function would have to be able to cope with
> returning arbitrary precision results.  As a first step you should
> specify what mysqrt should return when it can't return the exact
> result.  For instance, what would you like mysqrt (2%1) to return?

Fractional also contains RealFloat a => Complex a. For that,
you need to sqrt the magnitude, divide the phase by 2, and
pick a branch. Which branch to pick depends on how
you are using the calculation.

Perhaps Zoltán has some other instance of Fractional
that is not in the standard libraries.

One can define an instance of Fractional for a
"numeric" approximation of a p-adic field, where
taking the square root means something like
recursively taking square roots in the field of p
elements and collecting the results.

Or for an arbitrary finite field (if you are willing to
accept an occasional _|_ for fromRational).
Or for algebraic function fields. Etc.

I really don't think there is anything that will work
in general for Fractional. You have to look at
each specific type.


More information about the Haskell-Cafe mailing list