math.exp()
The math.exp()
function computes the value of the mathematical constant ( e ) (approximately 2.718) raised to the power of a given number. It is commonly used in exponential growth, decay calculations, and other mathematical applications.
Syntax
math.exp(`x`)
Parameters
- Name
x
- Type
- number
- Description
A number representing the exponent to which ( e ) is raised.
Return Value
Returns the result of ( e^x ), where ( e ) is the base of the natural logarithm (approximately 2.718).
- If
x
is0
, the function returns1
(since ( e^0 = 1 )). - If
x
is a negative number, the function returns the reciprocal of\( e^{|x|} \)
. - If
x
is invalid or non-numeric, the function returnsnil
.
Examples
Example 1: Basic Exponentiation
Compute \( e^x \)
for a positive number.
Example 1
local result = math.exp(1)
print(result)
-- Output: 2.718281828459
Example 2: Zero as the Exponent
Compute ( e^0 ).
Example 2
local result = math.exp(0)
print(result)
-- Output: 1
Example 3: Negative Exponents
Calculate \( e^{-x} \)
for a negative number.
Example 3
local result = math.exp(-1)
print(result)
-- Output: 0.36787944117144
Example 4: Exponentiation with Larger Numbers
Compute \( e^x \)
for a larger value of \( x \)
.
Example 4
local result = math.exp(5)
print(result)
-- Output: 148.41315910258
Example 5: Small Values of \( x \)
Compute ( e^x ) for small positive numbers.
Example 5
local result = math.exp(0.1)
print(result)
-- Output: 1.1051709180756
Use Cases
- Exponential Growth and Decay: Calculate compound growth or decay over time.
- Mathematical Modeling: Solve problems involving the natural exponential function.
- Logarithms and Inverses: Use in conjunction with
math.log()
for inverse operations.
See also
math.log()
: Calculates the natural logarithm of a number, the inverse ofmath.exp()
.math.pow()
: Computes the result of raising any base to a power.math.sqrt()
: Computes the square root of a number, useful for related calculations.