[Haskell-beginners] rational exponents

Daniel Fischer daniel.is.fischer at googlemail.com
Wed Sep 7 23:27:19 CEST 2011


On Wednesday 07 September 2011, 22:38:21, Brandon Allbery wrote:
> On Wed, Sep 7, 2011 at 16:25, Christopher Howard <
> 
> christopher.howard at frigidcode.com> wrote:
> > Hi. I'm working with simple functions involving rational exponents. I
> > noticed that the (**) function seems to do okay with negative powers,
> > but
> 
> > that something else is needed for rational exponents:
> Nothing else is needed; you're just seeing the inevitable failure mode
> of floating point math (once you get into exponents that aren't
> integers, you can't escape it).

One could have an exact

(??) :: Rational -> Rational -> Maybe Rational

so that (r % s) ?? (p  % q) = Just (n % d) if r^p == n^q && s^p == d^q, 
Nothing otherwise (if r < 0, p even, q odd, then n has to be chosen 
negative).

But when dealing with floating point numbers, you can only get the 
occasional correct result by accident.

> You may want to restrict printing
> precision.

Or round to k bits.
Or write a function

rationalPower :: Floating a => a -> Rational -> a

which calculates the power e.g. by Newton's method. That would still give 
incorrect results most of the time, but could produce exact results in 
cases where the exact result is representable and all involved numbers are 
such that no overflow occurs.





More information about the Beginners mailing list