Signature
exp(float $num): floatPurpose
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 exactly1.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. NANas input givesNAN.
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.
$values = [
round(exp(1), 6),
round(exp(-1), 6),
round(exp(5.7), 6),
];
echo json_encode($values), PHP_EOL;[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.
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;[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.
var_dump(exp(0));
var_dump(exp(710));
var_dump(exp(-1000));float(1) float(INF) float(0)
Edge cases
- Overflow is silent. A result of
INFcan spread through later arithmetic. If inputs are not trusted, test withis_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 tointis rarely meaningful.
Common mistakes
- Using
exp()as if it were a general power function.exp(2)isesquared, not 2 squared or 10 squared. For2 ** 3use the**operator. - Confusing it with
log().log()is the natural logarithm and undoesexp(). It is not base 10; for that uselog10(). - Printing raw floats in tests. Output depends on precision settings. Rounding first gives stable results.
- Forgetting units. In growth formulas the rate and time must use the same unit, such as a yearly rate with time in years.
- Passing percentages directly. A rate of 5% must be
0.05, not5. Passing5gives a huge number rather than an error. - Ignoring overflow. Inputs above roughly 709 produce
INFrather than a failure you can catch.
Related functions
log(): natural logarithm, the inverse ofexp().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
$values = [
round(exp(1), 6),
round(exp(-1), 6),
round(exp(5.7), 6),
];
echo json_encode($values), PHP_EOL;[2.718282,0.367879,298.867401]
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;[1105.17,148.16]
var_dump(exp(0));
var_dump(exp(710));
var_dump(exp(-1000));float(1) float(INF) float(0)