Open In App

Java Math exp() Method

Last Updated : 13 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The Math.exp() method is a part of the java.lang.Math class. This method is used to calculate the value of Euler's number e raised to the power of a specified exponent.

In this article, we are going to discuss how this method works for regular values and for special cases such as infinity and NaN.

Special Cases:

  • 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.

These special cases make sure that the Math.exp() methods work correctly.

Syntax of exp() Method

public static double exp(double a)

  • Parameter: This method takes a single parameter a, of type double, which represents the exponent.
  • Return Type: This method returns ea, where e is the base of the natural logarithms.

Now, we are going to discuss some examples for better understanding.


Examples of Java Math exp() Method

Example 1: In this example, we will see the basic usage of exp() method with regular values.

Java
// Java program to demonstrate working of
// Math.exp() method with regular values
import java.lang.Math;

class Geeks {
    
    public static void main(String args[]) {
        // Test values
        double x = 5;

        // Regular number
        double res = Math.exp(x);
        System.out.println("exp(5) = " + res);
    }
}

Output
exp(5) = 148.4131591025766

Explanation: Here, we are calculating the exponential of regular number with the help of exp() method. We have declared a variable x with a value 5 and for this number we are calculating the exponential value.

Example 2: In this example, we will see how exp() method handles the NaN and Infinity cases.

Java
// Java program to demonstrate 
// handling of NaN and Infinity
import java.lang.Math;

class Geeks {
    
    public static void main(String args[]) {
        
        // Edge cases
        double p = Double.POSITIVE_INFINITY;
        double n = Double.NEGATIVE_INFINITY;
        double nan = Double.NaN;

        // Testing NaN
        double res = Math.exp(nan);
        System.out.println("exp(NaN) = " + res);  

        // Testing positive infinity
        res = Math.exp(p);
        System.out.println("exp(+∞) = " + res);  

        // Testing negative infinity
        res = Math.exp(n);
        System.out.println("exp(-∞) = " + res); 
    }
}

Output
exp(NaN) = NaN
exp(+∞) = Infinity
exp(-∞) = 0.0

Explanation: Here, we are handling the special cases such as NaN and infinity.


Note: This method is very useful when we have to do interest calculations and also used in machine learning algorithm.


Next Article
Practice Tags :

Similar Reads