The math.log10(x) function computes the common (base-10) logarithm of a positive real number x (satisfying 10y = x). Compared to evaluating math.log(x, 10), math.log10(x) provides higher precision and speed because it delegates directly to hardware C99 log10() routines, avoiding division by the irrational number ln(10).
Domain Constraint: Requires strictly positive inputs (x > 0). Passing zero or a negative number raises a ValueError: math domain error.
The math.log10() function is part of Python's standard math module:
import math
result = math.log10(x)
| Parameter | Type | Description |
|---|---|---|
x |
float or int |
Required. A strictly positive real number (> 0.0) whose base-10 logarithm is computed. |
| Return Value | float |
The base-10 logarithm of x as a 64-bit IEEE 754 float. |
ValueError |
Exception | Raised when x ≤ 0 (zero or negative numbers). |
TypeError |
Exception | Raised when passing non-numeric types (strings, lists, None). |
For exact powers of ten, math.log10() produces clean integer-valued floats:
import math
# Positive powers of 10
print("log10(1): ", math.log10(1)) # 10^0 == 1 -> 0.0
print("log10(10): ", math.log10(10)) # 10^1 == 10 -> 1.0
print("log10(100): ", math.log10(100)) # 10^2 == 100 -> 2.0
print("log10(1000): ", math.log10(1000)) # 10^3 == 1000 -> 3.0
print("log10(1_000_000):", math.log10(1_000_000)) # 10^6 == 1,000,000 -> 6.0
# Sub-unit decimal fractions yield negative outputs
print("log10(0.1): ", math.log10(0.1)) # 10^(-1) == 0.1 -> -1.0
print("log10(0.01): ", math.log10(0.01)) # 10^(-2) == 0.01 -> -2.0
print("log10(0.001): ", math.log10(0.001)) # 10^(-3) == 0.001 -> -3.0
# Arbitrary non-power-of-10 floats
print("log10(2): ", round(math.log10(2), 6)) # 0.301030
print("log10(50): ", round(math.log10(50), 6)) # 1.698970
Output:
log10(1): 0.0
log10(10): 1.0
log10(100): 2.0
log10(1000): 3.0
log10(1_000_000): 6.0
log10(0.1): -1.0
log10(0.01): -2.0
log10(0.001): -3.0
log10(2): 0.301030
log10(50): 1.698970
In Python, you can calculate arbitrary-base logarithms using the 2-argument form of math.log(x, base). However, math.log(x, 10) computes the change-of-base ratio ln(x) / ln(10). Because ln(10) is irrational, floating-point rounding errors often manifest:
import math
powers = [10, 100, 1000, 10000, 100000]
print(f"{'x':>8} | {'math.log10(x)':>15} | {'math.log(x, 10)':>18} | Difference")
print("-" * 58)
for p in powers:
exact_log = math.log10(p)
div_log = math.log(p, 10)
diff = abs(exact_log - div_log)
print(f"{p:8d} | {exact_log:15.1f} | {div_log:18.15f} | {diff:.2e}")
Output:
x | math.log10(x) | math.log(x, 10) | Difference
----------------------------------------------------------
10 | 1.0 | 1.000000000000000 | 0.00e+00
100 | 2.0 | 2.000000000000000 | 0.00e+00
1000 | 3.0 | 2.999999999999999 | 4.44e-16
10000 | 4.0 | 4.000000000000000 | 0.00e+00
100000 | 5.0 | 5.000000000000000 | 0.00e+00
Notice that for x = 1000, math.log(1000, 10) yields 2.9999999999999996, which fails boolean checks (math.log(1000, 10) == 3.0 is False). In contrast, math.log10(1000) == 3.0 is guaranteed exact.
Like all real logarithms, math.log10() is defined strictly on the open interval (0.0, +∞). Passing non-positive values raises a ValueError:
import math
for bad in [0.0, -10.0]:
try:
math.log10(bad)
except ValueError as err:
print(f"log10({bad}): ValueError -> {err}")
# Special values
print("log10(inf):", math.log10(float('inf'))) # inf
print("log10(nan):", math.log10(float('nan'))) # nan
Output:
log10(0.0): ValueError -> math domain error
log10(-10.0): ValueError -> math domain error
log10(inf): inf
log10(nan): nan
Python provides four dedicated logarithmic functions in the math module for optimal precision and performance:
| Function | Base | Mathematical Purpose | Best For |
|---|---|---|---|
math.log10(x) |
Base 10 | log10(x) |
Decibels (dB), pH, Richter scale, digit counting |
math.log2(x) |
Base 2 | log2(x) |
Bit-length, Shannon entropy, binary trees, CS algorithms |
math.log(x) |
Base e | ln(x) |
Continuous compounding, physics calculus, natural growth |
math.log1p(x) |
Base e | ln(1 + x) |
High-precision evaluation when x ≈ 0 |
Base-10 logarithms underpin fundamental measurement standards throughout science and computing:
The number of decimal digits in any positive integer n equals math.floor(math.log10(n)) + 1:
import math
number = 987654321
digits = math.floor(math.log10(number)) + 1
print(f"Digits in {number}: {digits} == {len(str(number))}") # 9 == 9
Earthquake magnitude is defined on a base-10 logarithmic scale: an increase of 1.0 represents a 10-fold increase in measured seismographic wave amplitude.
In acoustics, Sound Pressure Level (SPL) in decibels (dB) is computed relative to the auditory threshold of human hearing (p0 = 20 μPa = 2 × 10−5 Pa):
In chemistry, pH measures hydrogen ion molarity [H+]:
Let's build an engineering calculator and verify both models with automated unit assertions:
import math
AUDITORY_THRESHOLD_PA = 2e-5 # 20 micro-Pascals
def calculate_sound_db(pressure_pascals, reference_pa=AUDITORY_THRESHOLD_PA):
"""Calculates Sound Pressure Level in decibels (dB): 20 * log10(p / p_0)."""
if pressure_pascals <= 0 or reference_pa <= 0:
raise ValueError("Sound pressures must be strictly positive.")
spl_db = 20.0 * math.log10(pressure_pascals / reference_pa)
return round(spl_db, 2)
def calculate_ph(hydrogen_ion_conc):
"""Calculates solution pH: -log10([H+])."""
if hydrogen_ion_conc <= 0:
raise ValueError("Hydrogen ion concentration must be strictly positive.")
return round(-math.log10(hydrogen_ion_conc), 2)
# Unit Assertions
# 1. Auditory threshold (20 uPa) produces exactly 0 dB
assert calculate_sound_db(2e-5) == 0.0
# 2. Conversational speech (0.02 Pa) produces 60 dB
assert calculate_sound_db(0.02) == 60.0
# 3. Acoustic pain threshold (20 Pa) produces 120 dB
assert calculate_sound_db(20.0) == 120.0
# 4. Pure water [H+] = 1e-7 M produces neutral pH 7.0
assert calculate_ph(1e-7) == 7.0
# 5. Strong acid [H+] = 0.01 M produces pH 2.0
assert calculate_ph(0.01) == 2.0
# 6. Alkaline bleach [H+] = 1e-13 M produces pH 13.0
assert calculate_ph(1e-13) == 13.0
print("Sound level of vacuum cleaner (0.063 Pa):", calculate_sound_db(0.063), "dB")
print("pH of black coffee ([H+] = 1e-5 M): ", calculate_ph(1e-5))
print("All decibel and pH assertions passed successfully!")
Open in Google Colab View on GitHub
Run all math.log10() examples interactively, benchmark precision against math.log(x, 10), count integer digits, and compute acoustic decibels and chemistry pH in Google Colab.
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.