EN
JavaScript - Math.E property example
7 points
The Math.E
property returns e mathematical constant (2.718281828459045...
).
e
is called as Euler's number or as Napier's constant. However, it was discovered by Jacob Bernoulli. It is a mathematical constant used as the base of the natural logarithm.
xxxxxxxxxx
1
console.log( Math.E ); // 2.718281828459045
2
3
console.log( Math.exp(1) ); // 2.718281828459045
4
console.log( Math.exp(2) ); // 7.38905609893065
5
console.log( Math.exp(3) ); // 20.085536923187668
Syntax | Math.E |
Result | e number (2.718281828459045... ). |
Description |
|
To calculate e
number following function with infinity series can be used - to get better precision infinite number of iterations with big precision numbers should be used.
xxxxxxxxxx
1
function computeE(iterations) {
2
var e = 0;
3
4
for (var i = 0; i < iterations; ++i) {
5
var divider = 1;
6
7
for (var j = 0; j < i; ++j) {
8
divider *= (j + 1);
9
}
10
11
e += (1 / divider);
12
}
13
14
return e;
15
}
16
17
console.log( computeE( 1 ) ); // 1
18
console.log( computeE( 2 ) ); // 2
19
console.log( computeE( 5 ) ); // 2.708333333333333
20
console.log( computeE( 10 ) ); // 2.7182815255731922
21
console.log( computeE( 20 ) ); // 2.7182818284590455
22
console.log( computeE( 50 ) ); // 2.7182818284590455