In Java, Math.exp(x) returns e raised to the power x, where e (Math.E, about 2.71828) is Euler's number. Math.exp(0) is 1.0.
The function describes smooth growth and decay: continuously compounded interest, population growth, radioactive decay, and cooling. It has been part of java.lang.Math since the early versions of Java, and Math.log is its inverse.
A static method of the java.lang.Math class, available since the early versions of Java (JDK 1.0). Call it on the class name, as in Math.exp(...). No import is needed, because classes in java.lang are always visible.
Syntax
Pass the exponent as any number. The result is a double.
Java
double one = Math.exp(0); // 1.0
double e = Math.exp(1); // about 2.71828
double value = start * Math.exp(rate * time); // growth
double left = start * Math.exp(-k * time); // decay
Call
What it does
Math.exp(0)
Returns exactly 1.0.
Math.exp(1)
About 2.71828, close to Math.E.
Math.exp(-x)
Shrinking factor between 0 and 1 for positive x.
Math.exp(Math.log(a) * b)
The idea behind pow(a, b) for positive a.
Math.E is the constant itself (2.718281828459045). exp(1) is allowed to be one unit in the last place away from it, so compare the two with a tolerance rather than ==.
Library declarations
Java
// java.lang.Math
public static double exp(double a)
Parameters
Parameter
Description
a
The exponent applied to e. Negative values give results between 0 and 1.
Return Value
Return Value
Description
double
e raised to the power a. Infinity for Infinity or when the answer is too big, 0.0 for -Infinity or when the answer is too small, NaN for NaN.
Zero exponent. Any base to the power 0 is 1, so exp(0) is 1.0; and exp(1) is e itself, shown here rounded to four decimals.
Tolerance, not equality. The two numbers are within a hair of each other (less than 1e-15), so the check prints true even if the last digit differs.
Both ends of the range. 710 is just above the overflow limit of about 709.78, so the result is Infinity; -746 is below the underflow limit of about -745.13, so it collapses to 0.0.
Example 2 — Continuous vs Yearly Compound Interest
2000 saved at 3 percent for 5 years: compare P * e^(r * t) with ordinary yearly compounding. Output uses an English-style decimal point.
Java
import java.util.Locale;
public class MathExpInterest {
public static void main(String[] args) {
double principal = 2000;
double rate = 0.03;
int years = 5;
double continuous = principal * Math.exp(rate * years);
double yearly = principal * Math.pow(1 + rate, years);
System.out.println("Continuous: " + String.format(Locale.US, "%.2f", continuous));
System.out.println("Yearly: " + String.format(Locale.US, "%.2f", yearly));
}
}
Output
Continuous: 2323.67
Yearly: 2318.55
How It Works
One combined exponent. The rate and time are multiplied first (0.03 * 5 = 0.15), so the balance grows by the factor exp(0.15), about 1.1618.
Slightly more than yearly. Yearly compounding multiplies by 1.03 five times, about 1.1593; interest that is added continuously always ends a little higher.
Rounded for display. 2000 * 1.1618 is about 2323.67 and 2000 * 1.1593 is about 2318.55; %.2f keeps two decimals, and Locale.US guarantees the dot.
Example 3 — Half-Life: How Much Is Left?
A sample of 100 grams has a half-life of 10 days. Use exp with the decay constant ln(2) / halfLife to track the rest.
Java
public class MathExpDecay {
public static void main(String[] args) {
double start = 100;
double halfLife = 10;
double k = Math.log(2) / halfLife;
for (int days = 0; days <= 30; days += 10) {
double left = start * Math.exp(-k * days);
System.out.println("Day " + days + ": " + Math.round(left * 100) / 100.0);
}
}
}
Output
Day 0: 100.0
Day 10: 50.0
Day 20: 25.0
Day 30: 12.5
How It Works
Decay constant.k = ln(2) / 10 is chosen so that after exactly one half-life the factor exp(-k * 10) equals exp(-ln 2), which is 1/2.
Halving every 10 days. Each extra 10 days multiplies by another 1/2, so the amounts are 100, 50, 25, and 12.5 grams.
Tidying the output. The raw values can carry tiny errors such as 50.00000000000001, so Math.round(left * 100) / 100.0 keeps two decimals and prints clean numbers.
Frequently Asked Questions
It returns e raised to the power of its argument, where e is about 2.71828. Math.exp(0) is 1.0.
exp always uses the base e, while pow accepts any base. Math.exp(x) is the same idea as Math.pow(Math.E, x), but it is usually more accurate and faster.
Use principal * Math.exp(rate * years). For a 3 percent rate, write 0.03 for the rate.
Above about 709.78 it returns Infinity, and below about -745.13 it returns 0.0. It never throws an exception.
Use Math.log, the natural logarithm: Math.log(Math.exp(x)) gives back x, apart from tiny rounding differences.