The Math class contains methods to work with mathematical functions. In this article, we are going to talk about the Math.exp() method in Java. It returns the number e raised to the power of a double value.
The processes that obey the exponential law have one common property: for the same time interval, their parameters change the same number of times. For example, the cooling of a liquid: the greater the temperature difference between air and liquid, the faster it cools. The larger the snowball rolling down the mountain becomes, the faster it rolls down.
![Math.exp() method in Java - 2]()
What is an exponential function: a very short math introduction
Note: This section explains the mathematics behind the Math.exp() method. If you already know this, or just want to use the method without understanding the essence, feel free to move on to the next point. The exponent is the function y = ex, where e is a tricky mathematical number that is roughly 2.718281828459045. This number is as important as the famous pi number, but it is known mainly by mathematicians, programmers and people working with statistics. By the way, it has a name: Euler's number. Also e is the base of natural logarithm. Here is the exponential function graph:
Math.exp() method in Java
Now let's get back to Java. The double exp(double x) method of the Math class calculates the value of the exponent function at the point x, in other words, it returns the number e to the power of x. More precisely, it returns an approximate value with a certain precision. Returns Euler's number e raised to the power of a double value. That is, Math.exp(2.0) = e2.0 (roughly it’s 7.34) Here is a declaration of the method:
double exp(double x)
Where x is the degree to raise the number e. Let's give an example.
public class ExpExample {
public static void main(String[] args) {
int x1 = 2;
double x2 = 0.5;
double x3 = 1;
System.out.println("exponential function in " + x1 + " = " + Math.exp(x1));
System.out.println("exponential function in " + x2 + " = " + Math.exp(x2));
System.out.println("exponential function in " + x3 + " = " + Math.exp(x3));
}
}
The output is:
exponential function in 2 = 7.38905609893065
exponential function in 0.5 = 1.6487212707001282
exponential function in 1.0 = 2.718281828459045
Some special cases
In mathematics there are concepts of indeterminate form, as well as positive and negative infinity. A positive number divided by 0.0 gives positive infinity, and a negative number gives negative infinity. You can get indeterminate form in different ways. For example, if you try to divide zero by zero or infinity to infinity. In Java there are special constants from class Double such as Double.NaN (somewhat indeterminate form), Double.POSITIVE_INFINITY and Double.NEGATIVE_INFINITY. Math.exp() method behaves in a specific way when faced with these three concepts:- If the argument is NaN, the result is also NaN.
- If the argument is positive infinity, then the result is also positive infinity.
- If the argument is negative infinity, then the result is positive zero.
public class ExpSpecialCases {
public static void main(String[] args) {
double positiveInfinity = Double.POSITIVE_INFINITY;
double negativeInfinity = Double.NEGATIVE_INFINITY;
double nan = Double.NaN;
//The argument is positive infinity, the output is positive infinity
System.out.println(Math.exp(positiveInfinity));
//The argument is negative infinity, the output is zero
System.out.println(Math.exp(negativeInfinity));
//The argument is NaN, the output is NaN
System.out.println(Math.exp(nan));
}
}
The output is:
Infinity
0.0
NaN
GO TO FULL VERSION