The cube root symbol is denoted by ‘3√’. In the case of the square root, we have used just the root symbol such as ‘√’, also called a radical.
For example, the cube root of 64, denoted as 3√64, is 4 because when we multiply 4 by itself three times, we get 4 x 4 x 4 = 64 = 43.
How to calculate cube root in Python
To find the cube root in Python, use the simple math equation: x ** [1. / 3]. It computes the [floating-point] cube root of x. It is a simple math equation that takes the cube root of x, rounds it to the nearest integer, raises it to the third power, and checks whether the result equals x.
x = 27 cr = x ** [1./3.] print[cr]
Output
3.0
To calculate the exponent number in Python, we use the **. The double star[**] is also called the power operator. To calculate the cube root, we can set the power equal to 1/3.
Find a cube root of a negative number in Python.
To find the cube root of a negative number in Python, first, use the abs[] function, and then you can use the simple math equation to calculate the cube root.
We can not find the cube root of the negative numbers the way we calculated for the above method. For example, the cube root of integer -27 should be -3, but Python returns 1.5000000000000004+2.598076211353316j.
Let’s write a complete function that will check if the input number is negative, then it will calculate accordingly.
def cuberoot[x]: if x < 0: x = abs[x] cube_root = x**[1/3]*[-1] else: cube_root = x**[1/3] return cube_root print[cuberoot[27]] print[round[cuberoot[-27]]]
Output
3.0 -3
As you can see, we need to round the result to get the cube root’s exact value.
Using Numpy cbrt[] function
To calculate a cube root in numpy, use the numpy.cbrt[] method. The np.cbrt[] function returns the cube root of every array element.
Numpy cbrt[] is a mathematical method to find the cube root of every element in a given array.
import numpy as np arr1 = [1, 8, 27, 64] arr2 = np.cbrt[arr1] print[arr2]
Output
[1. 2. 3. 4.]
The np.cbrt[] function is the easiest method to calculate a number’s cube root. Unlike the above approaches, it does not get in trouble with negative inputs and returns the exact number like 4 for input 64.
l
=
[
1
,
2
,
3
,
l
0l
1
l
2
l
3
l
4=
l
6
l
7 l
8l
9 =
0
=
1=
2=
3l
8=
3=
6
l
2
=
8
=
9[
0
Output:
[1, 8, 27, 64]
Method 2 : Using pow[] function
This is also the brute force way. In this, we use in-built pow[] function
Example:
Python3
# Initializing list
l
=
[
1
,
2
,
3
,
l
0l
1
l
2
l
3
l
4=
l
6
l
7 l
8l
9 =
0
=
1,
3,
4,
53
,
7
l
2
=
8
=
9[
0
Output:
[1, 8, 27, 64]
Method 3 : Using list comprehension
This task can also be performed using list comprehension. This is similar to above function. Just the difference is that its compact and one liner.
Example:
Python3
# Initializing list
l
=
[
1
,
2
,
3
,
l
0l
1
l
2
,
5
l
4=
[
,
4,
53
3
2l
7 l
8l
9 3
6
l
2
=
8
=
9[
0
Output:
[1, 8, 27, 64]
Method 4: Using lambda
This can also be achieved using lambda function
Example:
Python3
# Initializing list
l
=
[
1
,
2
,
3
,
l
0l
1
l
2
l
4=
l
06l
07l
08l
07l
10 l
11=
3=
3 3
l
15