Python Program for ldexp() Function

In the previous article, we have discussed Python Program for isnan() Function
ldexp() Function in Python:

The inverse of math.frexp() is returned by the math.ldexp() method, which returns x * (2**i) of the given numbers x and i. ().

For example:

Let x= 3, i=4

Then math.ldexp(x, i) = 3*(2**4) = 48

Syntax:

math.ldexp(x, i)

Parameters:

x: This is required. A number that is either positive or negative. It returns TypeError if the value is not a number.

i: This is required. A number that is either positive or negative. It returns TypeError if the value is not a number.

Return value: It returns the value of x * (2**i)

Examples:

Example1:

Input:

Given x value = 3
Given i value = 4

Output:

The value of given  x * (2**i) = 48.0

Example2:

Input:

Given x value = 5
Given i value =  6

Output:

The value of given x * (2**i) = 320.0

Program for ldexp() 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.
  • Give the second number as static input and store it in another variable.
  • Apply math.ldexp() function to the given first and the second number to get the value of the given first number*(2**second number) from the above-given formula x * (2**i)
  • Store it in another variable.
  • Print the above result which is the result after applying math.ldexp() function.
  • Give the number as static input and store it in another variable.
  • Apply math.ldexp() function to the given list element and above-given number 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 = [1, 4, 2, 0]
# Give the first number as static input and store it in another variable.
gvn_numb1 = 3
# Give the second number as static input and store it in another variable.
gvn_numb2 = 4
# Apply math.ldexp() function to the given first and the second number to get
# the value of the given first number*(2**second number) from the above given
# formula x * (2**i)
# Store it in another variable.
rslt = math.ldexp(gvn_numb1, gvn_numb2)
# Print the above result which is the result after applying math.ldexp() function.
print(
    "The value of given first number*(2**second number) = ", rslt)
# Give the number as static input and store it in another variable.
gvn_numb3 = 2
# Apply math.ldexp() function to the given list element and above given number
# and print it.
print(
    "The value of given list element *(2** above given number ) = ", math.ldexp(
        gvn_lst[2], gvn_numb3))

Output:

The value of given first number*(2**second number) =  48.0
The value of given list element *(2** above given number ) =  8.0

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 first and second numbers respectively as user input using int(), map(), input(), and split()
    functions.
  • Store them in two separate variables.
  • Apply math.ldexp() function to the given first and the second number to get the value of the given first number*(2**second number) from the above-given formula x * (2**i)
  • Store it in another variable.
  • Print the above result which is the result after applying math.ldexp() function.
  • Give the number as user input using the int(input()) function and store it in another variable.
  • Apply math.ldexp() function to the given list element and above-given number 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(int, input(
    'Enter some random List Elements separated by spaces = ').split()))
# Give first and second numbers respectively as user input using int(),map(),input(),and split()
# functions and store them in two separate variables.
gvn_numb1, gvn_numb2 = map(int, input(
    "Enter two random numbers separated by spaces = ").split())
# Apply math.ldexp() function to the given first and the second number to get
# the value of the given first number*(2**second number) from the above given
# formula x * (2**i)
# Store it in another variable.
rslt = math.ldexp(gvn_numb1, gvn_numb2)
# Print the above result which is the result after applying math.ldexp() function.
print(
    "The value of given first number*(2**second number) = ", rslt)
# Give the number as user input using the int(input()) function and store it in another variable.
gvn_numb3 = int(input("Enter some Random Number = "))
# Apply math.ldexp() function to the given list element and above given number
# and print it.
print(
    "The value of given list element *(2** above given number ) = ", math.ldexp(
        gvn_lst[3], gvn_numb3))

Output:

Enter some random List Elements separated by spaces = 3 0 2 1
Enter two random numbers separated by spaces = 5 6
The value of given first number*(2**second number) = 320.0
Enter some Random Number = 7
The value of given list element *(2** above given number ) = 128.0

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.