Java expm1 Function

The Java expm1 Function is one of the Java Math functions, which is to return E raised to the power of double value and subtracts One from it. Here, E is Euler’s number, and it is approximately equal to 2.71828.

Java expm1 Function Syntax

The basic syntax of the Math.expm1 in Java Programming language is as shown below.

static double expm1(double number); //Return Type is Double

// In order to use in program: 
Math.expm1(double number);

Number: It can be a double value or a valid numerical expression, and it represents the exponent value.

  • If the number argument is a positive or negative double value, the Math.expm1 function will return the output.
  • If the number argument is not a number, the Math.expm1 function will return NaN.
  • When the number argument is positive infinity, the Java expm1 function will return Positive Infinity as the output.
  • If it is negative infinity, the Math.expm1 function will return -1.0 as output.

For example, If we specify, Math.expm1 (2.00); It means e² – 1 ==> 2.718² – 1 ==> 6.38

Java expm1 Function Example

The Java Math.expm1 Function calculates the power of Euler’s number E and subtracts one from it. In this Java program, we are going to find the Power of E minus 1 of both positive and negative values and display the output.

// Java Math.expm1 Function

package MathFunctions;

public class Expm1Method {
	public static void main(String[] args) {
		double a = Math.expm1(5.12 - 4.65 + 3.21);
		System.out.println("Math.Expm1 Result = = " + a);	

		System.out.println("\nMath.Expm1 Result of Positive Number = " + Math.expm1(10.45));
		System.out.println("Math.Expm1 Result of Positive Number = " + Math.expm1(9.52));
		
		System.out.println("\nMath.Expm1 Result of Negative Number = " + Math.expm1(-8.50));
		System.out.println("Math.Expm1 Result of Negative Number = " + Math.expm1(-4.25));
		
		System.out.println("\nMath.Expm1 Result = " + Math.expm1(1));
		System.out.println("Math.Expm1 Result = " + Math.expm1(-1));
	}
}
Java expm1 Function 1

First, we declared a variable of type Double and performed the Math.expm1 function directly on expression.

double a = Math.expm1(5.12 - 4.65 + 3.21);
System.out.println("Math.Expm1 Result = = " + a);

Next, we used the Math.expm1 Function directly on Positive double values. Here, Math.expm1(10.45) means (2.71828)^10.45 – 1

System.out.println("\nMath.Expm1 Result of Positive Number = " + Math.expm1(10.45));
System.out.println("Math.Expm1 Result of Positive Number = " + Math.expm1(9.52));

Next, we used the Java Math.expm1 Function directly on Negative double values.

System.out.println("\nMath.Expm1 Result of Negative Number = " + Math.expm1(-8.50));
System.out.println("Math.Expm1 Result of Negative Number = " + Math.expm1(-4.25));

Last, we used the expm1 Function directly on both Positive and Negative double values.

System.out.println("\nMath.Expm1 Result = " + Math.expm1(1));
System.out.println("Math.Expm1 Result = " + Math.expm1(-1));

Java expm1 on Array example

In this Java program, we use Math.expm1 function on array items. Here, we are going to declare an array of double type and using the Java Math.expm1 function of array elements.

public class Expm1MethodOnArrays {
	public static void main(String[] args) {
		
		double [] myArray = {2.35, 4.98, -8.41, -9.5987, 12.38, 14.4897};

		for (int i = 0; i < myArray.length; i++) {
			System.out.format("Math.Expm1 Result of Array Element = %.2f\n", Math.expm1(myArray[i]));
		}
	}
}
Math.Expm1 Result of Array Element = 9.49
Math.Expm1 Result of Array Element = 144.47
Math.Expm1 Result of Array Element = -1.00
Math.Expm1 Result of Array Element = -1.00
Math.Expm1 Result of Array Element = 237992.82
Math.Expm1 Result of Array Element = 1962440.66

We used the Java For Loop to iterate the Array. Within the expm1 For Loop, we initialized the i value as 0. Next, the compiler will check for the condition (i < myArray.length).

TIP: myArray.length finds the length of an array.

for (int i = 0; i < myArray.length; i++) {

Here, we used the expm1 Math function directly inside the System.out.format statement. Next, the Javac compiler will call the Math.expm1 method ( static double expm1(double number) ) to find the corresponding exp value and prints the output.

System.out.format("Math.Expm1 Result of Array Element = %.2f\n", Math.expm1(myArray[i]));

NOTE: To use Math.expm1 on single item then use: Math.expm1(myArray[index_position])

Java expm1 function on Arraylist example

In this math expm1 program, we are going to declare an ArrayList of double type and return the Math.expm1 function result of list elements.

package MathFunctions;

import java.util.ArrayList;

public class Expm1MethodOnArrayList {
	public static void main(String[] args) {		
		ArrayList<Double> myList = new ArrayList<Double>(5);
		myList.add(-1.5);
		myList.add(1.5);
		myList.add(2.45);
		myList.add(-2.45);
		myList.add(6.00);
		
		for (double x : myList) {
			System.out.println("Math.Expm1 Result of ArrayList =  " + Math.expm1(x));
		}
	}
}
Math.Expm1 Result of ArrayList =  -0.7768698398515702
Math.Expm1 Result of ArrayList =  3.481689070338065
Math.Expm1 Result of ArrayList =  10.588346719223392
Math.Expm1 Result of ArrayList =  -0.9137064135006295
Math.Expm1 Result of ArrayList =  402.4287934927351

We used the For Loop to iterate the double values in ArrayList

for (double x : myList) {

Here, the Javac compiler will call the math.expm1 method ( static double expm1(double x) ) to find the corresponding expm1 value and print the output.

System.out.println("Math.Expm1 Result of ArrayList =  " + Math.expm1(x));