In C++, std::cbrt() finds the cube root of a number. It answers this question: which number multiplied by itself three times gives x? For 27 the answer is 3, and for 1000 it is 10.
Unlike a square root, a cube root exists for negative numbers. A negative number cubed is still negative, so std::cbrt(-8.0) is -2. That makes cbrt() better than pow(x, 1.0 / 3), which gives NaN for a negative x. It was added in C++11.
std::cbrt() is declared in <cmath> and is part of the group Roots and powers. These functions raise numbers to powers or take roots. Watch the domain: a negative number has no real square root, so the answer is NaN.
Syntax
Include <cmath> and call std::cbrt. Start with these lines.
C++
double a = std::cbrt(27.0); // double version
float b = std::cbrt(27.0f); // float version
Call
What it does
std::cbrt(27.0)
Passes a double and returns a double. Here the result is 3.
std::cbrt(27.0f)
Passes a float, so the float overload runs and returns a float.
std::cbrt(27)
An integer is converted to double. Returns 3 as a double.
Write std::cbrt so the C++ overloads for float, double, and long double are used.
Library declarations
C++
float cbrt(float x);
double cbrt(double x);
long double cbrt(long double x);
double cbrt(Integer x); // for integer types
Parameters
Parameter
Description
x
Required. Any number, positive, negative, or zero. An integer is converted to double.
Return Value
Return Value
Description
Cube root
The real cube root of x, with the same sign as x.
0 / inf
cbrt(0) is 0 and cbrt(inf) is inf.
NaN
Returned only when x is NaN.
C++ Examples
Example 1 — Cube Roots of a List of Numbers
Perfect cubes give whole numbers. Other numbers give decimals.
C++
#include <cmath>
#include <iostream>
int main() {
double values[] = {27.0, 8.0, 1000.0, 2.0};
for (double v : values) {
std::cout << "cbrt(" << v << ") = " << std::cbrt(v) << '\n';
}
return 0;
}
(-3) * (-3) * (-3) is -27, so the cube root of -27 is -3. The sign is kept.
The same idea gives -2 for -8. A cube root has no domain problem with negative numbers.
std::pow() with a negative base and a non-whole exponent returns NaN, so isnan() prints 1. Use cbrt() instead.
Example 3 — Side Length of a Cube from Its Volume
The volume of a cube is side × side × side. To go back from the volume to the side, take the cube root.
C++
#include <cmath>
#include <iostream>
int main() {
double volumes[] = {125.0, 64.0, 10.0};
for (double v : volumes) {
std::cout << "Volume " << v << " -> side " << std::cbrt(v) << '\n';
}
return 0;
}
Output
Volume 125 -> side 5
Volume 64 -> side 4
Volume 10 -> side 2.15443
How It Works
A cube with volume 125 has side 5, because 5 * 5 * 5 equals 125. cbrt() finds that side.
A volume of 64 gives a side of 4. Both of these are exact, so there is no decimal part.
A volume of 10 is not a perfect cube, so the side is the decimal 2.15443. The loop repeats the same steps for each volume.
Frequently Asked Questions
std::cbrt() returns the cube root of a number. The cube root of 27 is 3, because 3 times 3 times 3 is 27.
The cube root as float, double, or long double. Unlike sqrt(), it also works for negative numbers, so cbrt(-8) is -2.
cbrt() returns the exact real cube root and works for negative numbers. pow(x, 1.0 / 3.0) returns NaN for a negative x, and 1.0 / 3.0 is not exactly one third, so the result can be slightly off.
std::cbrt() was added in C++11, together with many other functions in <cmath>. Any modern compiler supports it.
Include <cmath> and write std::cbrt(). That always works and picks the C++ overloads. The global name ::cbrt often works too, but the standard does not promise it, and it may only take a double.