C++ std::cbrt() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
<cmath>

Definition

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
CallWhat 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

ParameterDescription
xRequired. Any number, positive, negative, or zero. An integer is converted to double.

Return Value

Return ValueDescription
Cube rootThe real cube root of x, with the same sign as x.
0 / infcbrt(0) is 0 and cbrt(inf) is inf.
NaNReturned 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;
}

How It Works

  1. The loop gives each value in the array to std::cbrt().
  2. 27, 8, and 1000 are perfect cubes (3³, 2³, 10³), so the answers 3, 2, and 10 are whole.
  3. 2 is not a perfect cube, so its cube root is a decimal. cout shows six digits: 1.25992.

Example 2 — Cube Roots of Negative Numbers

Compare cbrt() with the pow(x, 1.0 / 3) trick for a negative number.

C++
#include <cmath>
#include <iostream>

int main() {
    std::cout << "cbrt(-27) = " << std::cbrt(-27.0) << '\n';
    std::cout << "cbrt(-8)  = " << std::cbrt(-8.0) << '\n';

    double wrong = std::pow(-8.0, 1.0 / 3.0);
    std::cout << "pow(-8, 1/3) is NaN: " << std::isnan(wrong) << '\n';
    return 0;
}

How It Works

  1. (-3) * (-3) * (-3) is -27, so the cube root of -27 is -3. The sign is kept.
  2. The same idea gives -2 for -8. A cube root has no domain problem with negative numbers.
  3. 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;
}

How It Works

  1. A cube with volume 125 has side 5, because 5 * 5 * 5 equals 125. cbrt() finds that side.
  2. A volume of 64 gives a side of 4. Both of these are exact, so there is no decimal part.
  3. 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.

Next: ceil()

Keep going through the <cmath> functions. Round a number up to the next whole number.

ceil() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

15 people found this page helpful