EN
Java - Math.E property example
0
points
The Math.E
property returns e mathematical constant (2.718281828459045...
).
e
is called Euler's number or Napier's constant. However, it was discovered by Jacob Bernoulli. It is a mathematical constant used as the base of the natural logarithm.
public class MathExample {
public static void main(String[] args) {
System.out.println( Math.E ); // 2.718281828459045
System.out.println( Math.exp(1) ); // 2.718281828459045
System.out.println( Math.exp(2) ); // 7.38905609893065
System.out.println( Math.exp(3) ); // 20.085536923187668
}
}
1. Documentation
Syntax |
|
Result | e number (2.718281828459045... ). |
Description |
|
2. e
number approximation example
To calculate e
The following function with infinity series can be used to get a better precision infinite number of iterations with big precision numbers.
public class CustomMath {
static double computeE(int iterations) {
double e = 0;
for (int i = 0; i < iterations; i++) {
double divider = 1;
for (int j = 0; j < i; j++) {
divider *= (j + 1);
}
e += (1.0 / divider);
}
return e;
}
public static void main(String[] args) {
System.out.println(CustomMath.computeE(1) ); // 1.0
System.out.println(CustomMath.computeE(2) ); // 2.0
System.out.println(CustomMath.computeE(5) ); // 2.708333333333333
System.out.println(CustomMath.computeE(10)); // 2.7182815255731922
System.out.println(CustomMath.computeE(20)); // 2.7182818284590455
System.out.println(CustomMath.computeE(50)); // 2.7182818284590455
}
}
Output:
1.0
2.0
2.708333333333333
2.7182815255731922
2.7182818284590455
2.7182818284590455