Tutorial Gateway

  • C Language
  • Java
  • R
  • SQL
  • MySQL
  • Python
  • BI Tools
    • Informatica
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • QlikView
  • Js

Java exp Function

by suresh

The Java exp Function is one of the Java Math Library function which is used to return E raised to the power of double value, Where E is Euler’s number and it is approximately equal to 2.71828. In this article we will show you, How to use Math.exp in Java Programming language with example.

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 represent the exponent value.

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

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

Java exp Function Example

The Java Math.exp Function is used to calculate the power of Euler’s number E.

In this exp Java program, We are going to find the same with both positive and negative values and display the output.

JAVA CODE

// Java Math.exp Function

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));
	}
}

OUTPUT

Java exp Function 1

ANALYSIS

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

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

Next, We used the 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 Java exp 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 Java 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 will show you, How to 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 using Java Math.exp function of an array elements.

JAVA CODE

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]));
		}
	}
}

OUTPUT

Java exp Function 2

ANALYSIS

First, We declared an Array of double type and assigned some random values.

double [] myArray = {-1.69, 5.98, 4.21, -3.9999, 6.879, 4.4897};

Next, We used the Java For Loop to iterate the Array. Within the For Loop, we initialized the i value as 0. Next, 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 is used to find the length of an array.

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

Below statement will print the output. If you observe the code snippet, we used the Math.exp Function directly inside the System.out.format statement. Here, compiler will call the Math.exp method ( static double exp(double number) ) to find the corresponding exp value.

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

NOTE: If you want to use Math.exp on 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.

JAVA CODE

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));
		}
	}
}

OUTPUT

Java exp Function 3

ANALYSIS

First, We declared an ArrayList of double type and assigned some random values.

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);

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

for (double x : myList) {

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

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

Thank You for Visiting Our Blog

Placed Under: Java Tutorial

Stay in Touch!

Sign Up to receive Email updates on Latest Tutorials

  • C Programs
  • Java Programs
  • SQL FAQ’s
  • Python Programs
  • SSIS
  • Tableau
  • JavaScript

Copyright © 2019 | Tutorial Gateway· All Rights Reserved by Suresh

Home | About Us | Contact Us | Privacy Policy