Java Math.expm1(double x) method returns ex – 1. Here, e is a Euler’s number, whose approximate value is 2.718281828459045.
public class JavaExample { public static void main(String[] args) { double x = 1; // returns e (2.718281828459045) to power of 1 minus 1 System.out.println(Math.expm1(x)); } }
Output:
1.718281828459045
Note: An interesting point to note is that for the values of x near 0, the sum of expm1(x) + 1 gives more accurate value of ex than exp(x) method.
Syntax of Math.expm1() method
Math.expm1(0); //returns 0.0
expm1() Description
public static double expm1(double x): Returns ex – 1.
expm1() Parameters
- x: A double value that is the exponent in computation of ex – 1.
expm1() Return Value
- Returns
e
raised to the power of given argumentx
, minus 1. - If the argument is NaN (Not a number), then it returns NaN.
- If the argument is positive infinity, then it returns positive infinity.
- If the argument is negative infinity, then it returns -1.0.
- If the argument is zero, then it returns zero with the same sign.
Example 1
public class JavaExample { public static void main(String[] args) { double x = 1, x2 = 10; System.out.println(Math.expm1(x)); System.out.println(Math.expm1(x2)); } }
Output:
Example 2
public class JavaExample { public static void main(String[] args) { double x = 2, x2 = -2, x3 = 0; System.out.println(Math.expm1(x)); System.out.println(Math.expm1(x2)); System.out.println(Math.expm1(x3)); } }
Output:
Example 3
public class JavaExample { public static void main(String[] args) { double x = Double.NaN; double x2 = Double.MAX_VALUE; double x3 = Double.MIN_VALUE; System.out.println(Math.expm1(x)); System.out.println(Math.expm1(x2)); System.out.println(Math.expm1(x3)); } }
Output: