Is there a cube root function in python?

Home » Programming » Python » Python: Calculating a Number’s Root (Square/sqrt, Cube), With Examples

This article will show you how to calculate the square root, or any root, of numbers in the Python programming language.

We previously covered squaring and raising numbers to a power in this article.

What is a Root in Mathematics?

The root of a number x is the number that must be multiplied by itself a given number of times to equal the number x.

For example, the second root of 16 is 4 as 4*4 = 16. It is the second root because 4 must be multiplied by itself twice to reach the required result of 16.

The third root of 64 is 4 as 4*4*4 = 64. It is the third root because 4 must be multiplied by itself three times to reach the required result of 64.

The second root is usually called the square root. The third root is usually called the cube root. After that, there are fourth, fifth roots, and onwards – you can calculate any root, though it is less common.

Calculating the square root of a number is a common task, so Python has a built-in function for the job as part of the math library.

math.sqrt() Syntax

Here’s the syntax for the math.sqrt() function:

math.sqrt(n)

Note that:

  • n should be any number greater than 0
  • math.sqrt() will return the square root of the number provided

math.sqrt() Examples

Here’s the math.sqrt() function in action:

# Import the required math library 
import math 

# Calculate and print the square root of 9
print(math.sqrt(9))

Which will print the result:

3

Calculating Any Root In Python

If you want to calculate any other root number in Python, use the following syntax:

x**(1/float(n))

Where n is the root you wish to calculate, and x is the number you wish to calculate the root of.

In Python 3, it’s not necessary to coerce n to a float, so you can just use:

x**(1/n)

For example:

64**(1/3)

…to calculate the cubed root of 64.

Is there a cube root function in python?

Author

I'm Brad, and I'm nearing 20 years of experience with Linux. I've worked in just about every IT role there is before taking the leap into software development. Currently, I'm building desktop and web-based solutions with NodeJS and PHP hosted on Linux infrastructure. Visit my blog or find me on Twitter to see what I'm up to.

We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept”, you consent to the use of ALL the cookies.

In this article, you will learn how to find the cube root of a number using python programming language. This tutorial will make you understand the different approaches to finding a cube root of both positive and negative numbers.

But before jumping to the main parts, let’s clear our basics and understand the concept of cube root.

What is a cube root?

The cube root of a number is the number that would have to be multiplied by itself two times to produce the original number. For example, the cube root of 8 is 2 (8 = 2 x 2 x 2) because 2 multiplied by itself two times equals 8.

The above one is the most common and widely known definition of a cube root. But, there are different ways to find a cube root of a number, and here are some of them.

  • First, use a calculator to determine what number you want to find the cube root. This number is called the radicand. Once you have the radicand, press the cube root button on your calculator. This button is usually the radical symbol (√) with a small number 3 next to it. Enter the radicand into the calculator and press the equals button (=). Then, the answer will appear on the screen.
  • Another quickest way is to find a square root and divide it by 2. For example, to find the cube root of 64, we could take the square root of 64, which is 8, and then divide that by 2, giving us the cube root of 64, which is 4. This approach is not ideal as it is not suitable for every situation.

In addition, you should also keep in mind that the cube root of the positive number is positive, and the cube root of the negative number is negative (-27 = -3 x -3 x -3).

How to find cube root in python

There are a few different ways you can find a number’s cube root in python. The most common way is to use the ** operator and built-in pow() function. 

Over here, we will learn these two methods plus some other approaches that you can use to find cube roots in python. 

The ** operator is also known as the power operator. It is used to calculate the exponential value of a number.

The syntax for using the ** operator is as follows:

number**power

For example, to find the cube root of 8, we would use the following code:

number = 8
print("Cube root of",number,"is",number**(1/3))

'''
#Output
Cube root of 8 is 2.0
'''

This would give us the answer 2.

02.

Using the pow() function with a fractional exponent of ⅓

The built-in pow() function takes two arguments, the number to be raised to a power and the power to which the number needs to be raised. To find the cube root, you should raise the number to ⅓ power.

