[Haskell-beginners] rational exponents
Daniel Fischer
daniel.is.fischer at googlemail.com
Wed Sep 7 23:44:50 CEST 2011
On Wednesday 07 September 2011, 23:32:28, Christopher Howard wrote:
> Are there any alternative approaches that could taken? Perhaps, some
> kind of Floating Ratio implementation?
That's not sanely possible. Floating demands pi and functions like log,
exp, sin... which usually have irrational values. Since Rational contains
no "closest value to the true result", a Floating instance for Rational
would be very evil.
>
> Prelude Data.Ratio> 8 ** (2 % 3)
>
> <interactive>:1:3:
> No instance for (Floating (Ratio a0))
> arising from a use of `**'
> Possible fix: add an instance declaration for (Floating (Ratio a0))
> In the expression: 8 ** (2 % 3)
> In an equation for `it': it = 8 ** (2 % 3)
>
> Or maybe an all-rational math?
That's possible to a certain extent, but in most cases powers with non-
integer exponents will again be irrational, so those couldn't be done.
> I'm mainly curious as my CASIO fx-115W
> calculator returns a result of exactly 4 if I input 8^(2/3), but I do
> not know how it arrives at the answer.
Probably it's something like
Prelude> 8 ** (2/3) :: Float
4.0
With the right number of bits in the type, the errors cancel for certain
calculations.
More information about the Beginners
mailing list