Saturday, November 16, 2024
Google search engine
HomeLanguagesPython math library | expm1() method

Python math library | expm1() method

Python has math library and has many functions regarding to it. One such function is expm1(). This function mathematically computes the value of exp(x) – 1. This method can be used if we need to compute this very value.

Syntax : math.expm1() 
Parameters : x : Number whose exp(x)-1 has to be computed. 
Returns : Returns the computed value of “exp(x)-1”

Time Complexity: O(1)
Auxiliary Space: O(1)

Code #1 : Demonstrate the working of expm1() 

Python3




# Python3 code to demonstrate
# the working of expm1()
import math
 
# initializing the value
test_int = 4
test_neg_int = -3
 
# checking expm1() values
# doesn't throw error with negative
print ("The expm1 value using positive integer : "
                    + str(math.expm1(test_int)))
                         
print ("The expm1 value using negative integer : "
                + str(math.expm1(test_neg_int)))


Output :

The expm1 value using positive integer : 53.598150033144236
The expm1 value using negative integer : -0.950212931632136
“exp() – 1” vs “expm1()”

There would be a question why expm1() method was even created if we could always compute exp() and then subtract 1 from it. The first reason is that value exp() – 1 is used a lot in mathematics and science applications and formulas. The most important reason is that for smaller value of x, of the order less than e-10, expm1() method give a result more accurate than exp() – 1. Code #2 : Comparing expm1() and exp()-1 

Python3




# Python3 code to demonstrate
# the application of expm1()
import math
 
# initializing the value
test_int = 1e-10
 
# checking expm1() values
# expm1() is more accurate
print ("The value with exp()-1 : " + str(math.exp(test_int)-1))
print ("The value with expm1() : " + str(math.expm1(test_int)))


Output :

The value with exp()-1  : 1.000000082740371e-10
The value with expm1() : 1.00000000005e-10

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments