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.
xxxxxxxxxx
1
public class MathExample {
2
3
public static void main(String[] args) {
4
5
System.out.println( Math.E ); // 2.718281828459045
6
7
System.out.println( Math.exp(1) ); // 2.718281828459045
8
System.out.println( Math.exp(2) ); // 7.38905609893065
9
System.out.println( Math.exp(3) ); // 20.085536923187668
10
}
11
}
Syntax |
xxxxxxxxxx 1 package java.lang; 2 3 public final class Math { 4 5 public static final double E = 2.7182818284590452354; 6 7 }
|
Result | e number (2.718281828459045... ). |
Description |
|
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.
xxxxxxxxxx
1
public class CustomMath {
2
3
static double computeE(int iterations) {
4
double e = 0;
5
for (int i = 0; i < iterations; i++) {
6
double divider = 1;
7
for (int j = 0; j < i; j++) {
8
divider *= (j + 1);
9
}
10
e += (1.0 / divider);
11
}
12
return e;
13
}
14
15
public static void main(String[] args) {
16
17
System.out.println(CustomMath.computeE(1) ); // 1.0
18
System.out.println(CustomMath.computeE(2) ); // 2.0
19
System.out.println(CustomMath.computeE(5) ); // 2.708333333333333
20
System.out.println(CustomMath.computeE(10)); // 2.7182815255731922
21
System.out.println(CustomMath.computeE(20)); // 2.7182818284590455
22
System.out.println(CustomMath.computeE(50)); // 2.7182818284590455
23
}
24
}
Output:
xxxxxxxxxx
1
1.0
2
2.0
3
2.708333333333333
4
2.7182815255731922
5
2.7182818284590455
6
2.7182818284590455