Python math.cbrt() Function
In mathematical computation,the cube rootis a very important operation. Unlike square roots, cube roots can handle negative numbers, because the cube of a negative number is still negative.
math.cbrt() is a function introduced in Python 3.11, designed specifically to compute thecube rootof a number. It returns the cube root of x, that is, the value y satisfying y^3 = x.
Word meaning: cbrt is an abbreviation of "cube root", meaning "cube root".
Basic syntax and parameters
math.cbrt() is a static function of the math module, so the math module must be imported before use.
Syntax format
import math
math.cbrt(x)
Parameter Description
- Parameters:
x - Type: numeric (integer or floating-point)
- Description: the number whose cube root is to be computed. Can be any real number, including negatives.
Return Value
- Return
xThe cube root of x, returned as a floating-point number. - If x is 0, returns 0.
- If x is negative, returns the cube root of the negative number.
Example
Let's master it thoroughly through examples math.cbrt() through a series of examples from simple to complex.
Example 1: basic usage - computing the cube root of a positive number
Example
import
math
Compute the cube roots of some common numbers
(
"Cube root of 8:"
,
math
.
cbrt
(
8
)
)
2.0, because 2^3 = 8
(
"Cube root of 27:"
,
math
.
cbrt
(
27
)
)
3.0, because 3^3 = 27
(
"Cube root of 1000:"
,
math
.
cbrt
(
1000
)
)
10.0, because 10^3 = 1000
(
"Cube root of 2:"
,
math
.
cbrt
(
2
)
)
approximately 1.2599
Cube root of 0
(
"Cube root of 0:"
,
math
.
cbrt
(
0
)
)
0.0
Result:
8 的立方根: 2.0
27 的立方根: 3.0
1000 的立方根: 10.0
2 的立方根: 1.2599210498948732
0 的立方根: 0.0
Example 2: computing the cube root of a negative number
math.cbrt() Its power lies in handling negative numbers correctly, whereas math.sqrt() raises an error for negative numbers.
Example
import
math
Compute the cube root of a negative number
(
"Cube root of -8:"
,
math
.
cbrt
(
8
)
)
-2.0, because (-2)^3 = -8
(
"Cube root of -27:"
,
math
.
cbrt
(
27
)
)
-3.0, because (-3)^3 = -27
(
"Cube root of -1:"
,
math
.
cbrt
(
1
)
)
-1.0
Comparison: sqrt cannot handle negative numbers
try
:
(
math
.
sqrt
(
8
)
)
except
ValueError
as
e:
(
"math.sqrt(-8) raises an error:"
,
e
)
Result:
-8 的立方根: -2.0
-27 的立方根: -3.0
-1 的立方根: -1.0
math.sqrt(-8) 报错: math domain error
Example 3: verifying the properties of cube roots
Use cube roots to verify the mathematical property: the cube of the cube root of a equals a itself.
Example
import
math
Verify the cube root property: cbrt(x)^3 = x
test_values
=
[
8
,
8
,
27
,
27
,
0.125
,
0.125
]
for
val
in
test_values:
cube_root
=
math
.
cbrt
(
val
)
result
=
cube_root **
3
(
f
"cbrt({val}) = {cube_root}, its cube = {result}"
)
Floating-point precision issues
(
"Precision check:"
)
(
"cbrt(5)³ ="
,
math
.
cbrt
(
5
)
**
3
)
Should equal 5
Result:
cbrt(8) = 2.0, 其立方 = 8.0
cbrt(-8) = -2.0, 其立方 = -8.0
cbrt(27) = 3.0, 其立方 = 27.0
cbrt(-27) = -3.0, 其立方 = -27.0
cbrt(0.125) = 0.5, 其立方 = 0.125
cbrt(-0.125) = -0.5, 其立方 = -0.125
Accuracy verification:
cbrt(5)³ = 5.0
Example 4: practical application - solving cubic equations
Cube roots can be used to solve cubic equations. For example, solving x^3 = 27 gives x = cbrt(27) = 3.
Example
import
math
Practical problem: finding the side length from the volume
Suppose there is a cube-shaped box with a volume of 1000 cubic meters
Find the side length
volume
=
1000
side_length
=
math
.
cbrt
(
volume
)
(
f
"A cube with volume {volume} cubic meters has side length: {side_length:.4f} meters"
)
Physics application: finding the radius of a sphere
Given the volume of a sphere V = (4/3)pi r^3, find the radius r
r = cbrt(V * 3 / (4 * π))
sphere_volume
=
904.78
Approximate sphere volume
radius
=
math
.
cbrt
(
sphere_volume *
3
/
(
4
math
.
pi
)
)
(
f
"A sphere with volume {sphere_volume:.2f} has radius approximately: {radius:.4f}"
)
Result:
体积为 1000 立方米的正方体,边长为: 10.0000 米
体积为 904.78 的球,半径约为: 6.0000
Comparison with other methods
in math.cbrt() Before it appeared, we usually used the exponentiation operator to compute cube roots:
Example
import
math
x
=
27
Method 1: use math.cbrt() (recommended)
result1
=
math
.
cbrt
(
x
)
Method 2: use exponentiation x ** (1/3)
result2
=
x **
(
1
/
3
)
Method 3: use the pow() function
result3
=
pow
(
x
,
1
/
3
)
(
f
"math.cbrt({x}) = {result1}"
)
(
f
"{x} ** (1/3) = {result2}"
)
(
f
"pow({x}, 1/3) = {result3}"
)
The case of negative numbers
y
=
8
(
f
"For negative numbers {y}:"
)
(
f
"math.cbrt({y}) = {math.cbrt(y)}"
)
(
f
"{y} ** (1/3) = {y ** (1/3)}"
)
May produce complex numbers
Result:
math.cbrt(27) = 3.0
27 ** (1/3) = 2.9999999999999996
pow(27, 1/3) = 2.9999999999999996
对于负数 -8:
math.cbrt(-8) = -2.0
-8 ** (1/3) = -(8**(1/3)) = -2.0 # 由于运算符优先级,结果正确但不够直观
Description:math.cbrt() is the best choice for computing cube roots because:
- Clear semantics and readable code
- Handles negative numbers correctly
- Higher precision, avoiding floating-point precision issues
Notes
math.cbrt()is a function available only in Python 3.11+, so older Python versions cannot use it.- For older versions, you can use
x ** (1/3)orpow(x, 1/3)instead. - This function only works with real numbers and does not support complex numbers. For complex cube roots, use
cmathmodule.
Python math Module