Java exp Function

The Java exp Function is one of the Math functions, which is useful to return E raised to the power of double value. Where E is Euler’s number, and it is approximately equal to 2.71828.

Java exp Function Syntax

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

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

// In order to use in program: 
Math.exp(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.exp function will return the output.
  • If the number argument is not a number, the Java Math.exp function will return NaN.
  • When the number argument is positive infinity, the Math.exp function will return Positive Infinity as the output.
  • If it is negative infinity, the Math.exp function will return Positive Zero as output.

For example, if we specify the expression as Java Math.exp (2.00). It means e² ==> 2.718² ==> 7.38

Java exp Function Example

In this program, we use the Java Math.exp Function to calculate the power of Euler’s number E of both positive and negative values and display the output.

package MathFunctions;

public class ExpMethod {
	public static void main(String[] args) {
		double a = Math.exp(10.9666 - 14.9865 + 10.9852);
		System.out.println("Math.Exp Result = = " + a);	

		System.out.println("\nMath.Exp Result of Positive Number = " + Math.exp(4.25));
		System.out.println("Math.Exp Result of Positive Number = " + Math.exp(6.95));
		
		System.out.println("\nMath.Exp Result of Negative Number = " + Math.exp(-2.85));
		System.out.println("Math.Exp Result of Negative Number = " + Math.exp(-10.25));
		
		System.out.println("\nMath.Exp Result = " + Math.exp(1));
		System.out.println("Math.Exp Result = " + Math.exp(-1));
	}
}
Java exp Function 1

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

double a = Math.exp(10.9666 - 14.9865 + 10.9852);
System.out.println("Math.Exp Result = = " + a);

Next, we used the Java Math.exp Function directly on Positive double values. Here, Math.exp(4.25) means (2.71828)^4.25

System.out.println("\nMath.Exp Result of Positive Number = " + Math.exp(4.25));
System.out.println("Math.Exp Result of Positive Number = " + Math.exp(6.95));

Here, we used the Function directly on Negative double values.

System.out.println("\nMath.Exp Result of Negative Number = " + Math.exp(-2.85));
System.out.println("Math.Exp Result of Negative Number = " + Math.exp(-10.25));

Next, we used the Math.exp Function directly on both Positive and Negative double values.

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

Java exp on Array example

In this Java program, we return the Euler’s number E raised by the power of bulk data. Here, we are going to declare an array of double type and use the Java Math.exp function of array elements.

package MathFunctions;

public class ExpMethodOnArrays {
	public static void main(String[] args) {
		
		double [] myArray = {-1.69, 5.98, 4.21, -3.9999, 6.879, 4.4897};

		for (int i = 0; i < myArray.length; i++) {
			System.out.format("Math.Exp Result of Array Element = %.2f\n", Math.exp(myArray[i]));
		}
	}
}
Math.Exp Result of Array Element = 0.18
Math.Exp Result of Array Element = 395.44
Math.Exp Result of Array Element = 67.36
Math.Exp Result of Array Element = 0.02
Math.Exp Result of Array Element = 971.65
Math.Exp Result of Array Element = 89.09

We used the For Loop to iterate the Array. Within the exp For Loop, we initialized the i value as 0. Next, the compiler will check for the condition (i < myArray.length). As along the condition is True statement inside the for loop will be executed.

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

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

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

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

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

Java exp function on Arraylist example

In this Java program, we are going to declare an arraylist of double type and return the Euler’s number E raised by the power of list elements.

package MathFunctions;

import java.util.ArrayList;

public class ExpMethodOnArrayList {
	public static void main(String[] args) {
		
		ArrayList<Double> myList = new ArrayList<Double>(5);
		myList.add(-2.5);
		myList.add(2.5);
		myList.add(4.25);
		myList.add(-4.25);
		myList.add(5.00);
		
		for (double x : myList) {
			System.out.println("Math.Exp Result of ArrayList =  " + Math.exp(x));
		}
	}
}
Math.Exp Result of ArrayList =  0.0820849986238988
Math.Exp Result of ArrayList =  12.182493960703473
Math.Exp Result of ArrayList =  70.10541234668786
Math.Exp Result of ArrayList =  0.014264233908999256
Math.Exp Result of ArrayList =  148.4131591025766

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

for (double x : myList) {

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

System.out.println("Math.Exp Result of ArrayList =  " + Math.exp(x));