expm1(): Precise Computation for ๐‘’x - 1

expm1() returns e raised to the power x ( input number ) -1 .
Here are some examples with different type of numbers.
import mathprint(math.expm1(5))   # 147.4131591025766print(math.expm1(0))   # 0.0
Using negative number
import mathprint(math.expm1(-34.11))   # -0.9999999999999984print(math.expm1(-34.99))   # -0.9999999999999993print(math.expm1(-34))      # -0.9999999999999983

Using formula and e

The math formula for expm1() function is e ** x -1
import mathprint(math.e ** -34.11 -1 ) # -0.9999999999999984
e : is a numerical constant of value equal to 2.71828
This is the base of natural logarithm and known as Euler's number.

Additional Explanation and Use Cases for expm1()

The math.expm1(x) function calculates ( e^x - 1 ) accurately for small values of x, avoiding precision errors associated with directly computing `math.exp(x) - 1`.

Example 1: Why Use expm1() for Small Values of x

For very small values of x, using `expm1()` is more precise than `exp(x) - 1`.
import mathx = 1e-10print(math.exp(x) - 1)       # Less accurate due to floating-point roundingprint(math.expm1(x))         # More accurate for small x

Example 2: Comparing exp() and expm1()

Comparing results from `exp()` and `expm1()` for different values of x.
import mathx_values = [1, 0.1, 1e-10, -1]for x in x_values:    print(f"x: {x}, exp(x) - 1: {math.exp(x) - 1}, expm1(x): {math.expm1(x)}")
Output
x: 1, exp(x) - 1: 1.718281828459045, expm1(x): 1.718281828459045x: 0.1, exp(x) - 1: 0.10517091807564771, expm1(x): 0.10517091807564763  x: 1e-10, exp(x) - 1: 1.000000082740371e-10, expm1(x): 1.00000000005e-10x: -1, exp(x) - 1: -0.6321205588285577, expm1(x): -0.6321205588285577  

Example 3: Financial Calculation Use Case

Use `expm1()` for financial computations where precision is essential, like calculating continuous interest.
import mathprincipal = 1000  # Principal amountrate = 0.05       # Annual interest ratetime = 1          # 1 year# Continuous compound interestinterest = principal * math.expm1(rate * time)print("Accrued interest:", interest)
Output
Accrued interest: 51.271096376024055

Applications of expm1()

  • Mathematical Computations: In scientific computing where small x values are common.
  • Continuous Compounding in Finance: Useful for precise financial calculations.
  • Physics and Engineering: Applicable in exponential decay/growth calculations.
These enhancements offer practical insights into `expm1()` and show when itโ€™s preferable over `exp() - 1` for computational accuracy.
floor() & ceil() modf() exp()