Skip to content

Core\Math::exp

Returns E raised to the power of the number you give it. E is a fixed number, about 2.718, and Core\Math::E contains it. So Core\Math::exp(1.0) is E, and Core\Math::exp(0.0) is 1.0.

The result is never negative, and it grows very fast. Above about 709.78 it is too big for a float, and the result is INFINITY. Far enough below zero, the result is 0.0. Core\Math::log does the reverse. NaN (a value that means "not a number") gives NaN.

In plain words: exp describes something that grows or shrinks by the same share of its size all the time. Money with interest grows this way. A score that loses value with age shrinks this way.

The examples below show a few results, then numbers that are too big or too small, then how a website ranks posts so that a new post can come before an older one with more votes.

Parameters

  • $nfloatThe power to raise E to.

Return value

Returns float. E ** $n, which is never negative. Above about 709.78 the result is INFINITY. Far enough below zero, the result is 0.0. NaN gives NaN.

Examples

A few results

<?nvs
// Shows a few results of `Core\Math::exp`.
// Any number to the power 0 is 1. This prints 1.
echo Core\Math::exp(0.0), "\n";
// `E` to the power 1 is `E`. This prints 2.718281828459.
echo Core\Math::exp(1.0), "\n";
// This is `E` × `E`. It prints 7.3890560989307.
echo Core\Math::exp(2.0), "\n";
// A negative power gives a number between 0 and 1. This prints 0.36787944117144.
echo Core\Math::exp(-1.0), "\n";
// `Core\Math::log` does the reverse. This prints 5.
echo Core\Math::log(Core\Math::exp(5.0)), "\n";
Output
1
2.718281828459
7.3890560989307
0.36787944117144
5

Too big and too small

<?nvs
// Shows what `Core\Math::exp` returns when the result is too big or too small for a `float`.
// This result still fits in a `float`. It prints 1.7928227943945E+308.
echo Core\Math::exp(709.78), "\n";
// A little higher, the result is too big. This prints INF.
echo Core\Math::exp(710.0), "\n";
// Far below zero, the result is too small to store. This prints 0.
echo Core\Math::exp(-800.0), "\n";
// `Core\Math::isFinite` tests whether the result fits. This prints "too big".
float $growth = Core\Math::exp(1000.0);
echo Core\Math::isFinite($growth) ? "fits\n" : "too big\n";
Output
1.7928227943945E+308
INF
0
too big

Rank posts by age

<?nvs
// Shows how a website ranks posts so that a new post can come before an older one with more votes.
class Ranking
{
// Returns the score of a post, with one decimal place.
// A vote loses value as the post gets older. After 24 hours, it counts about a third.
public static function score(float $votes, float $hours): float
{
return Core\Math::round($votes * Core\Math::exp(-$hours / 24.0), { precision: 1 });
}
}
// A new post: 40 votes, 2 hours old. This prints 36.8.
echo Ranking::score(40.0, 2.0), "\n";
// A post from this morning: 60 votes, 12 hours old. This prints 36.4.
echo Ranking::score(60.0, 12.0), "\n";
// A post from two days ago: 150 votes, 48 hours old. This prints 20.3.
echo Ranking::score(150.0, 48.0), "\n";
Output
36.8
36.4
20.3