Frexp() function in python math module

Overview:

  • Given a floating point number, the function frexp() returns the mantissa and the exponent whose base is 2.
  • Multiplying the mantissa with two raised to the power of exponent gives back the original number passed as the argument (num = m*2^e).

Example:

# Example Python program that gets the mantisa and exponent
# of a floating point number using the function math.frexp()
# The base of the exponent is 2.
import math

# Get the tuple of mantissa and exponent
me = math.frexp(6.667)
print("Mantissa:%f"%me[0])
print("Exponent:%d"%me[1])

# Using the mantissa, exponent and the base of 2
# get the input number back
num = me[0]*math.pow(2, me[1])
print("Input number:")
print(num)

Output:

Mantissa:0.833375

Exponent:3

Input number:

6.667

 


Copyright 2024 © pythontic.com