The Math.log1p() method returns the natural logarithm (base e) of the sum of its argument and 1, computing ln(1 + x) with high precision.
Preventing Floating-Point Round-Off Loss: When x is near zero, adding 1.0 + x directly loses precision before Math.log() is called. Math.log1p(x) preserves accuracy for tiny fractional values. For base-10 logarithms, see Math.log10(). For exponential functions, explore Math.exp() and Math.pow().
Defined in java.lang.Math:
// Formal signature of Math.log1p() in java.lang.Math
public static double log1p(double x)
| Signature Element | Type | Description & Operational Rule |
|---|---|---|
x |
double |
The value added to 1.0 before computing the natural logarithm; valid for x > -1.0. |
| Return Value | double |
The value of ln(1 + x); returns -Infinity at -1.0, NaN for < -1.0. |
| Small-Value Rule | Taylor Expansion | For tiny x where |x| < 2^-53, returns x directly with full significand accuracy. |
For large or moderate numbers, Math.log1p(x) matches Math.log(1.0 + x):
// Comparing Math.log1p(x) with Math.log(1 + x) for standard numbers
System.out.println("log1p(0.0): " + Math.log1p(0.0)); // 0.0 (ln(1))
System.out.println("log1p(4.0): " + Math.log1p(4.0)); // 1.6094379124341003 (ln(5))
System.out.println("log(5.0): " + Math.log(5.0)); // 1.6094379124341003
System.out.println("log1p(Math.E - 1):" + Math.log1p(Math.E - 1)); // 1.0 (ln(e))Terminal Output:
log1p(0.0): 0.0
log1p(4.0): 1.6094379124341003
log(5.0): 1.6094379124341003
log1p(Math.E - 1):1.0
When x is smaller than IEEE 754 machine epsilon (2.22e-16), 1.0 + x rounds to 1.0, causing Math.log(1.0 + x) to return 0.0. Math.log1p(x) retains exact precision:
// Near-zero precision: where Math.log(1.0 + x) fails completely
double tiny = 1.0e-16; // Below IEEE 754 double machine epsilon (2.22e-16)
// 1.0 + 1.0e-16 rounds to 1.0 in standard floating-point addition!
System.out.println("1.0 + tiny: " + (1.0 + tiny)); // 1.0
System.out.println("Math.log(1 + tiny): " + Math.log(1.0 + tiny)); // 0.0 (100% precision loss!)
System.out.println("Math.log1p(tiny): " + Math.log1p(tiny)); // 1.0E-16 (Exact!)Terminal Output:
1.0 + tiny: 1.0
Math.log(1 + tiny): 0.0
Math.log1p(tiny): 1.0E-16
Because the inner expression is 1 + x, the vertical asymptote shifts from 0.0 to -1.0:
// Domain boundaries: x = -1.0 yields -Infinity, x < -1.0 yields NaN
System.out.println("log1p(-0.5): " + Math.log1p(-0.5)); // -0.6931471805599453 (ln(0.5))
System.out.println("log1p(-1.0): " + Math.log1p(-1.0)); // -Infinity (ln(0))
System.out.println("log1p(-1.5): " + Math.log1p(-1.5)); // NaN (undefined for 1+x < 0)
System.out.println("log1p(NaN): " + Math.log1p(Double.NaN)); // NaNTerminal Output:
log1p(-0.5): -0.6931471805599453
log1p(-1.0): -Infinity
log1p(-1.5): NaN
log1p(NaN): NaN
Pairing Math.log1p(x) with Math.expm1(x) (ex - 1) enables lossless round-trip calculations at sub-epsilon scales:
// Exact round-trip pairing with Math.expm1(x) = e^x - 1
double microRate = 2.5e-17;
double expMinus1 = Math.expm1(microRate);
double recovered = Math.log1p(expMinus1);
System.out.println("Original microRate: " + microRate); // 2.5E-17
System.out.println("Recovered via log1p:" + recovered); // 2.5E-17Terminal Output:
Original microRate: 2.5E-17
Recovered via log1p:2.5E-17
Selecting the right logarithmic method in Java:
| Method | Mathematical Formula | Asymptote (-Infinity) | Primary Engineering Use Case |
|---|---|---|---|
Math.log1p(x) |
ln(1 + x) |
x = -1.0 | Small interest rates, ML loss functions, numerical analysis. |
Math.log(x) |
ln(x) |
x = 0.0 | General natural logarithms where x is not added to 1. |
Math.log10(x) |
log10(x) |
x = 0.0 | Decibels, pH scales, integer digit counting. |
Computing continuous log returns when per-tick or per-second interest rates are near zero:
public class MicroCompoundingCalculator {
public static double effectiveLogReturn(double perSecondRate, long seconds) {
// Total continuous log return over N seconds: N * ln(1 + r)
return seconds * Math.log1p(perSecondRate);
}
public static void main(String[] args) {
// Tiny per-second interest rate (1e-12 per second over 10 million seconds)
double logRet = effectiveLogReturn(1.0e-12, 10_000_000L);
System.out.println("Effective Log Return: " + logRet); // ~1.0E-5
}
}Terminal Output:
Effective Log Return: 9.999999999995E-6
Implementing the smooth ReLU approximation ln(1 + e^x) without underflow loss:
public class SoftplusActivation {
public static double softplus(double x) {
// Numerically stable softplus: ln(1 + e^x)
if (x > 20.0) return x; // Avoids exp(x) overflow for large positive x
return Math.log1p(Math.exp(x));
}
public static void main(String[] args) {
System.out.println("softplus(0.0): " + softplus(0.0)); // 0.6931471805599453 (ln(2))
System.out.println("softplus(-10.0): " + softplus(-10.0)); // ~4.5398899216870535E-5
}
}Terminal Output:
softplus(0.0): 0.6931471805599453
softplus(-10.0): 4.5398899216870535E-5
Evaluating Math.log(1.0 + rate) for sub-basis-point rates introduces severe rounding drift over millions of iterations. Always use Math.log1p(rate).
Do not add 1 manually! Calling Math.log1p(1.0 + x) computes ln(2 + x) and still suffers from floating-point addition round-off.
You can compile and run this complete verification class locally in any Java IDE or environment:
public class MathLog1pFullDemo {
public static void main(String[] args) {
// 1. Verify sub-epsilon accuracy where log(1 + x) fails
double tiny = 1.0e-17;
if (Math.log(1.0 + tiny) == 0.0 && Math.log1p(tiny) == tiny) {
System.out.println("Check 1 Passed: Sub-epsilon precision preserved.");
}
// 2. Verify boundary at x = -1.0
if (Math.log1p(-1.0) == Double.NEGATIVE_INFINITY) {
System.out.println("Check 2 Passed: x = -1.0 yields -Infinity.");
}
// 3. Verify NaN for x < -1.0
if (Double.isNaN(Math.log1p(-2.0))) {
System.out.println("Check 3 Passed: x < -1.0 returns NaN.");
}
System.out.println("All Math.log1p() verification checks passed successfully!");
}
}Terminal Output:
Check 1 Passed: Sub-epsilon precision preserved.
Check 2 Passed: x = -1.0 yields -Infinity.
Check 3 Passed: x < -1.0 returns NaN.
All Math.log1p() verification checks passed successfully!
It stands for "1 plus", indicating that the method computes the natural logarithm of 1 + x.
Because ln(1 + 0) = ln(1) = 0.0.
Yes, as long as x > -1.0 (for example, x = -0.5 computes ln(0.5)). Values below -1.0 return Double.NaN.
Math.expm1(y), which computes e^y - 1 with matching near-zero accuracy.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.