Python math.cbrt() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Python 3.11+

Definition

math.cbrt(x) returns the real cube root of x as a float. Unlike square roots, every negative real number has a real cube root, so math.cbrt(-27) returns -3.0 without raising an error.

Using fractional power syntax like (-27) ** (1 / 3) returns a complex number, and math.pow(-27, 1/3) raises ValueError, making math.cbrt() cleaner and more accurate.

math.cbrt() is a function in Python's standard math module: import it with import math and call math.cbrt(...), or import the name directly with from math import cbrt. Added in Python 3.11.

Syntax

Import math and pass any real number (Python 3.11+).

python
import math

side = math.cbrt(volume)
CallWhat it does
math.cbrt(27)Returns 3.0.
math.cbrt(-8)Returns -2.0 (handles negative inputs directly).
math.cbrt(0.125)Returns 0.5.
math.cbrt(2)Returns approximately 1.2599....

Requires Python 3.11 or newer; check hasattr(math, "cbrt") if your code must run on older Python releases.

Function signatures
python
math.cbrt(x, /)
# added in Python 3.11; returns real cube root as float

Parameters

ParameterDescription
xRequired positional real number (int or float), positive, zero, or negative.

Return Value

Return ValueDescription
floatReal cube root of x. Preserves -0.0, inf, -inf, and nan.

Python Examples

Example 1 — Find Edge Lengths From Cubic Volumes

Compute edge lengths for positive and negative cubic offsets using math.cbrt() (Python 3.11+).

python
import math

volumes = [1, 27, 125, -64, 0.125]
for vol in volumes:
    edge = math.cbrt(vol)
    print(f"volume={vol} -> edge={edge}")

How It Works

  1. Exact cube volumes. Cubes of integers and binary fractions produce exact float roots.
  2. Negative cube accepted. Passing -64 returns -4.0 directly instead of a complex number.
  3. Fractional volume. One eighth gives an exact edge length of 0.5.

Example 2 — Provide a Python 3.8+ Fallback With hasattr

Use hasattr(math, "cbrt") so negative and positive cube roots work on any Python 3 release.

python
import math


def safe_cbrt(x):
    if hasattr(math, "cbrt"):
        return math.cbrt(x)
    if x < 0:
        return -((-x) ** (1.0 / 3.0))
    return float(x) ** (1.0 / 3.0)


for value in [-27, -8, 0, 64, 1000]:
    print(f"cbrt({value}) = {safe_cbrt(value):.1f}")

How It Works

  1. Feature check first. hasattr(math, "cbrt") picks the fast native function on Python 3.11 and newer.
  2. Sign-safe fallback. On older Python, taking the cube root of -x and restoring the minus sign avoids complex results.
  3. Consistent formatting. Formatting with :.1f produces identical output across Python versions even though 1.0 / 3.0 is approximate.

Example 3 — Estimate Orbital Radius From Kepler Period Ratios

Apply Kepler's third law (radius cubed equals period squared) using math.cbrt() or its sign-safe helper.

python
import math

cbrt = getattr(
    math,
    "cbrt",
    lambda x: -((-x) ** (1.0 / 3.0)) if x < 0 else float(x) ** (1.0 / 3.0),
)

for period in [1, 8, 10, 27]:
    radius = cbrt(period * period)
    print(f"T={period} -> a={radius:.4f}")

How It Works

  1. Square the period. Squaring 1, 8, 10, and 27 prepares the values before extracting the cube root.
  2. Graceful lookup. getattr selects math.cbrt when present and supplies a portable fallback otherwise.
  3. Four-decimal stability. Exact cubic squares yield 1.0000, 4.0000, and 9.0000, while 100 rounds to 4.6416.

Frequently Asked Questions

math.cbrt() was introduced in Python 3.11. Older versions raise AttributeError if you call it without a fallback.
Python treats fractional powers of negative bases as complex numbers, returning (1+1.732j) instead of the negative real root.
No. Unlike math.sqrt(), every real number including negatives has a real cube root, so math.cbrt(-8) returns -2.0.
Flip the sign for negatives before using ** (1/3): -((-x) ** (1/3)) when x < 0, and x ** (1/3) otherwise.
It always returns a float, so math.cbrt(27) evaluates to 3.0.

Next: math.ceil()

Return the ceiling of x, the smallest integer >= x.

ceil() →

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