{"id":24196,"date":"2021-10-14T09:17:58","date_gmt":"2021-10-14T03:47:58","guid":{"rendered":"https:\/\/python-programs.com\/?p=24196"},"modified":"2021-11-05T20:55:53","modified_gmt":"2021-11-05T15:25:53","slug":"python-program-for-ldexp-function","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-for-ldexp-function\/","title":{"rendered":"Python Program for ldexp() Function"},"content":{"rendered":"
In the previous article, we have discussed Python Program for isnan() Function<\/a> The inverse of math.frexp() is returned by the math.ldexp() method, which returns x * (2**i)<\/strong> of the given numbers x and i. ().<\/p>\n For example:<\/p>\n Let x= 3, i=4<\/p>\n Then math.ldexp(x, i) = 3*(2**4) = 48<\/p>\n Syntax:<\/strong><\/p>\n Parameters:<\/strong><\/p>\n x: This is required. A number that is either positive or negative. It returns TypeError if the value is not a number.<\/p>\n i: This is required. A number that is either positive or negative. It returns TypeError if the value is not a number.<\/p>\n Return value: <\/strong>It returns the value of x * (2**i)<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n 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<\/a>.<\/p>\n 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) …<\/p>\n
\nldexp() Function in Python:<\/strong><\/p>\nmath.ldexp(x, i)<\/pre>\n
Given x value = 3\r\nGiven i value = 4<\/pre>\n
The value of given x * (2**i) = 48.0<\/pre>\n
Given x value = 5\r\nGiven i value = 6<\/pre>\n
The value of given x * (2**i) = 320.0<\/pre>\n
Program for ldexp() Function in Python<\/h2>\n
\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
\n
# Import math module using the import keyword.\r\nimport math\r\n# Give the list as static input and store it in a variable.\r\ngvn_lst = [1, 4, 2, 0]\r\n# Give the first number as static input and store it in another variable.\r\ngvn_numb1 = 3\r\n# Give the second number as static input and store it in another variable.\r\ngvn_numb2 = 4\r\n# Apply math.ldexp() function to the given first and the second number to get\r\n# the value of the given first number*(2**second number) from the above given\r\n# formula x * (2**i)\r\n# Store it in another variable.\r\nrslt = math.ldexp(gvn_numb1, gvn_numb2)\r\n# Print the above result which is the result after applying math.ldexp() function.\r\nprint(\r\n \"The value of given first number*(2**second number) = \", rslt)\r\n# Give the number as static input and store it in another variable.\r\ngvn_numb3 = 2\r\n# Apply math.ldexp() function to the given list element and above given number\r\n# and print it.\r\nprint(\r\n \"The value of given list element *(2** above given number ) = \", math.ldexp(\r\n gvn_lst[2], gvn_numb3))\r\n<\/pre>\n
The value of given first number*(2**second number) = 48.0\r\nThe value of given list element *(2** above given number ) = 8.0<\/pre>\n
Method #2: Using Built-in Functions (User Input)<\/h3>\n
\n
\nfunctions.<\/li>\n# Import math module using the import keyword.\r\nimport math\r\n# Give the list as user input using list(),map(),input(),and split() functions.\r\n# Store it in a variable.\r\ngvn_lst = list(map(int, input(\r\n 'Enter some random List Elements separated by spaces = ').split()))\r\n# Give first and second numbers respectively as user input using int(),map(),input(),and split()\r\n# functions and store them in two separate variables.\r\ngvn_numb1, gvn_numb2 = map(int, input(\r\n \"Enter two random numbers separated by spaces = \").split())\r\n# Apply math.ldexp() function to the given first and the second number to get\r\n# the value of the given first number*(2**second number) from the above given\r\n# formula x * (2**i)\r\n# Store it in another variable.\r\nrslt = math.ldexp(gvn_numb1, gvn_numb2)\r\n# Print the above result which is the result after applying math.ldexp() function.\r\nprint(\r\n \"The value of given first number*(2**second number) = \", rslt)\r\n# Give the number as user input using the int(input()) function and store it in another variable.\r\ngvn_numb3 = int(input(\"Enter some Random Number = \"))\r\n# Apply math.ldexp() function to the given list element and above given number\r\n# and print it.\r\nprint(\r\n \"The value of given list element *(2** above given number ) = \", math.ldexp(\r\n gvn_lst[3], gvn_numb3))<\/pre>\n
Enter some random List Elements separated by spaces = 3 0 2 1\r\nEnter two random numbers separated by spaces = 5 6\r\nThe value of given first number*(2**second number) = 320.0\r\nEnter some Random Number = 7\r\nThe value of given list element *(2** above given number ) = 128.0<\/pre>\n
\n