Languages
[Edit]
EN

Java - Math.E property example

0 points
Created by:
Nikki-Mathews
337

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.

Resulte 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

Java - Math object

Native Advertising
šŸš€
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

ā¤ļøšŸ’» šŸ™‚

Join