Java Math log() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
java.lang.Math

Definition

In Java, Math.log(x) returns the natural logarithm of x: the power to which e (about 2.71828) must be raised to get x. Math.log(1) is 0.0.

Despite its short name, it is not the base-10 logarithm; that is Math.log10. Use log for growth and doubling times, and for any other base with the change-of-base rule. It has been part of Java since the early versions.

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.log(...). No import is needed, because classes in java.lang are always visible.

Syntax

Pass a positive number. The result is a double.

Java
double zero = Math.log(1);                     // 0.0
double ln2 = Math.log(2);                      // about 0.6931
double log2 = Math.log(x) / Math.log(2);       // base 2
double years = Math.log(2) / Math.log(1.07);   // doubling time
CallWhat it does
Math.log(Math.E)Returns 1.0.
Math.log(0)Returns -Infinity.
Math.log(-1)Returns NaN.
Math.log(x) / Math.log(b)Logarithm of x to any base b.

Java has no built-in base-2 logarithm in Math. For whole numbers, Integer.numberOfLeadingZeros and Integer.toBinaryString avoid floating-point error completely.

Library declarations
Java
// java.lang.Math
public static double log(double a)

Parameters

ParameterDescription
aThe value whose natural logarithm you want. It must be greater than zero for a normal result.

Return Value

Return ValueDescription
doubleThe natural logarithm of a. NaN if a is negative or NaN; -Infinity if a is positive or negative zero; Infinity for Infinity.

Compare with Similar Methods

Other logarithm and exponential tools:

MethodHow it differs
log10(x)Base-10 logarithm; exact for powers of ten.
log1p(x)log(1 + x), accurate for tiny x.
exp(x)The inverse of log.
Integer.toBinaryString(n).length()Number of bits in an int, with no rounding errors.

Java Examples

Example 1 — Natural Logarithm and Its Special Values

Check a few known values, then see how zero, negative numbers, and infinity are handled.

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

How It Works

  1. Anchor values. e to the power 0 is 1, so log(1) is 0.0; e to the power 1 is e, so log(Math.E) comes out as 1.0.
  2. Not base 10. log(10) is about 2.302585, because e must be raised to roughly 2.3 to reach 10; the rounding trick shows 2.3026.
  3. Out-of-range input. As x shrinks toward 0 the logarithm falls without limit, so log(0) is -Infinity; negative numbers have no real logarithm and give NaN.

Example 2 — Change of Base: Logarithms in Base 2 and 5

log(x) / log(base) gives the logarithm to any base. Round results that should be whole numbers.

Java
public class MathLogBase {
    static double logBase(double base, double x) {
        return Math.log(x) / Math.log(base);
    }

    public static void main(String[] args) {
        System.out.println("log2(1024) = " + Math.round(logBase(2, 1024)));
        System.out.println("log5(125) = " + Math.round(logBase(5, 125)));
        System.out.println("log2(10) = " + Math.round(logBase(2, 10) * 10000) / 10000.0);

        int n = 1000;
        int bits = (int) (Math.log(n) / Math.log(2)) + 1;
        System.out.println("Bits needed for 1000: " + bits);
        System.out.println("Binary check: " + Integer.toBinaryString(n).length());
    }
}

How It Works

  1. The rule. A logarithm to base b equals the natural logarithm of x divided by the natural logarithm of b, so one helper method covers every base.
  2. Round exact answers. The quotients for 1024 and 125 should be exactly 10 and 3, but floating-point division may give 9.999999999999998 or 3.0000000000000004; Math.round returns the intended whole number as a long.
  3. Counting bits. log2(1000) is about 9.97; dropping the decimals gives 9, and adding 1 gives the 10 bits that Integer.toBinaryString confirms (1111101000).

Example 3 — How Many Years Until Money Doubles?

Solve (1 + rate) ^ years = target for years with a logarithm. Output uses an English-style decimal point.

Java
import java.util.Locale;

public class MathLogDoubling {
    public static void main(String[] args) {
        double years = Math.log(2) / Math.log(1.07);
        System.out.println("Years to double at 7%:  " + String.format(Locale.US, "%.2f", years));
        System.out.println("Rule of 72 estimate:    " + String.format(Locale.US, "%.2f", 72 / 7.0));

        double wait = Math.log(5000.0 / 1000.0) / Math.log(1.05);
        System.out.println("1000 to 5000 at 5%:     " + String.format(Locale.US, "%.2f", wait) + " years");
    }
}

How It Works

  1. Undoing a power. The balance grows by a factor of 1.07 each year, so asking "how many years to double" means solving 1.07 to the power years = 2; dividing the two logarithms does that.
  2. Close to the shortcut. 72 divided by 7 is about 10.29, a handy estimate; the logarithm gives the more exact 10.24.
  3. Any target. Going from 1000 to 5000 means a factor of 5, so Math.log(5) / Math.log(1.05) is about 32.99 years.

Frequently Asked Questions

It returns the natural logarithm (base e) of a number. Math.log(1) is 0.0 and Math.log(Math.E) is 1.0.
No. Math.log is the natural logarithm. For base 10 use Math.log10, and for other bases divide: Math.log(x) / Math.log(base).
Use Math.log(x) / Math.log(2). For whole numbers, use Math.round on the result when you expect an exact answer, or use Integer.numberOfLeadingZeros for bit counts.
It returns -Infinity. Math.log of a negative number returns NaN. Neither case throws an exception.
Use Math.exp. Math.exp(Math.log(x)) returns x, apart from tiny rounding differences.

Next: log10()

Base-10 logarithm of a double.

log10() 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.

9 people found this page helpful