In Java, Math.expm1(x) returns ex - 1, the same value as Math.exp(x) - 1, but computed in a way that stays accurate when x is close to zero. The name means "exp minus 1".
Use it for tiny rates, small percentage changes, and any formula where you subtract 1 from an exponential. It was added in Java 5 (JDK 1.5).
A static method of the java.lang.Math class, added in Java 5 (JDK 1.5). Call it on the class name, as in Math.expm1(...). No import is needed, because classes in java.lang are always visible.
Syntax
Pass the exponent; the result is a double.
Java
double small = Math.expm1(1e-10); // about 1.0E-10
double zero = Math.expm1(0); // 0.0
double pct = Math.expm1(logReturn) * 100; // percent change
Call
What it does
Math.expm1(0)
Returns 0.0.
Math.expm1(1)
About 1.71828 (e minus 1).
Math.expm1(-Infinity)
Returns -1.0.
Math.expm1(r) * 100
Converts a log return r into a percent change.
For values of x far from zero, expm1(x) and exp(x) - 1 are equally good; the benefit appears when exp(x) is so close to 1 that its digits cannot hold the small difference.
Library declarations
Java
// java.lang.Math (Java 5+)
public static double expm1(double x)
Parameters
Parameter
Description
x
The exponent whose ex - 1 you want.
Return Value
Return Value
Description
double
ex - 1. Infinity for Infinity, -1.0 for -Infinity, NaN for NaN, and a zero keeps its sign.
A double has about 16 digits. The gap between 1.0 and the next double is about 2.2e-16, so the tiny 1e-16 is lost the moment the exponential is rounded to 1.0.
The loss is permanent. Subtracting 1 from 1.0 gives exactly 0.0, which hides the true answer, while expm1 never forms the number just above 1 and keeps the tiny positive result.
Scaling check. For y = 1e-10 the true value is just over 1e-10, so multiplying by 1e10 gives about 1.000000; the special inputs return -1.0 and an unchanged negative zero.
Example 2 — Measure the Error of the Naive Formula
For a growth rate of 1e-12, a good result should be very close to the rate itself. Test both formulas against that.
Java
public class MathExpm1Naive {
public static void main(String[] args) {
double rate = 1e-12;
double naive = Math.exp(rate) - 1;
double exact = Math.expm1(rate);
System.out.println("Same digits? " + (Math.abs(naive - exact) < 1e-20));
System.out.println("expm1 close to rate? " + (Math.abs(exact / rate - 1) < 1e-9));
System.out.println("naive close to rate? " + (Math.abs(naive / rate - 1) < 1e-9));
}
}
Output
Same digits? false
expm1 close to rate? true
naive close to rate? false
How It Works
What a tiny ex - 1 should be. For very small x, ex - 1 is almost exactly x (plus a term about x * x / 2), so exact / rate should be about 1.
expm1 passes. The ratio differs from 1 by only about 5e-13, far inside the 1e-9 limit, so the second line is true.
The naive result is visibly off.Math.exp(rate) is stored in steps of 2.2e-16, so subtracting 1 leaves a value that is wrong by almost 0.01 percent, far above the 1e-9 limit, and the third line is false.
Example 3 — Convert a Log Return Into a Percent Change
Stock analysts often store daily changes as log returns. expm1(r) turns one back into an ordinary percentage.
Java
import java.util.Locale;
public class MathExpm1Returns {
public static void main(String[] args) {
double[] logReturns = {0.05, -0.05, 0.0};
for (double r : logReturns) {
double percent = Math.expm1(r) * 100;
System.out.println("log return " + r + " -> " + String.format(Locale.US, "%.2f", percent) + "%");
}
}
}
Growth factor minus one. A price that moves by a log return of 0.05 is multiplied by e to the 0.05, about 1.0513, which is a rise of 5.13 percent.
Losses are smaller than they look. A log return of -0.05 gives a factor of about 0.9512, a fall of 4.88 percent, a bit less than 5.
Zero stays zero.expm1(0.0) is exactly 0.0, which %.2f prints as 0.00.
Frequently Asked Questions
It returns e raised to the power x, minus 1: the same as Math.exp(x) - 1, but accurate for values of x close to zero.
For tiny x, Math.exp(x) is so close to 1.0 that the small difference is lost during rounding. expm1 avoids that loss. For example, Math.exp(1e-16) - 1 is 0.0 while Math.expm1(1e-16) is about 1.0E-16.
Java 5 (JDK 1.5). It is a final static method of java.lang.Math, so no import is needed.
It is -1.0, because e to the power minus infinity is 0 and 0 minus 1 is -1.
Math.log1p. Math.log1p(Math.expm1(x)) gives back x, apart from tiny rounding differences.