Java Math log1p() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Java 5+

Definition

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);
CallWhat 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

ParameterDescription
xThe value to which 1 is added inside the logarithm. It must be greater than -1 for a normal result.

Return Value

Return ValueDescription
doubleThe 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.

Compare with Similar Methods

Related methods:

MethodHow it differs
log(1 + x)Same formula, but loses accuracy when x is tiny.
expm1(x)The inverse: expm1(log1p(x)) is x.
log10(x)Base-10 logarithm instead of base e.
exp(x)Turns a logarithm back into a plain growth factor.

Java Examples

Example 1 — log1p() Values and the Edges of Its Domain

Check the zero values, ln 2, and what happens at -1, below -1, and at infinity.

Java
public class MathLog1pBasics {
    public static void main(String[] args) {
        System.out.println("log1p(0) = " + Math.log1p(0));
        System.out.println("log1p(-0.0) = " + Math.log1p(-0.0));
        System.out.println("log1p(1) = " + Math.round(Math.log1p(1) * 10000) / 10000.0);
        System.out.println("log1p(-1) = " + Math.log1p(-1));
        System.out.println("log1p(-2) = " + Math.log1p(-2));
        System.out.println("log1p(Infinity) = " + Math.log1p(Double.POSITIVE_INFINITY));
    }
}

How It Works

  1. Zero in, zero out. The natural log of 1 + 0 is 0.0, and a negative zero is returned with its sign unchanged.
  2. The log of 2. log1p(1) is ln(1 + 1), which is 0.6931 after rounding to four decimals.
  3. 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.

Java
import java.util.Locale;

public class MathLog1pPrecision {
    public static void main(String[] args) {
        double x = 1e-16;
        System.out.println("1 + x == 1.0? " + (1 + x == 1.0));
        System.out.println("log(1 + x) == 0.0? " + (Math.log(1 + x) == 0.0));
        System.out.println("log1p(x) > 0? " + (Math.log1p(x) > 0));

        double y = 1e-10;
        System.out.println("log(1 + y) * 1e10: " + String.format(Locale.US, "%.8f", Math.log(1 + y) * 1e10));
        System.out.println("log1p(y) * 1e10:   " + String.format(Locale.US, "%.8f", Math.log1p(y) * 1e10));
    }
}

How It Works

  1. 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.
  2. log1p never adds. It works from x directly, so the tiny positive answer, about 1e-16, survives.
  3. 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.

Java
import java.util.Locale;

public class MathLog1pGrowth {
    public static void main(String[] args) {
        double[] yearly = {0.10, -0.10};
        double plainSum = 0;
        double logSum = 0;
        for (double r : yearly) {
            plainSum += r;
            logSum += Math.log1p(r);
        }
        double plain = plainSum / yearly.length * 100;
        double real = Math.expm1(logSum / yearly.length) * 100;
        System.out.println("Plain average: " + String.format(Locale.US, "%.2f", plain) + "%");
        System.out.println("Real average:  " + String.format(Locale.US, "%.2f", real) + "%");
        System.out.println("1000 becomes:  " + String.format(Locale.US, "%.2f", 1000 * (1 + 0.10) * (1 - 0.10)));
    }
}

How It Works

  1. 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.
  2. 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.
  3. 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.

Next: max()

The larger of two int, long, float, or double values.

max() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful