Python math.log10() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
math module

Definition

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)
CallWhat 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

ParameterDescription
xRequired positive number (int or float). Zero or negatives raise ValueError.

Return Value

Return ValueDescription
floatBase-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}")

How It Works

  1. Exact powers of ten. Ratios of ten, one hundred, and one thousand convert cleanly to 10.0, 20.0, and 30.0 dB.
  2. Negative power scales. Hydrogen ion concentration 1e-4 has log10 equal to -4.0, yielding pH 4.0.
  3. 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)))

How It Works

  1. Exact small counts. Flooring log10(n) gives one less than the digit count for powers and boundaries up to ordinary sizes.
  2. Float rounding surprise. At 10**16 - 1, binary64 rounds up to 10**16, inflating log10 to 16.0 and giving 17.
  3. String length never lies. Converting huge integers with str() and len() reports the true 16 digits.

Example 3 — Compare math.log10() Against math.log(x, 10) and Errors

Check exactness on 1000 and handle negative input cleanly.

python
import math

print(math.log10(1000), math.log(1000, 10) == 3.0)

try:
    math.log10(-10)
except ValueError:
    print("log10(-10) raises ValueError")

How It Works

  1. Exact 3.0. math.log10(1000) returns exact 3.0, whereas dividing natural logs in math.log(1000, 10) falls short by one ULP.
  2. Boolean proof. Checking == 3.0 prints False for the two-argument log() call.
  3. Negative input caught. Passing -10 raises ValueError.

Frequently Asked Questions

math.log10() calls a dedicated base-10 implementation, whereas math.log(1000, 10) divides two float logs and loses one bit of accuracy.
Only for integers well below 10**15. Above that, float rounding can turn 10**16 - 1 into 10**16. Use len(str(abs(n))) for arbitrary integers.
ValueError, because the logarithm of zero is not defined for real numbers.
Use 10 ** x for exact integer powers or 10.0 ** x / math.pow(10, x) for floats.
Yes, because it skips computing two separate logarithms and dividing them.

Next: math.log1p()

Return the natural logarithm of 1 + x accurately near zero.

log1p() →

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.

15 people found this page helpful