Math.exp
func Math.exp(x: float64): float64Math.exp returns e raised to the power x.
Details
Math.expnever panics. A largexoverflows toInfinity, a very negativexunderflows to0.0, andNaNgivesNaN.
Basic examples
Math.exp(0.0) // 1.0
Math.exp(1.0) // 2.718281828459045
Math.exp(-1.0) // 0.36787944117144233Scope
Negative inputs give values between 0.0 and 1.0, and -0.0 gives 1.0:
Math.exp(-2.0) // 0.1353352832366127
Math.exp(-0.0) // 1.0The infinities and NaN follow IEEE 754:
Math.exp(Math.Infinity) // Infinity
Math.exp(-Math.Infinity) // 0.0
Math.exp(Math.NaN) // NaNAn integer goes through float64 first:
let n = 3
Math.exp(float64(n)) // 20.085536923187668Pass it by name to Array.map, or call it in a pipe:
[0.0, 1.0, 2.0].map(Math.exp) // [1.0, 2.718281828459045, 7.38905609893065]
1.0 | Math.exp() // 2.718281828459045Applications
Grow a deposit at 5% interest compounded continuously for ten years:
let principal = 1000.0
let rate = 0.05
principal * Math.exp(rate * 10.0) // 1648.7212707001281Find how much carbon-14 is left after two half-lives, from the decay constant:
let halfLife = 5730.0
let k = Math.log(2.0) / halfLife
Math.exp(-k * 11460.0) // 0.25Squash any score into 0.0 ..= 1.0 with the logistic function:
func sigmoid(x: float64): float64 {
1.0 / (1.0 + Math.exp(-x))
}
println([-2.0, 0.0, 2.0].map(sigmoid))[0.11920292202211755, 0.5, 0.8807970779778823]Turn scores into probabilities with softmax. Subtracting the largest score first keeps every Math.exp finite, even for large scores:
func softmax(xs: Array[float64]): Array[float64] {
let top = xs.max() ?? 0.0
let ws = xs.map({ Math.exp(x - top) })
let total = ws.sum() ?? 0.0
ws.map({ x / total })
}
println(softmax([1.0, 2.0, 3.0]))
println(softmax([1000.0, 1001.0]))[0.09003057317038046, 0.24472847105479764, 0.6652409557748218]
[0.2689414213699951, 0.7310585786300049]Properties and relations
Math.exp never returns a negative number and never decreases, Math.log undoes it, and exp(x) * exp(-x) is 1.0, each up to rounding for inputs that neither overflow nor underflow:
func close(a: float64, b: float64): bool {
Math.abs(a - b) <= 1e-12 * Math.max(Math.abs(a), Math.abs(b))
}
let xs = [-3.0, -1.0, 0.0, 0.3, 2.0]
let ys = xs.map(Math.exp)
for i in Iter.range(0, xs.length()) {
assert(ys[i] >= 0.0)
assert(close(Math.log(ys[i]), xs[i]))
assert(close(ys[i] * Math.exp(-xs[i]), 1.0))
if i > 0 {
assert(ys[i - 1] <= ys[i])
}
}Possible issues
The result overflows to Infinity a little above 709.0:
Math.exp(709.0) // 8.218407461554972e+307
Math.exp(710.0) // Infinity
Math.isFinite(Math.exp(710.0)) // falseSo a ratio of two exponentials that both overflow is Infinity / Infinity, which is NaN, though the true ratio is about 0.37. Shift the inputs first, as softmax does above:
let ws = [1000.0, 1001.0].map(Math.exp)
ws[0] / ws[1] // NaN
Math.exp(1000.0 - 1001.0) // 0.36787944117144233A very negative input underflows to 0.0, and the logarithm can no longer recover it:
Math.exp(-746.0) // 0.0
Math.log(Math.exp(-746.0)) // -InfinityWork with logarithms instead of the exponentials themselves, or shift the inputs as softmax does.
Rounding can make Math.log(Math.exp(x)) differ from x in the last digit, so compare with a tolerance rather than ==:
Math.log(Math.exp(0.3)) // 0.30000000000000004Math.exp(x) and Math.pow(Math.E, x) can differ in the last digit too:
Math.exp(2.0) // 7.38905609893065
Math.pow(Math.E, 2.0) // 7.3890560989306495An integer argument is a compile error, since a numeric literal without a point is an int:
Math.exp(1) // error: cannot use int where float64 is expectedMath.exp is not a function on float64, so it cannot be called through .:
2.0.exp() // error: float64 has no field or function expSee also
Math.log · Math.pow · Math.log2 · Math.log10 · Math.isFinite · Math
