JavaScript Reference ยท Math
Math.exp()
Reference for Math.exp(): computing e raised to a power, continuous growth and decay, the accurate expm1() variant, and its relation to Math.log().
Math.exp(x) returns e raised to the power x, where e is Euler's number, about 2.71828. It is the inverse of Math.log(): Math.exp(Math.log(5)) gives 5 (up to a rounding error).
The exponential function describes continuous growth and decay, such as compound interest, population models, radioactive decay and the sigmoid function in machine learning.
Math.exp(x)
Try it
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Syntax
Math.exp(x) Math.expm1(x)
| Name | Description |
|---|---|
x |
The exponent. A number or a value that converts to one. |
Return value: e ** x. The result is Infinity when x is too large (about 710 or more), 0 when x is -Infinity or very negative, and NaN for non-numeric input. Math.expm1(x) returns e ** x - 1.
Exceptions: a BigInt argument throws a TypeError.
Usage notes
- Same idea as
Math.E ** x, butMath.exp(x)is more accurate becauseMath.Eis itself rounded. - Overflow and underflow.
Math.exp(709)is finite andMath.exp(710)isInfinity; strongly negative inputs turn into0. For the sigmoid and softmax functions, subtract the maximum input first to keep the values in range. Math.expm1(x)givese ** x - 1without losing digits whenxis tiny. The plain subtractionMath.exp(x) - 1cancels most significant digits near zero.- Inverse.
Math.log()undoesMath.exp(), andMath.exp()undoesMath.log(). - Hyperbolic functions such as
Math.cosh()andMath.tanh()are built from exponentials. - Discrete versus continuous growth: compare
(1 + r) ** twithMath.exp(r * t).
Browser support
Math.exp() is supported everywhere. Math.expm1() is part of ES2015.