PHP expm1() Function

The expm1() function calculates ex - 1 in a numerically stable way. It provides better precision than manually computing exp(x) - 1, especially when the value of x is close to zero.

Syntax

expm1(float $arg) : float

Parameters

Parameter Description
arg A floating point number whose expm1 value is to be calculated

Return Value

Returns earg - 1 as a floating point number.

Examples

Basic Usage

Here's a simple example demonstrating the expm1() function −

<?php
    echo "expm1(1) = " . expm1(1) . "<br>";
    echo "expm1(0) = " . expm1(0) . "<br>";
    echo "expm1(-1) = " . expm1(-1) . "<br>";
    echo "expm1(2) = " . expm1(2) . "<br>";
?>
expm1(1) = 1.718281828459
expm1(0) = 0
expm1(-1) = -0.63212055882856
expm1(2) = 6.3890560989307

Precision Advantage

This example shows why expm1() is more accurate than exp() - 1 for small values −

<?php
    $x = 1e-10;  // Very small number
    
    echo "Using expm1(): " . expm1($x) . "<br>";
    echo "Using exp()-1: " . (exp($x) - 1) . "<br>";
    echo "Difference: " . abs(expm1($x) - (exp($x) - 1)) . "<br>";
?>
Using expm1(): 1.00000000005E-10
Using exp()-1: 1.00000000005E-10
Difference: 0

Common Use Cases

The expm1() function is particularly useful in:

  • Financial calculations involving compound interest
  • Scientific computations requiring high precision
  • Statistical analysis with exponential distributions

Conclusion

The expm1() function provides a numerically stable way to calculate ex - 1, offering better precision than manual computation, especially for values close to zero. It's essential for applications requiring high mathematical accuracy.

Updated on: 2026-03-15T08:56:32+05:30

151 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements