Java – Find Euler’s Number e – exp() Method – Examples & Explanation

java.lang.Math.exp() method is used to find the Euler’s number e raised to the power of a double value in Java for the given input (x – parameter).
The computed result must be within 1 ulp of the exact result. Results must be semi-monotonic.
The value of constant e = 2.71828 approximately.

exp() method exists in Math class of java.lang package.
Since: JDK1.0

 

Declaration of exp() Method

The declaration of exp() method is:

public static double exp(double x)

 

Syntax of exp() Method

The syntax of exp() method in Java is:

 
java.lang.Math.exp(double x)

Example

/*
*Find the Euler's number e raised to the power of a double value of given input(double) without importing the 
java.lang Package.
*/
public class JavaMathExample {
    public static void main(String[] args) {
        //Declaring variables
        double x = 91;
        double result;
        //Assign Euler's number e raised to the power of a double value of x to result variable
        result = java.lang.Math.exp(x);
        //Printing result to the console
        System.out.println(result);
    }
}

Output:

3.317400098335743E39

Example
By importing java.lang package the syntax of exp() method can be used as:

/*
*Find the Euler's number e raised to the power of a double value of given input(double) by importing the java.lang Package.
*/
import java.lang.*;  //importing package

public class JavaMathExample {
    public static void main(String[] args) {
        
        //Declaring variables
        double x = 3.317400098335743E39;
        double result;
        
        //Assign Euler's number e raised to the power of a double value of x to result variable
        result = Math.exp(x);
        
        //Printing result to the console
        System.out.println(result);
    }
}

Output:

Infinity

 

Parameters of exp() Method in Java

x – Where x is any valid double input value. This parameter is required.

 

Error Handling

If the x parameter is not a number exp() method returns an error.
If there is no argument (x – input value) passes to the method, the compiler will produce an error.Example

/*
*This example returns a compile time error
*/
import java.lang.*; //importing java.lang package

public class JavaMathExample {
    public static void main(String[] args) {

        //Declaring variables
        String x = "Not a number";
        double result;

        //argument is not a number
        result = Math.exp(x);
        System.out.println(result);

        //Here no argument is used
        result = Math.exp();
        System.out.println(result);


    }

}

Output:
Error

 

Return Value of exp() Method in Java

exp() method returns the Euler’s number e raised to the power of a double value for the given input (x – parameter).The value e, where e is the base of the natural logarithms.

 

Java exp() Method Example 1

import java.lang.*; //importing java.lang package

public class JavaMathExample {
    public static void main(String[] args) {
        double negativeValue = -18.869;
        double positiveValue = 27.78639;
        double zero = 0;
        System.out.println("Euler's number e raised to the power of a double value of a Negative Value: "+Math.exp(negativeValue));
        System.out.println("Euler's number e raised to the power of a double value of a Positive Value: "+Math.exp(positiveValue));
        System.out.println("Euler's number e raised to the power of a double value of Zero: "+Math.exp(zero));

    }
}

Output:

Euler's number e raised to the power of a double value of a Negative Value: 6.387007424042083E-9
Euler's number e raised to the power of a double value of a Positive Value: 1.1680887709824473E12
Euler's number e raised to the power of a double value of Zero: 1.0

 

Java exp() Method Example 2

import java.lang.*; //importing java.lang package

public class JavaMathExample {
    public static void main(String[] args) {
        //Declare and initialize an array containing double values
        double test [] = { Double.MAX_VALUE, Double.MIN_VALUE, 
		Double.NaN, Double.MIN_EXPONENT, 42.867e-12, 16.852, 0, -2.58, -78.8569e7 };

        //Use for loop to print Euler's number e raised to the power of a double value of each element of the array
        for (int i=0; i < test.length; i++) {
            System.out.println(  Math.exp(test[i])  );
        }

    }
}

Output:

Infinity
1.0
NaN
0.0
1.000000000042867
2.0831982834638383E7
1.0
0.07577400402284548
0.0

 

Special Cases of exp() Method in Java

If the argument is NaN, the result is NaN.
If the argument is positive infinity, then the result is positive infinity.
If the argument is negative infinity, then the result is positive zero.
Example

/*
*This example shows some special cases of exp() method in Java
*/
import java.lang.*; //importing java.lang package

public class JavaMathExample {
    public static void main(String[] args) {

        //Declaring variables
      
        double positiveInfinity = Double.POSITIVE_INFINITY;
        double negativeInfinity = Double.NEGATIVE_INFINITY;
        double nan = Double.NaN;
        double result;

        //Here argument is positive infinity, output will also be positive infinity
        result = Math.exp(positiveInfinity);
        System.out.println(result);

        //Here argument is negative infinity, output will be zero
        result = Math.exp(negativeInfinity);
        System.out.println(result);

        //Here argument is NaN, output will be NaN
        result = Math.exp(nan);
        System.out.println(result);


    }

}

Output:

Infinity
0.0
NaN