Open In App

Java Math expm1() Method

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

In Java, the Math.expm1() method is used to calculate e^x - 1. This method is used when we have to deal with small values of x close to zero, as it avoids errors that can occur when calculating Math.exp(x) - 1.

Note: e is the base of natural logarithms, and x is the argument passed to the method.

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, then 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 -1.0.
  • If the argument is zero, then the result is 0.0 with the same sign as the argument.

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

Syntax of expm1() Method

public static double expm1(double x)

  • Parameter: This method takes a single parameter x the exponent part which raises to e.
  • Return Type: This method returns e^x-1 where, e is the base of the natural logarithms.

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


Examples of Java Math expm1() Method

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

Java
// Java program to demonstrate 
// the working of exmp1() method
import java.lang.Math;

public class Geeks {
    
    public static void main(String args[]) {
        
        double x = 2;
        // when x is a regular number
        double res = Math.expm1(x);
        System.out.println("expm1(2): " + res);  
    }
}

Output
expm1(2): 6.38905609893065

Explanation: Here, we are calculating the e^x-1 for a regular number 2, with the help of expm1() method.

Example 2: In this example, we will see how the expm1() method handles special cases like positive infinity, negative infinity and zero.

Java
// Java program to demonstrate handling 
// positive infinity, negative infinity and zero
import java.lang.Math;

public class Geeks {
    
    public static void main(String args[]) {
        
        double p = Double.POSITIVE_INFINITY;
        double n = Double.NEGATIVE_INFINITY;
        double x = 0;

        // when x is positive infinity
        double res = Math.expm1(p);
        System.out.println("expm1(+INF): " + res);

        // when x is negative infinity
        res = Math.expm1(n);
        System.out.println("expm1(-INF): " + res);

        // when x is zero
        res = Math.expm1(x);
        System.out.println("expm1(0): " + res);
    }
}

Output
expm1(+INF): Infinity
expm1(-INF): -1.0
expm1(0): 0.0

Practice Tags :

Similar Reads