Skip to main content
Inspiring
May 3, 2006
Question

Third root

  • May 3, 2006
  • 13 replies
  • 1080 views
This is more mathematical but scripting problem, I guess, but I know that lot of you are expert of mathematics too.

I need to calculate a third root of a number (I'm not sure if that's the phrase in English).

Function for square root is built-in (Math.sqrt()), but there are no built-in functions for larger exponent roots.

How to calculate it?
This topic has been closed for replies.

13 replies

kglad
Community Expert
Community Expert
May 4, 2006
the current estimate (est) is cubed and checked to see if the absolute value of that cube and the number whose cube root is being sought have a difference greater than accuracy. if yes, the while-loop continues to execute.

btw, if anyone is going to use this there are better ways to create a first estimate than starting with num/6.
Inspiring
May 4, 2006
Comprendo. Thanks.
Inspiring
May 4, 2006
kglad, I've had a look at Newton's method and the function cubeRootF(), tried to figure it out but failed. Could you explain one thing: how is accuracy used in the function?
kglad
Community Expert
Community Expert
May 4, 2006
you're welcome. and yes, luigiL, that's correct. (and yes, that is not a recursive formula. it's more properly called an iterative process.)
Kolja1987Author
Inspiring
May 3, 2006
Thank you very much, kglad.
Inspiring
May 3, 2006
So, would this get any root then...

function getRoot(res:Number,pow:Number):Number{
var z:Number=Math.pow(res,1/pow);
trace(Math.pow(z,pow));
return z;
}

trace(getRoot(144,5));
Inspiring
May 3, 2006
>> function cubeRootF(num, accuracy) {
est = num/6;
while (Math.abs(num-est*est*est)>accuracy) {
est = (est*2+num/(est*est))/3;
}
return est;
}

Good formula - but it's not recursive.

--
Dave -
Adobe Community Expert
www.blurredistinction.com
www.macromedia.com/support/forums/team_macromedia/


kglad
Community Expert
Community Expert
May 3, 2006
yes, i don't think flash version 1 dates back too many centuries.

the cube root recursive formula above is a special case of newton's method and in its earliest version may have been used by the babylonians thousands of years ago to calculate square roots using:

est =(est + num/est)/2.

in general, the nth root can be calculated by extending the babylonian algorithm using:

est=(est*(n-1)+num/Math.pow(est,n-1))/n
Inspiring
May 3, 2006
>>So cool to see how to calculate cube roots. Thanks kglad.
Amen to that. Very cool.
Participating Frequently
May 3, 2006
But the cube root of x is simply x^(1/3). This holds in all cases -- the yth root of x = x^(1/y).

Can't you just use that? In this form, it's exact.
Participating Frequently
May 3, 2006
But the cube root of x is simply x^(1/3). This holds in all cases -- the yth root of x = x^(1/y).

Can't you just use that? In this form, it's exact.
Inspiring
May 3, 2006
So cool to see how to calculate cube roots. Thanks kglad. I'm not sure that specific "code" has been known for hundreds of years, but I suspect the algorithm is. Just have to tease you a little bit since I envy your math kung-fu!
kglad
Community Expert
Community Expert
May 3, 2006
there are many ways to calculate cube roots. in general, for calculating nth roots (including 3rd roots), jeckylls suggestion is the easiest to employ.

for cube roots, using a simple recursive formula is faster and may be helpful if you're calculating many 3rd roots. the code below is probably the best known (for hundreds of years):

Inspiring
May 3, 2006
pfff, finally understand third or cube root (not a native speaker...). So, that must be Newton's method?