Importing and Using the Math Module
When the required base is 10, math.log10(x) communicates the intent directly. math.log(x, 10) also specifies base 10 through the optional parameter.
Try it: The math Module: Logs, Roots and Angles
What math.log(x), math.log10(x), math.log(x, base), math.sqrt(x) and math.sin / cos / tan really compute: which logarithm base each call uses, why 0 and negative inputs must be checked before the call (ValueError: math domain error), why trigonometric functions need radians, and that math.pi and every result are binary64 approximations (math.log(1000, 10) prints 2.9999999999999996).
How it works
- math.log(x) is the natural logarithm (base e); math.log10(x) is base 10; math.log(x, base) computes log(x) / log(base) with two rounded natural logs.
- Logarithms need x > 0 and math.sqrt needs x >= 0; otherwise the call raises ValueError: math domain error, so a guard checks the input first.
- math.sin, math.cos and math.tan read their argument as radians: convert degrees with degrees / 360.0 * 2 * math.pi.
- math.pi is a float value (no parentheses) that approximates pi to about 16 digits, so sin(180 degrees) is 1.2246467991473532e-16, not 0.
- Every result is the nearest binary64 value; print shows the shortest repr that reads back as the same float, while the stored value has more digits.
Default run (6 steps): Ready: the program calls math.log10(x). … print(result) shows 3.0.
Simplified: One-call programs over int / float literals you type (|x| up to 1e15). The results come from the browser's IEEE-754 math library: math.sqrt and the radians arithmetic are identical to CPython; log / sin / cos / tan match CPython 3.12 (Windows) exactly for about 91% of the tested inputs and are within 2 units in the last place otherwise, because C math libraries differ in the last bit. Error messages are CPython 3.12's (3.14 says 'expected a positive input').
Loading the simulation…
A Function Choice Changes the Result
Two logarithm functions can receive the same input and still produce different results. The key is not only the number being passed in, but also the function name: math.log10() calculates a base 10 logarithm, while math.log() calculates a natural logarithm unless an optional base is supplied.
What do you think happens?
Which expression produces 3: math.log10(1000) or math.log(1000)?
Reveal answer
math.log10(1000)
1000 is a power of 10, so its base 10 logarithm is 3. math.log(1000) uses the natural logarithm when no optional base is supplied, so it produces a different result.
Tracing the Called Function
Trace a logarithm expression in three steps. First, identify the function after math. Second, identify the value passed to that function. Third, use the function's definition to determine the kind of logarithm being calculated. The function name determines the base unless math.log() receives its optional base argument.
import math
result = math.log10(1000)
print(result)- 1The math module is imported for use in the example.
- 2
math.log10is selected, so the calculation uses base 10. - 3The value 1000 is passed to the function.
- 4The result is printed as 3.0.
3.0Two Logarithm Bases
Use math.log10() when the calculation specifically requires base 10. This is useful for decibels, pH scales, and other common logarithmic scales. Use math.log() when the calculation requires a natural logarithm, which has base e. The source describes natural logarithms as useful in calculus, exponential modeling, and statistics.
| Expression | Logarithm used | Result for input 1000 |
|---|---|---|
| math.log10(1000) | Base 10 | 3 |
| math.log(1000) | Natural logarithm, base e | Approximately 6.907755278982137 |
| math.log(1000, 10) | Custom base 10 | 3 |
The same input can produce different results because the logarithm base differs.
Base-10 Work with Ratios
A signal-to-noise ratio is a practical setting where a base 10 logarithm can be relevant. Pass the positive ratio to math.log10(). The returned value is the ratio's base 10 logarithm, which can then be used in a decibel-related calculation. This article focuses on selecting and applying the logarithm function; it does not replace the full domain-specific decibel formula.
3.0Natural and Custom Bases
math.log() has two useful forms. With one argument, math.log(x) computes the natural logarithm of x, using base e. With an optional second argument, math.log(x, base) computes a logarithm with the specified base. The source identifies base 2 as an example for specialized applications such as information theory.
import math
natural_result = math.log(8)
base_2_result = math.log(8, 2)
base_10_result = math.log(8, 10)
print(natural_result)
print(base_2_result)
print(base_10_result)- 1
math.log(8)uses the natural logarithm because no base is supplied. - 2
math.log(8, 2)uses base 2, so it calculates the base 2 logarithm. - 3
math.log(8, 10)uses base 10. - 4The optional base changes which logarithm is calculated.
2.0794415416798357
3.0
0.9030899869919435When the required base is 10, math.log10(x) communicates the intent directly. math.log(x, 10) also specifies base 10 through the optional parameter.
Mistakes Beginners Make
Common mistake Using math.log() when a base 10 result is required.
What it looks likeresult = math.log(1000)Why it failsmath.log()without a second argument computes a natural logarithm, not a base 10 logarithm.Do this insteadUse math.log10(1000), or use math.log(1000, 10) when expressing the base through the optional parameter.
Common mistake Assuming that math.log10() and math.log() always return the same value.
What it looks likemath.log10(1000) and math.log(1000)Why it failsThe functions use different logarithm bases when
math.log()has no optional base.Do this insteadRead the function name and check whether math.log() includes a base argument before predicting the output.
Common mistake Passing zero or a negative value to a logarithm function.
What it looks likemath.log10(0)Why it failsLogarithms are undefined for zero and negative values.
Do this insteadValidate that the input is positive before calling math.log10() or math.log().
Common mistake Forgetting what the optional base parameter represents.
What it looks likemath.log(8, 2)Why it failsThe second argument selects the base; it is not another input value unrelated to the logarithm.
Do this insteadRead math.log(x, base) as a logarithm of x using the supplied base.
Practice the Trace
For each expression, identify the logarithm base and predict the result before checking it: math.log10(100), math.log(100, 10), and math.log(8, 2). Then explain why math.log(100) should not be expected to match the first two expressions.
Hints
math.log10()always calculates base 10.- The second argument of
math.log()supplies a custom base. math.log()without a second argument calculates a natural logarithm.
Key Takeaways
- 1
math.log10(x)computes the base 10 logarithm of x. - 2
math.log(x)computes the natural logarithm of x when no base is supplied. - 3
math.log(x, base)supports a custom base, including base 2 for specialized applications such as information theory. - 4Use
math.log10()for base-10 contexts such as decibels, pH scales, and other common logarithmic scales. - 5Check that every logarithm input is positive because logarithms are undefined for zero and negative values.
At a glance
- Choose
math.log10()when the required logarithm base is 10. - Choose
math.log()for natural logarithms, or provide a second argument when a custom base is required. - Trace the function name, input, and optional base to predict the result.
- Validate logarithm inputs as positive before calculating.