Languages
[Edit]
EN

Java - Math.exp() method example

0 points
Created by:
Emelia
517

Math.exp() is a static method that takes only one parameter and returns exponential function value in the range 0 (exclusive) to +Infinity.

public class MathExample {

    public static void main(String[] args) {
        System.out.println( Math.exp(      -100 ) ); // 3.720075976020836E-44
        System.out.println( Math.exp(        -1 ) ); // 0.36787944117144233
        System.out.println( Math.exp(         0 ) ); // 1.0
        System.out.println( Math.exp(         1 ) ); // 2.718281828459045
        System.out.println( Math.exp(       100 ) ); // 2.6881171418161356E43

        System.out.println( Math.exp( Double.NEGATIVE_INFINITY ) ); // 0.0
        System.out.println( Math.exp( Double.POSITIVE_INFINITY ) ); // Infinity
        System.out.println( Math.exp( Double.NaN ) ); // NaN
    }
}

1. Documentation

Syntax
package java.lang;

public final class Math {

    public static double exp(double number) { ... }

}

Note: Classes in the java.lang package are imported automatically, so it is not necessary to do it manually - we use just Math.exp() call.

Parametersnumber - double value (primitive value).
Result

Exponential function value of a number in the range 0 (exclusive) to +Infinity (primitive value).

If the value can not be calculated NaN is returned.

Description

Math.exp() is a static method that takes only one parameter and returns exponential function value.

2. Reversed console plot example

public class MathExample {

    static void printLine(double y1, double y2, double dy, String character) {
        StringBuilder line = new StringBuilder();
        for (double y = y1; y < y2; y += dy) {
            line.append(" ");
        }
        System.out.println(line + character);
    }

    public static void main(String[] args) {
        double x1 = -4;   // beginning of sine chart
        double x2 = +2.8; // end of sine chart

        double y1 = -1.0;
        double y2 = +10.0;

        double xSteps = 25;
        double ySteps = 60;

        double dx = (x2 - x1) / xSteps; // x axis step
        double dy = (y2 - y1) / ySteps; // y axis step

        for (double rad = x1; rad < x2; rad += dx) {
            double y = Math.exp(rad);
            if (y <= y1 || y >= y2) {
                System.out.println(" ");
            } else {
                printLine(y1, y, dy, "+");
            }
        }
    }
}

 

Output: 

 

      +
      +
      +
      +
      +
      +
      +
       +
       +
       +
       +
        +
         +
         +
          +
            +
              +
                +
                   +
                       +
                             +
                                    +
                                              +
                                                          +

 

References

 

  1. Exponential function - Wikipedia

 

Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

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