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.


In reply to Re: How to get better exponentiation? by vr
in thread How to get better exponentiation? by Athanasius

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.