In Java, the Math.expm1() method is used to calculate e^x - 1. This method is used when we have to deal with small values of x close to zero, as it avoids errors that can occur when calculating Math.exp(x) - 1.
Note: e is the base of natural logarithms, and x is the argument passed to the method.
In this article, we are going to discuss how this method works for regular values and for special cases such as infinity and NaN.
Special Cases:
- If the argument is NaN, then the result is NaN.
- If the argument is positive infinity, then the result is positive infinity.
- If the argument is negative infinity, then the result is -1.0.
- If the argument is zero, then the result is 0.0 with the same sign as the argument.
These special cases make sure that the Math.expm1() methods work correctly.
Syntax of expm1() Method
public static double expm1(double x)
- Parameter: This method takes a single parameter x the exponent part which raises to e.
- Return Type: This method returns e^x-1 where, e is the base of the natural logarithms.
Now, we are going to discuss some examples for better understanding.
Examples of Java Math expm1() Method
Example 1: In this example, we will see the basic usage of expm1() method with regular number.
Java
// Java program to demonstrate
// the working of exmp1() method
import java.lang.Math;
public class Geeks {
public static void main(String args[]) {
double x = 2;
// when x is a regular number
double res = Math.expm1(x);
System.out.println("expm1(2): " + res);
}
}
Outputexpm1(2): 6.38905609893065
Explanation: Here, we are calculating the e^x-1 for a regular number 2, with the help of expm1() method.
Example 2: In this example, we will see how the expm1() method handles special cases like positive infinity, negative infinity and zero.
Java
// Java program to demonstrate handling
// positive infinity, negative infinity and zero
import java.lang.Math;
public class Geeks {
public static void main(String args[]) {
double p = Double.POSITIVE_INFINITY;
double n = Double.NEGATIVE_INFINITY;
double x = 0;
// when x is positive infinity
double res = Math.expm1(p);
System.out.println("expm1(+INF): " + res);
// when x is negative infinity
res = Math.expm1(n);
System.out.println("expm1(-INF): " + res);
// when x is zero
res = Math.expm1(x);
System.out.println("expm1(0): " + res);
}
}
Outputexpm1(+INF): Infinity
expm1(-INF): -1.0
expm1(0): 0.0
Similar Reads
Java Math expm1() method with Example In Java, the Math.expm1() method is used to calculate e^x - 1. This method is used when we have to deal with small values of x close to zero, as it avoids errors that can occur when calculating Math.exp(x) - 1.Note: e is the base of natural logarithms, and x is the argument passed to the method.In t
2 min read
Java Math exp() method with Example The Math.exp() method is a part of the java.lang.Math class. This method is used to calculate the value of Euler's number e raised to the power of a specified exponent. In this article, we are going to discuss how this method works for regular values and for special cases such as infinity and NaN.Sp
2 min read
heapDump() Method of MBean in Java The heapDump() method in Java is a solid and powerful tool for troubleshooting and analyzing the performance of Java Programs. MBeans, or Managed Beans, are the objects of Java that provide various information and control aspects of a Java Application, Such as memory usage, threading, and performanc
5 min read
Java Math addExact(int a, int b) method The java.lang.Math.addExact() is a built-in math function in java which returns the sum of its arguments. It throws an exception if the result overflows an int.As addExact(int a, int b) is static, so object creation is not required. Syntax : public static int addExact(int a, int b) Parameter : a : t
1 min read
StrictMath exp() Method in Java All methods of java.lang.StrictMath class : Set 1, Set 2 The java.lang.StrictMath.exp() is an inbuilt method in Java is used to return a Euler number raised to the power of the specified double value. It gives rise to three special results: The result is positive infinity when the given argument is
2 min read
Java Math getExponent() method with Example The java.lang.Math.getExponent() is a built-in math function which returns the unbiased exponent used in the representation of a float. If the argument is NaN or infinite, then the result is Float.MAX_EXPONENT + 1. If the argument is zero or subnormal, then the result is Float.MIN_EXPONENT -1. Synta
1 min read
Java Math expm1() Method
min read