Python expm1: How to Use Python expm1() Function
The expm1() function is defined under the math library, so we need to import the math library before using the expm1() method.
Python expm1() is an inbuilt math function that is used to calculate the value of exp(n)-1 means exponential of n and then subtracted from 1. The value of e is approximately equal to 2.71828…
The Python expm1() method is one of the Python Math functions, which is to calculate the power of E (Where E is Euler’s number approximately equal to 2.71828) and subtracts one from it.
The Python math expm1() Function takes only one argument num of which we want to find exponential.
The Python math expm1() function returns the floating type number by calculating e**n (e^n)-1 The expm1() Function returns a TypeError if the given input is not a number.
Difference between expm1(n) and exp(n)-1
Now one question may come to your mind that, why should we use exp1() we can use the second way. But there is the main difference between these two:
- exp(n)-1 method is used as a formula in many scientific works. So it is better to use expm1(n) to remove confusion.
- For the smaller value of x, of the order, less than the e-10, expm1() method gives more accurate results than exp()-1.
# Program to show the working of exp import math # Initializing the values # int type x = 10 # float type y = 12.6 # negative num z = -7 print("Value of e^x: ", math.expm1(x)) print("Value of e^y: ", math.expm1(y)) print("Value of e^z: ", math.expm1(z))
Value of e^x: 22025.465794806718 Value of e^y: 296557.5652982028 Value of e^z: -0.9990881180344455
In this program, we have first initialized all types of variables and checked the value in which this Function is returning every time.
# Program to show diff between # exp()-1 and expm1() # Importing math import math # initializing the value n = 1e-10 # Now printing values using both the function print("Value of n using exp(): ", math.exp(n)-1) print("Value of n using expm1(): ", math.expm1(n))
python3 app.py Value of n using exp(): 1.000000082740371e-10 Value of n using expm1(): 1.00000000005e-10
In this program, we have initialized value, and now we called both exp()-1 and expm1() Function. We can see that there is a little bit of difference in value. As said above, the value expm1() returned is more accurate.
Python expm1() with List and Tuple
See the following code.
import math Tup = (1.21, 20.26, 13.05, -40.95, 0.45) # Tuple Declaration Lis = [-10.21, 3.19, -9.59, -4.15, 5.97] # List Declaration print('Python EXPM1() Function on Positive Number = %.2f' % math.expm1(1)) print('Python EXPM1() Function on Negative Number = %.2f' % math.expm1(-1)) print('Python EXPM1() Function on Tuple Item = %.2f' % math.expm1(Tup)) print('Python EXPM1() Function on List Item = %.4f' % math.expm1(Lis)) print('Python EXPM1() Function on Multiple Number = %.4f' % math.expm1(11 + 21 - 15.19)) print('Python EXPM1() Function on String Value = ', math.expm1('46'))
python3 app.py Python EXPM1() Function on Positive Number = 1.72 Python EXPM1() Function on Negative Number = -0.63 Python EXPM1() Function on Tuple Item = 465095.41 Python EXPM1() Function on List Item = -0.9999 Python EXPM1() Function on Multiple Number = 19975157.8095 Traceback (most recent call last): File "app.py", line 15, in <module> print('Python EXPM1() Function on String Value = ', math.expm1('46')) TypeError: must be real number, not str
First, We declared Python List and Tuple with some random values.
Within the first two statements, We used the expm1 Function with both the Positive integer and negative integer values as arguments.
Next two statements, We used the expm1() Function on Python Tuple and List items. If you observe the above output, the expm1 Function is working correctly on them.
In the Last statement, We used the expm1 Function on the String value. As we said before, it returns TypeError because the expm1() function only accepts a real number and not string.