In the previous article, we have discussed Python Program for fmod() Function
frexp() Function in Python:
The mantissa and exponent of a given number are returned as a pair by the math.frexp() method (m,e).
This method’s mathematical formula: m * (2**e)
Syntax:
math.frexp(number)
Parameters:
number: This is required. A number that is either positive or negative. If the value is not a number, TypeError is returned.
Return Value:
A pair of tuple values representing the mantissa and exponent of x. (m,e). The mantissa is returned as a float, and the exponent is returned as an integer.
The Python frexp() function returns the mantissa and exponent of x as a pair if the number argument is positive or negative (m, e).
The Python frexp() function returns TypeError if the number argument is not a number.
Tip: We can easily find the Number value by taking the opposite approach, and
The formula is: Number = m * 2**e. The first argument is m, and the second argument is e.
Examples:
Example1:
Input:
Given list = [2, 4, 6, 8, 10] Given first number = 5 Given second number = -3
Output:
The result after applying frexp() function on above given first number 5 = (0.625, 3) The result after applying frexp() function on above given second number -3 = (-0.75, 2) The result after applying frexp() function on given list element gvnlst[1] = (0.5, 3)
Example2:
Input:
Given list = [4, 6, -7, -9] Given first number = 8 Given second number = 2
Output:
The result after applying frexp() function on above given first number 8 = (0.5, 4) The result after applying frexp() function on above given second number 2 = (0.5, 2) The result after applying frexp() function on given list element gvnlst[3] = (-0.5625, 4)
Program for frexp() Function in Python
Method #1: Using Built-in Functions (Static Input)
Approach:
- Import math module using the import keyword.
- Give the list as static input and store it in a variable.
- Give the first number as static input and store it in another variable.
- Apply math.frexp() function to the given first number to get the mantissa and exponent of a given number and print it.
- Similarly, do the same for the other number.
- Apply math.frexp() function to the given list element and print it.
- The Exit of Program.
Below is the implementation:
# Import math module using the import keyword. import math # Give the list as static input and store it in a variable. gvn_lst = [2, 4, 6, 8, 10] # Give the first number as static input and store it in another variable. gvn_numb1 = 5 # Apply math.frexp() function to the given first number to get the mantissa # and exponent of a given number and print it. print("The result after applying frexp() function on above given first number", gvn_numb1, " = ", math.frexp(gvn_numb1)) # similarly do the same for the other number. gvn_numb2 = -3 print("The result after applying frexp() function on above given second number", gvn_numb2, " = ", math.frexp(gvn_numb2)) # Apply math.frexp() function to the given list element and print it. print( "The result after applying frexp() function on given list element gvnlst[1] = ", math.frexp(gvn_lst[1]))
Output:
The result after applying frexp() function on above given first number 5 = (0.625, 3) The result after applying frexp() function on above given second number -3 = (-0.75, 2) The result after applying frexp() function on given list element gvnlst[1] = (0.5, 3)
Method #2: Using Built-in Functions (User Input)
Approach:
- Import math module using the import keyword.
- Give the list as user input using list(),map(),input(),and split() functions.
- Store it in a variable.
- Give the first number as user input using the int(input()) function and store it in a variable.
- Apply math.frexp() function to the given first number to get the mantissa and exponent of a given number and print it.
- Similarly, do the same for the other number.
- Apply math.frexp() function to the given list element and print it.
- The Exit of Program.
Below is the implementation:
# Import math module using the import keyword. import math # Give the list as user input using list(),map(),input(),and split() functions. # Store it in a variable. gvn_lst = list(map(float, input( 'Enter some random List Elements separated by spaces = ').split())) # Give the first number as user input using the int(input()) function # and store it in a variable. gvn_numb1 = int(input("Enter some random number = ")) # Apply math.frexp() function to the given first number to get the mantissa # and exponent of a given number and print it. print("The result after applying frexp() function on above given first number", gvn_numb1, " = ", math.frexp(gvn_numb1)) # similarly do the same for the other number. gvn_numb2 = int(input("Enter some random number = ")) print("The result after applying frexp() function on above given second number", gvn_numb2, " = ", math.frexp(gvn_numb2)) # Apply math.frexp() function to the given list element and print it. print( "The result after applying frexp() function on given list element gvnlst[3] = ", math.frexp(gvn_lst[3]))
Output:
Enter some random List Elements separated by spaces = 4 6 -7 -9 Enter some random number = 8 The result after applying frexp() function on above given first number 8 = (0.5, 4) Enter some random number = 2 The result after applying frexp() function on above given second number 2 = (0.5, 2) The result after applying frexp() function on given list element gvnlst[3] = (-0.5625, 4)
Read all the mathematical functions available in Python and understand how to implement them in your program by using the tutorial of Python Mathematical Methods Examples.