Home Communities
IT Knowledge
Inspiration
Languages
EN

# Java - Math.E property example

0 points
Created by:
517

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 ``````package java.lang; public final class Math { public static final double E = 2.7182818284590452354; }`````` Note: Classes in the `java.lang` package are imported automatically, so it is not necessary to do it manually - we use just `Math.E()` call. Result `e` number (`2.718281828459045...`). Description `E` is a static property that keeps `e` number what is one of the most commonly used mathematical constants.

## 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``````

## References

1. e (mathematical constant) - Wikipedia

Join to our subscribers to be up to date with content, news and offers.