18 Replies Latest reply on May 4, 2006 3:49 PM by Newsgroup_User

# Third root

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?
• ###### 1. Re: Third root
trace(Math.sqrt(Math.sqrt(16)));
• ###### 2. Third root
Thanks, LuigiL, but you're wrong!
This would be a fourth root.
x^3 is not same as (x * x)^2
same sqrt(sqrt(x)) is not same as sqrt|3|(x)
• ###### 3. Re: Third root
> trace(Math.sqrt(Math.sqrt(16)));

No...

that is the fourth root .. not the third (cube) root.

To calculate other roots you need to use logs and exponents.
--
Jeckyl

• ###### 4. Re: Third root
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):

• ###### 5. Re: Third root
pfff, finally understand third or cube root (not a native speaker...). So, that must be Newton's method?
• ###### 6. Re: Third root
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!
• ###### 7. Re: Third root
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.
• ###### 8. Re: Third root
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.
• ###### 9. Re: Third root
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
• ###### 10. Re: Third root
>>So cool to see how to calculate cube roots. Thanks kglad.
Amen to that. Very cool.
• ###### 11. Re: Third root
>> 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 -
www.blurredistinction.com
www.macromedia.com/support/forums/team_macromedia/

• ###### 13. Re: Third root
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));
• ###### 14. Re: Third root
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.)
• ###### 15. Re: Third root
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?
• ###### 16. Re: Third root
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.
• ###### 17. Re: Third root
Comprendo. Thanks.
• ###### 18. Re: Third root
Easiest and fastest way to get cube root is:

function cubeRoot = function(x) { return Math.pow(x,1/3); }
--
Jeckyl