math.frexp()

Anonymous contributor's avatar
Anonymous contributor
Published Nov 29, 2024
Contribute to Docs

In Python, the math.frexp() method calculates the mantissa and the exponent of a given number. The mathematical formula that is used in this method is n = m * 2^e, where n is the given number, m is the mantissa, and e is the exponent. The method returns the mantissa and the exponent in a tuple (m, e).

Syntax

math.frexp(x)
  • x: The number whose mantissa and exponent are to be calculated. If the input is not a number, the method returns a TypeError.

Example

The following example demonstrates the usage of the math.frexp() method:

import math
# Calculating the mantissa and the exponent of different numbers
print(math.frexp(10))
print(math.frexp(20))
print(math.frexp(30))

The above code produces the following output:

(0.625, 4)
(0.625, 5)
(0.9375, 5)

Codebyte Example

Run the following codebyte example to understand the use of the math.frexp() method:

Code
Output
Loading...

All contributors

Contribute to Docs

Learn Python on Codecademy