Hava Darabi
Hava Darabi

Reputation: 537

How can I obtain the cube root in C++?

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?

Upvotes: 39

Views: 85761

Answers (9)

Michel
Michel

Reputation: 349

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;
}

Upvotes: -1

Saurabh Kumar
Saurabh Kumar

Reputation: 447

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

Upvotes: 0

Akash Vichare
Akash Vichare

Reputation: 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

Upvotes: -2

Sagar Kumar
Sagar Kumar

Reputation: 92

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

The Correct solution would be

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

Upvotes: -2

Pranjal
Pranjal

Reputation: 508

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++;
}

Upvotes: -3

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385274

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.

Upvotes: 70

triclosan
triclosan

Reputation: 5724

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

Upvotes: 8

OrangeDog
OrangeDog

Reputation: 38787

include <cmath>
std::pow(n, 1./3.)

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

Math for Dummies.

Upvotes: 2

Sebastian Redl
Sebastian Redl

Reputation: 72044

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.

Upvotes: -1

Related Questions