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.