For example, to find the cube root of 27, you would use the following code:

number = 27
print("Cube root of",number,"is",pow(number, 1/3))

'''
#Output
Cube root of 27 is 3.0
'''

As you can see, this code returns 3.0, which is the cube root of 27.

Similarly, if you use this function to find the cube root of the negative number, then it will give the following result.

number = -27
print("Cube root of",number,"is",pow(number, 1/3))

'''
#Output
Cube root of -27 is (1.5000000000000004+2.598076211353316j)
'''

03.

Using numpy.cbrt() function

numpy.cbrt() function is also a handy way to find the cube root of the number. One of the advantages of using this module for finding the cube root is that one can provide any type of input, i.e., int, float, etc.

Apart from that, you can also find a cube root of a negative number without using the abs() method.

Here are some examples of numpy.cbrt():

Advertisements
import numpy as np

print("Cube root of 27 =", np.cbrt(27))

print("Cube root of 27, 64 and 125 =",np.cbrt([27, 64, 125]))

print("Cube root of -27 =",np.cbrt(-27))


'''
#Output
Cube root of 27 = 3.0
Cube root of 27, 64 and 125 = [3. 4. 5.]
Cube root of -27 = -3.0
'''

04.

Using scipy.special.cbrt function

The cbrt() function from scipy.special library also helps find the cube root of a number in python. To find the cube root of a number, use the scipy.special.cbrt() function as follows:

import scipy.special

number = 27
cube_root = scipy.special.cbrt(number)
print("Cube root of",number,"is",cube_root)


'''
#Output
Cube root of 27 is 3.0
'''

In addition, scipy.special.cbrt() function can also help find the cube root of a negative number. For example, to find the cube root of -27, we can do the following:

import scipy.special

number = -27
cube_root = scipy.special.cbrt(number)
print("Cube root of",number,"is",cube_root)


'''
#Output
Cube root of -27 is -3.0
'''

sympy python library is a comprehensive library for symbolic mathematics. It helps solve equations, simplify expressions, and calculate various mathematical properties.

The cbrt function in sympy takes a single argument: the number whose cube root is to be calculated. 

The cube root of a number can also be calculated using the pow function in sympy. However, cbrt is a more efficient way to calculate cube roots.

Here is a simple example:

from sympy import cbrt, sympify
print("Cube root using cbrt() is",cbrt(8))
print("Cube root is",cbrt(sympify('8')))


'''
#Output
Cube root using cbrt() is 2
Cube root is 2
'''

In the first example, the cbrt() function calculates the cube root of 8. The result is 2. In the second example, the cbrt() function is used to calculate the cube root of a string containing the value 8. 

Difference between cube root and cube number

A cube root is a number that, when multiplied by itself two times, equals the original number.

For example, the cube root of 27 is 3, because 3 x 3 x 3 = 27. The cube root of -27 is also -3, because -3 x -3 x -3 = 27.

A cube number is a number resulting from multiplying a whole number by itself two times. For example, the cube of 2 is 2 x 2 x 2 = 8. The cube of 3 is 3 x 3 x 3 = 27.

Overall, the difference between a cube root and a cube number is that a cube root is a number that, when multiplied by itself two times, equals the original number. In comparison, a cube number is a number that is the result of multiplying a whole number by itself two times.

What is the cube root of 1

The cube root of 1 is a special case; it is the only number whose cube root is itself. This is because 1 is the only number whose cube is also 1(1 = 1 x 1 x 1). All other numbers have a different cube root. For example, the cube root of 8 is 2 because 2 cubed is 8 (2 x 2 x 2).

How do you find the 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 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.

Is there a cube function in Python?

The pow() function finds the cube of a number by giving the values of i and number.

Is there a root function in Python?

sqrt() function is an inbuilt function in Python programming language that returns the square root of any number. Syntax: math.

How do you cube root in Numpy?

To find a cube root in Python, use the numpy cbrt() method. It returns the cube root of every element of the array.