The Math.log10() method returns the base 10 common logarithm of a double argument.
Base-10 Scientific Scales: Used for decibels, Richter scales, and pH chemistry. Defined for positive numbers (0.0, +infinity). For natural logs (base e), see Math.log(). For near-zero precision, see Math.log1p(). For base-10 powers, see Math.pow().
Defined statically in java.lang.Math:
// Formal signature of Math.log10() in java.lang.Math
public static double log10(double a)
| Signature Element | Type | Description & Operational Rule |
|---|---|---|
a |
double |
The positive argument whose common logarithm (base 10) is evaluated. |
| Return Value | double |
The base 10 logarithm; returns -Infinity for 0, NaN for < 0. |
| Base Constant | Base 10 | Satisfies the identity: Math.pow(10, Math.log10(x)) == x. |
Evaluating clean integer powers of 10:
System.out.println("log10(1.0): " + Math.log10(1.0)); // 0.0
System.out.println("log10(10.0): " + Math.log10(10.0)); // 1.0
System.out.println("log10(100.0): " + Math.log10(100.0)); // 2.0
System.out.println("log10(1000.0): " + Math.log10(1000.0)); // 3.0Terminal Output:
log10(1.0): 0.0
log10(10.0): 1.0
log10(100.0): 2.0
log10(1000.0): 3.0
Values between 0 and 1 evaluate to negative logarithms (e.g. log10(0.01) = -2.0):
// Fractional numbers yield negative logarithms (10^-1 = 0.1, 10^-2 = 0.01)
System.out.println("log10(0.1): " + Math.log10(0.1)); // -1.0
System.out.println("log10(0.01): " + Math.log10(0.01)); // -2.0
System.out.println("log10(0.001): " + Math.log10(0.001)); // -3.0
System.out.println("log10(4.12): " + Math.log10(4.12)); // ~0.614897Terminal Output:
log10(0.1): -1.0
log10(0.01): -2.0
log10(0.001): -3.0
log10(4.12): 0.6148972160331346
Logarithms are undefined for zero and negative real numbers:
// Special boundary values: 0 yields -Infinity, negatives return NaN
System.out.println("log10(0.0): " + Math.log10(0.0)); // -Infinity
System.out.println("log10(-0.0): " + Math.log10(-0.0)); // -Infinity
System.out.println("log10(-10): " + Math.log10(-10.0)); // NaN (undefined)
System.out.println("log10(NaN): " + Math.log10(Double.NaN)); // NaNTerminal Output:
log10(0.0): -Infinity
log10(-0.0): -Infinity
log10(-10): NaN
log10(NaN): NaN
Contrasting logarithmic methods in Java:
| Method | Mathematical Base | Result for Input 10.0 | Primary Use Case |
|---|---|---|---|
Math.log10(x) |
Base 10 | 1.0 | Decibels, scientific orders of magnitude, digit counts. |
Math.log(x) |
Euler's base e | ~2.302585 | Calculus, physics, compound growth models. |
Math.log1p(x) |
Euler's base e (1+x) | ~2.397895 (for x=10) | Accurate computation for small x values near zero. |
Determining the number of decimal digits in an integer in O(1) time without string conversions:
public class IntegerDigitCounter {
public static int countDigits(long number) {
if (number == 0) return 1;
long absVal = Math.abs(number);
// Formula: floor(log10(absVal)) + 1
return (int) Math.floor(Math.log10(absVal)) + 1;
}
public static void main(String[] args) {
System.out.println("Digits in 7: " + countDigits(7)); // 1
System.out.println("Digits in 999: " + countDigits(999)); // 3
System.out.println("Digits in 50000: " + countDigits(50000)); // 5
System.out.println("Digits in -12345678: " + countDigits(-12345678)); // 8
}
}Terminal Output:
Digits in 7: 1
Digits in 999: 3
Digits in 50000: 5
Digits in -12345678: 8
Measuring audio amplification and signal gain on a logarithmic decibel scale:
public class DecibelPowerCalculator {
public static double powerGainDb(double pOut, double pIn) {
return 10.0 * Math.log10(pOut / pIn);
}
public static void main(String[] args) {
System.out.println("10x Gain: " + powerGainDb(100.0, 10.0) + " dB"); // 10.0 dB
System.out.println("100x Gain: " + powerGainDb(1000.0, 10.0) + " dB"); // 20.0 dB
}
}Terminal Output:
10x Gain: 10.0 dB
100x Gain: 20.0 dB
Determining acidity/alkalinity from hydrogen ion molarity (pH = -log10[H+]):
public class ChemistryPhCalculator {
public static double getPh(double hConcentration) {
return -Math.log10(hConcentration);
}
public static void main(String[] args) {
System.out.println("Pure Water pH: " + getPh(1.0e-7)); // 7.0
System.out.println("Acid pH: " + getPh(1.0e-2)); // 2.0
}
}Terminal Output:
Pure Water pH: 7.0
Acid pH: 2.0
Calling Math.log10(0) returns -Infinity. When implementing digit counters, always handle 0 as an explicit edge case (1 digit).
Calling Math.log10() with negative numbers silently produces Double.NaN without throwing exceptions. Always validate positive input ranges.
You can compile and run this complete verification class locally in any Java IDE or environment:
public class MathLog10FullDemo {
public static void main(String[] args) {
// 1. Verify exact integer powers of 10
if (Math.log10(100.0) == 2.0 && Math.log10(1000.0) == 3.0) {
System.out.println("Check 1 Passed: Exact powers of 10 match integers.");
}
// 2. Verify negative infinity asymptote
if (Math.log10(0.0) == Double.NEGATIVE_INFINITY) {
System.out.println("Check 2 Passed: Zero yields negative infinity.");
}
// 3. Verify NaN on negative input
if (Double.isNaN(Math.log10(-50.0))) {
System.out.println("Check 3 Passed: Negative values return NaN.");
}
System.out.println("All Math.log10() verification checks passed successfully!");
}
}Terminal Output:
Check 1 Passed: Exact powers of 10 match integers.
Check 2 Passed: Zero yields negative infinity.
Check 3 Passed: Negative values return NaN.
All Math.log10() verification checks passed successfully!
Because our numeral system is decimal (base 10), making base-10 logarithms the standard for engineering, decibels, and scientific scales.
Use Math.pow(10.0, x).
Math.log() uses base e (~2.71828), while Math.log10() uses base 10.
Returns Double.NEGATIVE_INFINITY.
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.