3v4l.org

run code in 500+ PHP versions simultaneously
<?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);

preferences:
63.47 ms | 2705 KiB | 5 Q