exp

Returns e raised to the power of the given number.

Signature
exp(float $num): float
  • Added in PHP 4.0

Parameters

ParameterTypeDescription
$numfloatThe argument to process, used as the exponent.

Return value

floate (approximately 2.718282) raised to the power of $num. Returns INF on overflow and 0.0 on underflow.

Errors

Throws a TypeError if $num is not a float or a value that can be coerced to float; overflow returns INF rather than throwing.

exp() returns M_E ** $num, where e is the base of natural logarithms, about 2.718282. It is the exact inverse of log(): log(exp($x)) gives back $x.

When to reach for it

Anywhere a quantity grows or decays in proportion to its current size. Continuously compounded interest is $principal * exp($rate * $years). Radioactive decay, capacitor discharge, cache hit-rate decay and exponential backoff all take the same shape with a negative exponent. In statistics and machine learning it is the backbone of the normal distribution, the softmax and the logistic function.

What trips people up

Overflow arrives sooner than people expect. A 64-bit float tops out around exp(709.78); anything above that returns INF with no warning, and the INF then spreads through every later calculation. This bites hardest in softmax, where the standard fix is to subtract the maximum value from every input before exponentiating.

At the other end, exp(-746) and below underflows to 0.0, which turns a subsequent log() into -INF. And when $num is very close to zero, exp($num) - 1 loses most of its significant digits to cancellation — use expm1() for that specific expression.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Calculate a balance under continuously compounded interest: principal * exp(rate * years).
  • Model exponential decay such as a half-life, a cooling curve or a time-decayed score.
  • Implement the logistic function 1 / (1 + exp(-x)) for a probability output.

Frequently asked questions

What is the difference between exp and pow in PHP?

exp($x) is always e raised to $x, while pow($base, $exp) lets you choose any base. exp($x) is equivalent to pow(M_E, $x) but is faster and slightly more accurate.

Why does exp return INF?

A 64-bit float cannot hold values above roughly 1.8e308, which exp() reaches at an argument of about 709.78. Anything larger returns INF silently, so clamp the input or subtract a constant offset first.

How do I compute exp(x) - 1 accurately for small x?

Use expm1($x) instead. For x near zero, exp($x) rounds to 1.0 and the subtraction throws away every significant digit, while expm1() keeps full precision.

More examples

Overflow-safe softmax

Both calls return the same probabilities — 0.0900, 0.2447, 0.6652 — because softmax only depends on the differences between inputs. Without the max subtraction the second call would compute exp(1003), overflow to INF, and return NAN for every element.

Example · php
<?php
function softmax(array $xs): array {
    $max = max($xs);                       // shift so the largest is 0
    $e = array_map(fn($x) => exp($x - $max), $xs);
    $sum = array_sum($e);
    return array_map(fn($v) => $v / $sum, $e);
}
print_r(array_map(fn($p) => round($p, 4), softmax([1.0, 2.0, 3.0])));
print_r(array_map(fn($p) => round($p, 4), softmax([1001.0, 1002.0, 1003.0])));

Logistic function and its inverse

The sigmoid squashes any real number into (0, 1) and passes through exactly 0.5 at x = 0. The last line applies the logit, the inverse transform, recovering 2.000000 and confirming that log() and exp() undo each other.

Example · php
<?php
$sigmoid = fn(float $x): float => 1 / (1 + exp(-$x));

foreach ([-4, -1, 0, 1, 4] as $x) {
    printf("sigmoid(%2d) = %.6f\n", $x, $sigmoid($x));
}
$p = $sigmoid(2.0);
printf("logit back: %.6f\n", log($p / (1 - $p)));

Continuous versus monthly compounding

Monthly compounding gives 1647.01 and continuous compounding gives 1648.72, a gap of 1.71 on a 1000 principal. Continuous compounding is the mathematical limit of ever more frequent compounding, which is why exp() shows up in finance formulas.

Example · php
<?php
$p = 1000.0; $rate = 0.05; $years = 10;

$monthly    = $p * (1 + $rate / 12) ** ($years * 12);
$continuous = $p * exp($rate * $years);

printf("Monthly:    %.2f\n", $monthly);
printf("Continuous: %.2f\n", $continuous);
printf("Difference: %.2f\n", $continuous - $monthly);

Discussion

  • Be the first to comment on this lesson.