Pathologically Eclectic Rubbish Lister | |
PerlMonks |
Re: How to get better exponentiation?by syphilis (Archbishop) |
on Jan 22, 2022 at 13:43 UTC ( [id://11140710]=note: print w/replies, xml ) | Need Help?? |
I came up with the following, which works for my use-case ... Note that the floating point value 1/3 is not the same as the rational value 1/3, and while your subroutine works as you want for your use case, it won't work correctly for (eg) cube_root( -91197 ** 3 ), where it will return -91196.9999999999. For general purposes, you therefore really need an implementation that performs cbrt(x), as opposed to pow(x, 1 / 3). haukex has already shown that the excellent Math::Prime::Util::GMP module provides what you're after. My own Math::MPFR module provides the same capability (courtesy of the mpfr C library): or, the more general:
Cheers, Rob
In Section
Seekers of Perl Wisdom
|
|