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
Call
What 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
Parameter
Description
a
The value whose natural logarithm you want. It must be greater than zero for a normal result.
Return Value
Return Value
Description
double
The natural logarithm of a. NaN if a is negative or NaN; -Infinity if a is positive or negative zero; Infinity for Infinity.
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.
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.
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.
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.
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.
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");
}
}
Output
Years to double at 7%: 10.24
Rule of 72 estimate: 10.29
1000 to 5000 at 5%: 32.99 years
How It Works
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.
Close to the shortcut. 72 divided by 7 is about 10.29, a handy estimate; the logarithm gives the more exact 10.24.
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.