[Haskell-beginners] rational exponents

Christopher Howard christopher.howard at frigidcode.com
Wed Sep 7 23:32:28 CEST 2011


On 09/07/2011 12:38 PM, Brandon Allbery wrote:
> On Wed, Sep 7, 2011 at 16:25, Christopher Howard
> <christopher.howard at frigidcode.com
> <mailto: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).  You may want to restrict printing
> precision.
>
> (No, this is not a bug.  No, there is no workaround that magically makes
> floating point behave the way new users think it should.  And no, this
> is absolutely *not* Haskell-specific; the same kind of question
> constantly comes up with C, C++, Perl, Java, PHP, ....)
>
> --
> brandon s allbery allbery.b at gmail.com <mailto:allbery.b at gmail.com>
> wandering unix systems administrator (available)     (412) 475-9364 vm/sms
>

Are there any alternative approaches that could taken? Perhaps, some 
kind of Floating Ratio implementation?

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? 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.

-- 
frigidcode.com
theologia.indicium.us



More information about the Beginners mailing list