Skip to main content
gamedev.net
🔒 Locked

3rd square root

Started by CProgrammer Mar 14, 2003 at 12:52 AM 14 replies 600+ views
Original Post
CProgrammer
CProgrammer
Whats the best way to calulate the 3rd squareroot of a number a. -CProgrammer
Zipster
Zipster
It''s just the third root, not the third square root Use pow to raise the number to a third (1.0f / 3.0f). The same goes for any root, which is just the reciprocal of the associated power, i.e squared is x2 and square root is x1 / 2.
Agentidd
Agentidd

  #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


uutee
uutee
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
Zipster
Zipster
quote:
Original post by Anonymous Poster
you can''t return if the function is void...can you?




You can return, you just can''t return a value. It''s an error if you try
CProgrammer
CProgrammer
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
Beer Hunter
Beer Hunter
Okay. Here''s a cbrt without pow:

float cbrt(float f)
{
  return exp(log(f)/3);
}


CProgrammer
CProgrammer
thanks Beer hunter thats exactly what i need.

-CProgrammer
Jesper T
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)




YoshiN
YoshiN
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...

-YoshiXGXCX ''99
Beer Hunter
Beer Hunter
quote:
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)
Yeah... good luck improving on the fpu instruction set.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.