In Java, Math.log1p(x) returns ln(1 + x), the natural logarithm of 1 + x. It gives the same answer as Math.log(1 + x) for ordinary values, but stays accurate when x is very close to zero. The name means "log of 1 plus".
Use it for small interest rates, small percentage changes, and growth rates that are stored as logarithms. 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.log1p(...). No import is needed, because classes in java.lang are always visible.
Syntax
Pass x, not 1 + x. The result is a double.
Java
double a = Math.log1p(0); // 0.0
double b = Math.log1p(0.05); // ln(1.05), about 0.0488
double c = Math.log1p(-1); // -Infinity
double rate = Math.log1p(percent / 100);
Call
What it does
Math.log1p(0)
Returns 0.0.
Math.log1p(1)
About 0.6931 (the natural log of 2).
Math.log1p(-1)
Returns -Infinity.
Math.log1p(-2)
Returns NaN.
Pass the rate, never 1 + rate: Math.log1p(1 + 0.05) would compute ln(2.05), which is not what you want.
Library declarations
Java
// java.lang.Math (Java 5+)
public static double log1p(double x)
Parameters
Parameter
Description
x
The value to which 1 is added inside the logarithm. It must be greater than -1 for a normal result.
Return Value
Return Value
Description
double
The natural logarithm of 1 + x. NaN if x is less than -1 or NaN; -Infinity if x is exactly -1; Infinity for Infinity; a zero keeps its sign.
Zero in, zero out. The natural log of 1 + 0 is 0.0, and a negative zero is returned with its sign unchanged.
The log of 2.log1p(1) is ln(1 + 1), which is 0.6931 after rounding to four decimals.
The domain starts at -1. At -1 the inside value is 0, whose logarithm is -Infinity; at -2 the inside value is -1, which has no real logarithm, giving NaN.
Example 2 — Where log(1 + x) Loses the Answer
Compare the two formulas for x = 1e-16 and x = 1e-10. Output uses an English-style decimal point.
The addition destroys the input. A double cannot tell 1.0000000000000001 from 1.0, so 1 + x is exactly 1.0 and its logarithm is 0.0.
log1p never adds. It works from x directly, so the tiny positive answer, about 1e-16, survives.
Visible error at 1e-10. The sum 1 + y is stored with a small rounding error, which shows up in the 8th decimal (1.00000008); log1p returns 1.00000000 after scaling, as the true value should.
Example 3 — Average Growth When Gains and Losses Alternate
A fund gains 10 percent one year and loses 10 percent the next. The average of logarithms gives the true yearly rate.
Plain average: 0.00%
Real average: -0.50%
1000 becomes: 990.00
How It Works
Logs turn multiplying into adding. Growth factors of 1.10 and 0.90 multiply, so log1p(r) converts each rate to a logarithm that can simply be summed.
Back to a percent. The mean logarithm is about -0.00503; expm1 converts it to a yearly rate of -0.50 percent, which is what each year really contributed.
The proof. 1000 times 1.10 times 0.90 is 990, so money shrinks even though the plain average of +10 and -10 is 0.00 percent.
Frequently Asked Questions
It returns the natural logarithm of 1 + x, accurate even for x very close to zero. Math.log1p(0) is 0.0.
For tiny x, 1 + x is rounded to 1.0 before the logarithm is taken, so Math.log(1 + x) returns 0.0. log1p keeps the real answer.
It returns -Infinity, because 1 + (-1) is 0. For x below -1, the result is NaN.
Java 5 (JDK 1.5). It is a static method of java.lang.Math, so no import is needed.
Math.expm1. Math.expm1(Math.log1p(x)) gives back x, apart from tiny rounding differences.