Mathematics7 min readExamples run on PHP 8.5.10

exp()

Learn how PHP's exp() raises e to a power, how it handles overflow and underflow, and which mistakes to avoid in growth, decay and scoring code.

Signature

exp(float $num): float

Purpose

exp() computes e raised to a power, where e is Euler's number, roughly 2.718282. It is the inverse of the natural logarithm log(). You will meet it in continuous growth and decay, probability formulas, logistic scoring and any model written as e^x.

For an arbitrary base, use pow() or the ** operator instead. exp($x) is the dedicated tool when the base is specifically e.

Signature

Parameters and defaults

  • $num: the exponent. It is required and has no default value.

The parameter is declared as float. In normal (non-strict) mode, integers and numeric strings are converted to float. In strict-types mode, an integer is still accepted for a float parameter, but a string is not. A value that cannot be treated as a number, such as an array or a non-numeric string, raises a TypeError.

Return value and failure cases

exp() always returns a float. It does not return false and does not emit warnings for large inputs. Instead, extreme inputs follow floating-point rules:

  • exp(0) is exactly 1.0.
  • Negative exponents give results between 0 and 1.
  • A very large exponent overflows to INF. On common 64-bit builds this happens at around 710.
  • A very negative exponent underflows to 0.0.
  • NAN as input gives NAN.

The only exception is a TypeError for an argument of the wrong type.

Example 1: basic values with rounding

Floats carry many digits, so round before comparing or displaying. The three values below are e, its reciprocal, and the value used in the manual's own example.

Example
<?php
$values = [
    round(exp(1), 6),
    round(exp(-1), 6),
    round(exp(5.7), 6),
];
echo json_encode($values), PHP_EOL;
Output
[2.718282,0.367879,298.867401]

Example 2: continuous growth and decay

The formula P * e^(r*t) models a quantity growing continuously at rate r for time t. A negative rate models decay.

Example
<?php
function continuousGrowth(float $principal, float $rate, float $time): float
{
    return $principal * exp($rate * $time);
}

$result = [
    round(continuousGrowth(1000, 0.05, 2), 2),
    round(continuousGrowth(200, -0.1, 3), 2),
];
echo json_encode($result), PHP_EOL;
Output
[1105.17,148.16]

Example 3: edge cases

The following calls show the identity value, overflow and underflow. var_dump() shows the float type explicitly.

Example
<?php
var_dump(exp(0));
var_dump(exp(710));
var_dump(exp(-1000));
Output
float(1)
float(INF)
float(0)

Edge cases

  • Overflow is silent. A result of INF can spread through later arithmetic. If inputs are not trusted, test with is_infinite() or clamp the exponent first.
  • Underflow is silent too. Very negative exponents give 0.0, which may then cause a division by zero further on.
  • Precision. Results are binary floating-point approximations. Never compare them with ==. Round them or compare with a small tolerance.
  • Large results lose integer exactness. Casting a big exp() result to int is rarely meaningful.

Common mistakes

  1. Using exp() as if it were a general power function. exp(2) is e squared, not 2 squared or 10 squared. For 2 ** 3 use the ** operator.
  2. Confusing it with log(). log() is the natural logarithm and undoes exp(). It is not base 10; for that use log10().
  3. Printing raw floats in tests. Output depends on precision settings. Rounding first gives stable results.
  4. Forgetting units. In growth formulas the rate and time must use the same unit, such as a yearly rate with time in years.
  5. Passing percentages directly. A rate of 5% must be 0.05, not 5. Passing 5 gives a huge number rather than an error.
  6. Ignoring overflow. Inputs above roughly 709 produce INF rather than a failure you can catch.
  • log(): natural logarithm, the inverse of exp().
  • pow(): raises any base to any exponent, also available as **.
  • is_infinite(): detects overflowed results.
  • round(): stabilizes float output for display and comparison.

Recap

exp(float $num): float returns e to the power of $num. It always returns a float, gives 1.0 for zero, shrinks toward zero for negative input, and silently becomes INF or 0.0 at the extremes. Round results before showing or comparing them, and use pow() when the base is not e.

Tested examples

Basic values with rounding
<?php
$values = [
    round(exp(1), 6),
    round(exp(-1), 6),
    round(exp(5.7), 6),
];
echo json_encode($values), PHP_EOL;
Output
[2.718282,0.367879,298.867401]
Continuous growth and decay
<?php
function continuousGrowth(float $principal, float $rate, float $time): float
{
    return $principal * exp($rate * $time);
}

$result = [
    round(continuousGrowth(1000, 0.05, 2), 2),
    round(continuousGrowth(200, -0.1, 3), 2),
];
echo json_encode($result), PHP_EOL;
Output
[1105.17,148.16]
Identity, overflow and underflow
<?php
var_dump(exp(0));
var_dump(exp(710));
var_dump(exp(-1000));
Output
float(1)
float(INF)
float(0)