35

I know how to obtain the square root of a number using the sqrt function.

How can I obtain the cube root of a number?

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Hava Darabi
  • 497
  • 1
  • 5
  • 10

9 Answers9

66

sqrt stands for "square root", and "square root" means raising to the power of 1/2. There is no such thing as "square root with root 2", or "square root with root 3". For other roots, you change the first word; in your case, you are seeking how to perform cube rooting.

Before C++11, there is no specific function for this, but you can go back to first principles:

  • Square root: std::pow(n, 1/2.) (or std::sqrt(n))
  • Cube root: std::pow(n, 1/3.) (or std::cbrt(n) since C++11)
  • Fourth root: std::pow(n, 1/4.)
  • etc.

If you're expecting to pass negative values for n, avoid the std::pow solution — it doesn't support negative inputs with fractional exponents, and this is why std::cbrt was added:

std::cout << std::pow(-8, 1/3.) << '\n';  // Output: -nan
std::cout << std::cbrt(-8)      << '\n';  // Output: -2

N.B. That . is really important, because otherwise 1/3 uses integer division and results in 0.

Community
  • 1
  • 1
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
8

in C++11 std::cbrt was introduced as part of math library, you may refer

triclosan
  • 5,578
  • 6
  • 26
  • 50
2
include <cmath>
std::pow(n, 1./3.)

Also, in C++11 there is cbrt in the same header.

Math for Dummies.

OrangeDog
  • 36,653
  • 12
  • 122
  • 207
0

The nth root of x is equal to x^(1/n), so use std::pow. But I don't see what this has to with operator overloading.

Sebastian Redl
  • 69,373
  • 8
  • 123
  • 157
0

Just to point this out, though we can use both ways but

 long long res = pow(1e9, 1.0/3);
 long long res2 = cbrt(1e9);
 cout<<res<<endl;
 cout<<res2<<endl;

returns

999
1000

So, in order to get the correct results with pow function we need to add an offset of 0.5 with the actual number or use a double data type i.e.

long long res = pow(1e9+0.5, 1.0/3)
double res = pow(1e9, 1.0/3)

more detailed explanation here C++ pow unusual type conversion

Saurabh Kumar
  • 437
  • 1
  • 5
  • 18
-1

Actually the round must go for the above solutions to work.

The Correct solution would be

ans = round(pow(n, 1./3.));

Sagar Kumar
  • 84
  • 1
  • 6
-1

The solution for this problem is

cube_root = pow(n,(float)1/3);

and you should #include <math.h> library file

Older standards of C/C++ don't support cbrt() function.

When we write code like cube_root = pow(n,1/3); the compiler thinks 1/3 = 0 (division problem in C/C++), so you need to do typecasting using (float)1/3 in order to get the correct answer

#include<iostream.h>
#include<conio.h>
#include<math.h>
using namespace std;

int main(){
float n = 64 , cube_root ;
clrscr();
cube_root = pow(n , (float)1/3);
cout<<"cube root = "<<cube_root<<endl;
getch();
return 0;
}

cube root = 4

  • Turbo C is not a C compiler, and Turbo C++ is not a C++ compiler. They both predate the first C standard (C89) and C++ standard (C++98) by a long time and **aren't valid C or C++ compilers**. Turbo C++ is more than a decade older than C++98 so your code **doesn't even compile in C++ compilers** (there's no iostream.h for you). Besides there's no language called C/C++, they're very different languages and both Turbo products are also different – phuclv Dec 01 '19 at 02:21
  • `cbrt` is a new function in C++11 so even compliant C++98 compilers don't support it, let alone a non-C++ compiler – phuclv Dec 01 '19 at 02:22
  • Why use `float` division and then call a `double` function? – chux - Reinstate Monica Feb 28 '21 at 01:22
-1

You can try this C algorithm :

// return a number that, when multiplied by itself twice, makes N. 
unsigned cube_root(unsigned n){
    unsigned a = 0, b;
    for (int c = sizeof(unsigned) * CHAR_BIT / 3 * 3 ; c >= 0; c -= 3) {
        a <<= 1;
        b = 3 * a * (a + 1) + 1;
        if (n >> c >= b)
            n -= b << c, ++a;
    }
    return a;
}
Michel
  • 259
  • 2
  • 3
-2

I would discourage any of the above methods as they didn't work for me. I did pow(64, 1/3.) along with pow(64, 1./3.) but the answer I got was 3
Here's my logic.

ans = pow(n, 1/3.);
if (pow(ans, 3) != n){
   ans++;
}
Pranjal
  • 500
  • 6
  • 12
  • this doesn't work. For example for the cube root of 7 this will return 2.9129... instead of 1.9129... which is off by merely just ~52.2% – phuclv Dec 01 '19 at 02:08