This content has been marked as final.
Show 18 replies

1. Re: Third root
Peter Lorent May 3, 2006 6:45 AM (in response to Kolja1987)trace(Math.sqrt(Math.sqrt(16))); 
2. Third root
Kolja1987 May 3, 2006 6:53 AM (in response to Kolja1987)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 sqrt3(x) 
3. Re: Third root
Newsgroup_User May 3, 2006 8:20 AM (in response to Kolja1987)> 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
kglad May 3, 2006 9:15 AM (in response to Kolja1987)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
Peter Lorent May 3, 2006 10:00 AM (in response to kglad)pfff, finally understand third or cube root (not a native speaker...). So, that must be Newton's method? 
6. Re: Third root
Rothrock May 3, 2006 10:02 AM (in response to Kolja1987)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 kungfu! 
7. Re: Third root
tlpann May 3, 2006 10:14 AM (in response to Kolja1987)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
tlpann May 3, 2006 10:15 AM (in response to Kolja1987)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
kglad May 3, 2006 10:26 AM (in response to Kolja1987)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*(n1)+num/Math.pow(est,n1))/n 
10. Re: Third root
Peter Lorent May 3, 2006 11:16 AM (in response to kglad)>>So cool to see how to calculate cube roots. Thanks kglad.
Amen to that. Very cool. 
11. Re: Third root
Newsgroup_User May 3, 2006 11:48 AM (in response to Kolja1987)>> function cubeRootF(num, accuracy) {
est = num/6;
while (Math.abs(numest*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/

12. Re: Third root
Kolja1987 May 3, 2006 1:10 PM (in response to Kolja1987)Thank you very much, kglad. 
13. Re: Third root
Peter Lorent May 3, 2006 1:30 PM (in response to Kolja1987)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
kglad May 3, 2006 7:59 PM (in response to Kolja1987)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
Peter Lorent May 4, 2006 1:22 AM (in response to Kolja1987)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
kglad May 4, 2006 9:32 AM (in response to Kolja1987)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 whileloop 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. 

18. Re: Third root
Newsgroup_User May 4, 2006 3:49 PM (in response to kglad)Easiest and fastest way to get cube root is:
function cubeRoot = function(x) { return Math.pow(x,1/3); }

Jeckyl