Home » Python

math.frexp() method with example in Python

Python math.frexp() method: Here, we are going to learn about the math.frexp() method with example in Python.
Submitted by IncludeHelp, on April 17, 2019

Python math.frexp() method

math.frexp() method is a library method of math module, it is used to get the pair of mantissa and exponent of the given number, it accepts a number (integer or float) and returns a tuple of mantissa and exponent of the given number, where mantissa is a float value and exponent is an integer value.

Where, the combination of mantissa and exponent should is like, number = mantissa*2**exponent.

Note: If anything is passed except the number, the method returns a type error, "TypeError: a float is required".

Syntax of math.frexp() method:

    math.frexp(n)

Parameter(s): a – a number (float/integer).

Return value: tuple – it returns a tuple containing the mantissa and exponent part of the given number n.

Example:

    Input:
    a = 10

    # function call
    print(math.frexp(a))

    Output:
    (0.625, 4)

Python code to demonstrate example of math.frexp() method

# Python code demonstrate example of 
# math.frexp() method
import math

# numbers
a = 0
b = 10
c = -10
d = 10.234
e = -10.234

# printing the mantissa and exponent
print("frexp(a): ", math.frexp(a))
print("frexp(b): ", math.frexp(b))
print("frexp(c): ", math.frexp(c))
print("frexp(d): ", math.frexp(d))
print("frexp(e): ", math.frexp(e))

Output

frexp(a):  (0.0, 0)
frexp(b):  (0.625, 4)
frexp(c):  (-0.625, 4)
frexp(d):  (0.639625, 4)
frexp(e):  (-0.639625, 4)


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.