Python math.exp()
Method
Python has an in-built module called math
, which provides a set of mathematical functions which allows you to perform extended mathematical operations beyond what’s provided by native Python.
One of the useful functions in this module is the math.exp()
function which allows us to calculate the exponential of a given number. The function always perform the exponential on bases on Euler’s number (e
= 2.71828)..
In simple terms, the function calculates e
raised to the power of the input number.
In this tutorial will walk you through comprehensive understanding of the math.exp()
method in Python with practical examples.
Import the Math Module
Before we can use the exp()
function from the math module, we need to import it using the import
keyword.
import math
The Python math.exp()
Method
Function Syntax
The syntax for the math.exp()
function is:
math.exp(x)
Function Parameters
x
- This is a required parameter formath.exp()
function. It represents the number to which you wish to calculate it’s exponent.
Function Return Value
The function returns e
raised to the power of x
.
In the case of overflow, the function raises an OverflowError
exception.
Note: The math.exp()
function can accept both positive and negative numbers.
For negative numbers, the function will return e
raised to the power of the negative number, which is equivalent to 1 divided by e
raised to the power of the absolute value of the input.
Examples
Example 1 - Calculate the exponential of a number
The following example shows how to use the exp
function to determine the exponential of 1.
import math
print(math.exp(1))
Output:
2.718281828459045
The output is e
itself, because e
raised to the power of 1 equals e
.
Example 2 - Calculate the Exponential of a Negative Number
We can also calculate the exponential of a negative integer:
import math
print(math.exp(-1))
Output:
0.36787944117144233
This output is the reciprocal of e
(approximately 1/2.71828), because e
raised to the power of -1 equals 1/e
.
Example 3 - Calculate the Exponential of Zero
The exponential of zero is also an important concept in mathematics. e
raised to the power of 0 is 1.
import math
print(math.exp(0))
Output:
1.0
Example 4 - Handling OverflowError
Error
If you attempt to calculate the exponential of a very large number, Python will throw an OverflowError
error.
Example is as shown below:
import math
print(math.exp(10000))
This would result in:
---------------------------------------------------------------------------
OverflowError Traceback (most recent call last)
---
1 import math
----> 2 print(math.exp(10000))
OverflowError: math range error
To handle this error, we can use a try-except block.
import math
try:
print(math.exp(10000))
except OverflowError:
print("Number too large for exponential calculation.")
This would result in:
Number too large for exponential calculation
In this case, the program will not terminate but instead, it returns a graceful error.
Conclusion
The math.exp()
function is an important function for handling exponential calculations in Python. It’s simple to use and very powerful, as it can handle both positive and negative numbers. However, be aware of the possibility of an OverflowError when dealing with large numbers.