math.log10(x) returns the base-10 logarithm of positive number x as a float.
It is usually more accurate than math.log(x, 10), returning exact integers like 3.0 for 1000 and -3.0 for 0.001.
math.log10() is a function in Python's standard math module: import it with import math and call math.log10(...), or import the name directly with from math import log10.
Syntax
Import math and pass a positive real number.
python
import math
exp10 = math.log10(x)
Call
What it does
math.log10(1000)
Returns 3.0 (exact, unlike math.log(1000, 10)).
math.log10(0.01)
Returns -2.0.
math.log10(1)
Returns 0.0.
math.log10(0)
Raises ValueError: domain error.
Use len(str(n)) instead of math.log10(n) when counting digits of huge integers past float precision.
Function signatures
python
math.log10(x, /)
# returns: float log base 10 of x
Parameters
Parameter
Description
x
Required positive number (int or float). Zero or negatives raise ValueError.
Return Value
Return Value
Description
float
Base-10 logarithm of x. Returns inf for inf and nan for nan.
Python Examples
Example 1 — Convert Power Ratios to Decibels and pH Values
Compute signal gain in decibels and solution acidity from hydrogen concentration.
python
import math
for ratio in [1, 10, 100, 1000]:
db = 10.0 * math.log10(ratio)
print(f"ratio={ratio:>4} -> {db:>4.1f} dB")
hydrogen_mol = 1e-4
print(f"pH = {-math.log10(hydrogen_mol):.1f}")
Output
ratio= 1 -> 0.0 dB
ratio= 10 -> 10.0 dB
ratio= 100 -> 20.0 dB
ratio=1000 -> 30.0 dB
pH = 4.0
How It Works
Exact powers of ten. Ratios of ten, one hundred, and one thousand convert cleanly to 10.0, 20.0, and 30.0 dB.
Negative power scales. Hydrogen ion concentration 1e-4 has log10 equal to -4.0, yielding pH 4.0.
Zero gain baseline. A ratio of 1 produces 0.0 dB.
Example 2 — Count Decimal Digits and Spot Huge-Int Rounding
Use floor(log10(n)) + 1 for small numbers and compare with len(str(n)) on 16-digit ints.
python
import math
for n in [1, 9, 42, 999, 1000]:
digits = math.floor(math.log10(n)) + 1
print(n, "-> digits:", digits)
huge = 10 ** 16 - 1
print(math.floor(math.log10(huge)) + 1, len(str(huge)))