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

I have a mathematical equation where is third root of number. I know that function sqrt 9 = 3 but which function return from number 27 number 3 what is third root ?

Thanks

Programing in Embperl and all questions are related to Embperl.
Excuse my bad English !!!
  • Comment on Which function for third root of number ?

Replies are listed 'Best First'.
Re: Which function for third root of number ?
by Roy Johnson (Monsignor) on Jul 09, 2004 at 11:34 UTC
    Use the fact that the nth root of x is x to the power of 1/n:
    27**(1/3)

    We're not really tightening our belts, it just feels that way because we're getting fatter.
Re: Which function for third root of number ?
by ysth (Canon) on Jul 09, 2004 at 11:37 UTC
    27 ** (1/3), which may cause some roundoff error.
      If you're looking for perfect cubes ....
      use POSIX qw(ceil floor); if (ceil($num ** 1/3) == floor($num ** 1/3)) { print $num . " is a cube of " $num ** 1/3; } else { print $num . " is not a perfect cube."; }
      Like all of my code the above is untested and comes with no warrenty implied or otherwise.
Re: Which function for third root of number ?
by davidj (Priest) on Jul 09, 2004 at 11:39 UTC
    Here's one way to do it (without having to use any module functions):

    #!/usr/bin/perl use strict; my $a = 27 ** (1/3); # 1/3 represents cube root print "a = $a"; exit;
    output:
    a = 3
    davidj

      Thanks to all

      Programing in Embperl and all questions are related to Embperl.
      Excuse my bad English !!!
Re: Which function for third root of number ?
by sleepingsquirrel (Chaplain) on Jul 09, 2004 at 16:55 UTC
    sub cbrt { return exp(log($_[0])/3) }


    -- All code is 100% tested and functional unless otherwise noted.