http://qs1969.pair.com?node_id=11140698

Athanasius has asked for the wisdom of the Perl Monks concerning the following question:

There are times when Perl’s inbuilt exponentiation operator, **, just doesn’t cut it:

16:46 >perl -wE "my $cube_root = (-8)**(1/3); say $cube_root;" NaN 16:46 >perl -v This is perl 5, version 32, subversion 1 (v5.32.1) built for MSWin32-x +64-multi-thread

I note in the documentation that ** “...is implemented using C's pow(3) function...”, and that’s where the limitation appears to lie. (I’m using Strawberry Perl running under Windows 8.1 64-bit. I get the same results using Raku. I don’t know if C’s pow(3) function has the same limitations under Unix?)

I came up with the following, which works for my use-case but is really a kludge:

sub cube_root { my ($n) = @_; return (abs( $n ) ** (1 / 3)) * ($n < 0 ? -1 : 1); }

So, what’s the quickest/simplest/correct way to get Perl to act like the calculator app that comes with Windows, and give -2 as the cube root of -8? (I’m only interested in the real solution.) I had a quick look through CPAN, but nothing stood out. I’m probably overlooking the obvious.

Thanks,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,