Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: How to get better exponentiation?

by vr (Curate)
on Jan 22, 2022 at 18:10 UTC ( [id://11140724]=note: print w/replies, xml ) Need Help??


in reply to How to get better exponentiation?

to get Perl to act like the calculator app that comes with Windows

Perhaps Perl better not do that:

Calculator 10.2103.8.0 © 2021 Microsoft. All rights reserved -1 ^ ( 1 / 2 ) = Invalid input -1 ^ ( 2 / 4 ) = 1 -1 ^ ( 1 / 3 ) = -1 -1 ^ ( 2 / 6 ) = 1 -32 ^ ( 1 / 5 ) = -2 -32 ^ ( 2 / 10 ) = 2 -32 ^ 0.2 = 2

And so on. You see the pattern.

Math::Prime::Util::GMP

I'm not sure, maybe it's un(der)-documented (about allowed inputs) or buggy/inconsistent w.r.t. functions mentioned:

perl -MMath::Prime::Util::GMP=:all -le 'print powreal(-4, .5)' -2.000000000000000000000000000000000000000 perl -MMath::Prime::Util::GMP=:all -le "print rootreal(-4, 2)" # aborted, core dumped

I think if task is only to ever get real roots, then your "kludge" (and checking for integer parity and a sign of argument, then disallowing obvious combination) looks far-far less "kludgier" than, say, calling Math::Complex::root and then grepping list for (possibly) zero imaginary part, or something like that.

If, OTOH, what's required is my_real_pow with any (+/-) real base and any (positive?) real exponent, then solution may be to use Math::BigRat::parts on exponent, and then disallowing "negative base and even exponent denominator", while numerator parity influences sign of result if base is negative. Just an idea to explore (what about Math::BigRat result of a reciprocal, will round-trip call for our function be consistent?). Then real answer for (-8)**(2/3) would be +4 same as Wolfram Alpha. Edit 23/01: Apparently, Math::BigRat just won't help with this approach (I should have checked what it returns for most simple 2/3 argument), if exponent already happens to be floating point approximation. CPAN doesn't seem to have solutions to convert such floats to reasonable rationals. For (non-Perl) point of reference, in J 8-byte doubles, results of division such as 2%3 or (just randomly typed) 73364%294557 are converted to rationals with numerator/denominator exactly as shown.

Replies are listed 'Best First'.
Re^2: How to get better exponentiation?
by syphilis (Archbishop) on Jan 23, 2022 at 00:19 UTC
    perl -MMath::Prime::Util::GMP=:all -le 'print powreal(-4, .5)' -2.000000000000000000000000000000000000000

    I've just raised an issue about this.

    Cheers,
    Rob

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11140724]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (8)
As of 2024-03-28 23:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found