🔒 Locked
3rd square root
Advertisement
Original Post
Whats the best way to calulate the 3rd squareroot of a number a.
-CProgrammer
#include <iostream>
#include "math.h"
using std::cout;
void main()
{
int a = 5;
cout << sqrt(sqrt(sqrt(a)));
return;
}
That would be the 3rd squarroot of a.
But... maybe you mean the "CUBE ROOT" of a number.
Remember you can raise any number by a fraction is giving the same number the nth root.
So a^(1/3) will give you the Cube root of a.
-DD
pow(x,1.0/3.0) isn''t sufficient - you can''t do pow if x is negative. It should be:
#define cbrt(x) ((x)>=0 ? pow((x),1.0/3.0)
ow(-(x),1.0/3.0))
Or if you''re using some other programming language than C, adapt from this:
void cbrt( float x )
{
if( x >= 0 )
return pow(x,1.0/3.0);
else
return pow(-x,1.0/3.0);
}
- Mikko Kauppila
#define cbrt(x) ((x)>=0 ? pow((x),1.0/3.0)
Or if you''re using some other programming language than C, adapt from this:
void cbrt( float x )
{
if( x >= 0 )
return pow(x,1.0/3.0);
else
return pow(-x,1.0/3.0);
}
- Mikko Kauppila
Pow is a good solution but say i dont have pow how would i make my own effective! 3rd root function. I made one that just finds a smaller number close to the result and a bigger one and then it just tries all the ones between according to a detail level.
Any ideas for a good algorithm
-CProgrammer
Any ideas for a good algorithm
-CProgrammer
Okay. Here''s a cbrt without pow:
float cbrt(float f)
{
return exp(log(f)/3);
}
float cbrt(float f)
{
return exp(log(f)/3);
}
thanks Beer hunter thats exactly what i need.
-CProgrammer
-CProgrammer
This should also work:
x/3 - (2*x^2)/(9*2) + (10*x^3)/(27*6) - (80*x^4)/(81*24) + ... + ((-1)^(n+1)*(2*5*8* .. * (2 + 3(n-2)))*x^n)/(n!*3^n) ..
and so on.. this should converge towards the third root of x.
Just added this since it is a way to do it using only basic functions like +, -, * and /.
You are probably better off using Beer Hunters version though since the exp() and log() functions are way better optimized than you can do with this function (unless you know machine language really well)

x/3 - (2*x^2)/(9*2) + (10*x^3)/(27*6) - (80*x^4)/(81*24) + ... + ((-1)^(n+1)*(2*5*8* .. * (2 + 3(n-2)))*x^n)/(n!*3^n) ..
and so on.. this should converge towards the third root of x.
Just added this since it is a way to do it using only basic functions like +, -, * and /.
You are probably better off using Beer Hunters version though since the exp() and log() functions are way better optimized than you can do with this function (unless you know machine language really well)

quote:
Original post by Jesper T
This should also work:
x/3 - (2*x^2)/(9*2) + (10*x^3)/(27*6) - (80*x^4)/(81*24) + ... + ((-1)^(n+1)*(2*5*8* .. * (2 + 3(n-2)))*x^n)/(n!*3^n) ..
and so on.. this should converge towards the third root of x.
Just added this since it is a way to do it using only basic functions like +, -, * and /.
You are probably better off using Beer Hunters version though since the exp() and log() functions are way better optimized than you can do with this function (unless you know machine language really well)
My question is what is the radius of convergence for that series? I''m too lazy to actually figure it out, but my guess is that it is not infinite...
quote:Yeah... good luck improving on the fpu instruction set.
Original post by Jesper T
You are probably better off using Beer Hunters version though since the exp() and log() functions are way better optimized than you can do with this function (unless you know machine language really well)
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement