Java – Find e Raise to Power x – 1 – expm1() Method with Examples

java.lang.Math.expm1() method is used to find the e^x -1 of a double value in Java for the given input (x – parameter).
Note that for values of x near 0, the exact sum of expm1(x) + 1 is much closer to the true result of ex than exp(x).
The computed result must be within 1 ulp of the exact result. Results must be semi-monotonic. The result of expm1 for any finite input must be greater than or equal to -1.0. Note that once the exact result of ex – 1 is within 1/2 ulp of the limit value -1, -1.0 should be returned.
Search on Google about function notaion

expm1() method exists in Math class of java.lang package.
Since: JDK1.5

 

Declaration of expm1() Method

The declaration of expm1() method is:

public static double expm1(double x)

 

Syntax of expm1() Method

The syntax of expm1() method in Java is:

 
java.lang.Math.expm1(double x)

Example

/*
*Find the ex -1 of given input(double) without importing the 
java.lang Package.
*/
public class JavaMathExample {
    public static void main(String[] args) {
        //Declaring variables
        double x = 9;
        double result;
        //Assign ex -1 of x to result variable
        result = java.lang.Math.expm1(x);
        //Printing result to the console
        System.out.println(result);
    }
}

Output:

8102.083927575384

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

/*
*Find the ex -1 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 = 25;
        double result;
        
        //Assign ex -1 of x to result variable
        result = Math.expm1(x);
        
        //Printing result to the console
        System.out.println(result);
    }
}

Output:

7.200489933638588E10

 

Parameters of expm1() 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 expm1() 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.expm1(x);
        System.out.println(result);

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


    }

}

Output:
Error

 

Return Value of expm1() Method in Java

expm1() method returns the computed value of e^x -1 of a double value for the given input (x – parameter).the value ex – 1.

 

Java expm1() 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("ex -1 of a Negative Value: "+Math.expm1(negativeValue));
        System.out.println("ex -1 of a Positive Value: "+Math.expm1(positiveValue));
        System.out.println("ex -1 of Zero: "+Math.expm1(zero));

    }
}

Output:

ex -1 of a Negative Value: -0.9999999936129925
ex -1 of a Positive Value: 1.1680887709814473E12
ex -1 of Zero: 0.0

 

Java expm1() 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 ex -1 of each element of the array
        for (int i=0; i < test.length; i++) {
            System.out.println(  Math.expm1(test[i])  );
        }

    }
}

Output:

Infinity
4.9E-324
NaN
-1.0
4.2867000000918794E-11
2.083198183463838E7
0.0
-0.9242259959771545
-1.0

 

Special Cases of expm1() 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 -1.0.
If the argument is zero, then the result is a zero with the same sign as the argument.
Example

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

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

        //Declaring variables
        double positiveZero = 0;
        double negativeZero = -0;
        double positiveInfinity = Double.POSITIVE_INFINITY;
        double negativeInfinity = Double.NEGATIVE_INFINITY;
        double nan = Double.NaN;
        double result;

        //Here argument is positive zero, output will also be positive zero
        result = Math.expm1(positiveZero);
        System.out.println(result);

        //Here argument is negative zero, output will be positive zero
        result = Math.expm1(negativeZero);
        System.out.println(result);

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

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

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


    }

}

Output:

0.0
0.0
Infinity
-1.0
NaN