Java Math exp()
exp() accepts a double value as an argument and returns the computed value of Euler’s number e raised raised to the power of the argument. The returned value is of type double.
Following is the syntax of exp() method.
double value = exp(double x)
Since the definition of exp() function has double datatype as argument, you can pass int, float or long as arguments as these datatypes will implicitly promote to double.
The approximate value of e is around 2.718281828459045. The Euler’s number is widely used in scientific calculations.
We shall learn about some of the special cases for exp() method with examples.
Example 1 – Math.exp(double)
In the following example, we use exp() method to find the value of e raised to the power of 4.5 .
Java Program
public class MathExample {
public static void main(String[] args) {
double x = 4.5;
double e_x = Math.exp(x);
System.out.println(e_x);
}
}
Output
90.01713130052181
Example 2 – Math.exp(int)
In the following example, we pass an int value for the argument to exp() method.
Java Program
public class MathExample {
public static void main(String[] args) {
int x = 2;
double e_x = Math.exp(x);
System.out.println(e_x);
}
}
Output
7.38905609893065
Similarly, you can provide a float or long value as argument to exp() method.
Example 3 – Math.exp(NaN)
In the following example, we pass Double.NaN as argument to exp() method. As per the definition of the exp() in Math class, the method should return NaN value.
Java Program
public class MathExample {
public static void main(String[] args) {
double x = Double.NaN;
double e_x = Math.exp(x);
System.out.println(e_x);
}
}
Output
NaN
Example 4 – Math.exp() – With Positive Infinity as Argument
In the following example, we pass Double.POSITIVE_INFINITY as argument to exp() method. As per the definition of the exp() method in Math class, the method should return positive infinity.
Java Program
public class MathExample {
public static void main(String[] args) {
double x = Double.POSITIVE_INFINITY;
double e_x = Math.exp(x);
System.out.println(e_x);
}
}
Output
Infinity
Example 5 – Math.exp() – With Negative Infinity as Argument
In the following example, we pass Double.POSITIVE_INFINITY as argument to exp() method. As per the definition of the exp() method in Math class, the should return 0.0 for negative infinity as input value.
Java Program
public class MathExample {
public static void main(String[] args) {
double x = Double.NEGATIVE_INFINITY;
double e_x = Math.exp(x);
System.out.println(e_x);
}
}
Output
0.0
Conclusion
In this Java Tutorial, we learned about Java Math.exp() function, with example programs.