On Fri, 13 Jul 2001, Bryan L Harris wrote:

> How do I get a cube root in perl?
>
> $cube_root = some_func("64");

Raise your number to the 1/3 power.  Be careful with negative numbers or
you will get a nan (not a number) error.  The trick is to multiply your
root by -1, take the cube root, then multiply the result by -1 again:

$n = 81;
$cr = ($n < 0) ? -1*((-1*$n) **(1/3)):$n ** (1/3);
print "$cr\n";
print $cr ** (3), "\n";

will output:

4.32674871092222
81

-- Brett
                                   http://www.chapelperilous.net/btfwk/
------------------------------------------------------------------------
Sometimes when you look into his eyes you get the feeling that someone
else is driving.
                -- David Letterman


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to