expm1() And log1p()

By Alexandre Daubois

<?php

$x = 1e-15;

// naive: catastrophic cancellation
$native = exp($x) - 1;
echo $native; // 1.1102230246252E-15, WRONG

// expm1: computed without cancellation
$precise = expm1($x);
echo $precise; // 1.00000000000000005E-15, CORRECT

// same problem in reverse
$y = 1e-15;

// naive
echo log(1 + $y); // 1.1102230246252E-15, WRONG
echo log1p($y);   // 1.00000000000000005E-15, CORRECT

// real-world use: compound interest on tiny rates.
$rate = 0.00001; // 0.001% daily rate
$days = 365;

// naive continuous compounding
$wrong = exp($rate * $days) - 1;

// precise
$right = expm1($rate * $days);

In PHP, expm1() computes exp(x) - 1.

log1p() computes log(1 + x).

“Why not just write exp($x) - 1” you may ask…

Because when x is close to 0, floating point eats your precision alive. Think about it if you deal with finance!

These two functions exist solely to save your math from IEEE 754.

See Also

PHP Features

Last updated: 14 July 2026