How would you find the cubed root of a number using C++. I know how to find the square root I just can't figure out how to find the cubed root.
How would you find the cubed root of a number using C++. I know how to find the square root I just can't figure out how to find the cubed root.
#include <cmath>
const double ONE_THIRD = 1.0 / 3.0;
double cuberoot(double num) {
return pow(num, ONE_THIRD);
}
I made a little calculator where I used the following code to find any root... I also wrote a power function to go along with it, but that's already included in math.h I think.
double Root(double number, double base)
{
double guess = 0;
double low = 0;
double high = number;
double counter = 0;
double change = 0;
while( counter < base )
counter++;
if( counter != base)
number = 0; // The point of this was to be sure it was an integer, I believe
if (number > 0)
{
for( guess = (low + high)/2; Power(guess, base) != number; )
{
if( Power(guess, base) > number )
high = guess;
else
low = guess;
guess = (low + high)/2;
if( high - low == change )
break;
change = high - low;
}
}
return guess;
}