Trigonometric Functions in the Math Module
Choosing the wrong function can therefore produce a valid numerical result that is not the result your application requires.
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…
The Function Choice
The logarithm function you choose determines the base of the calculation. math.log10() computes a base 10 logarithm, while math.log() computes a natural logarithm with base e unless you provide another base. Choosing the wrong function can therefore produce a valid numerical result that is not the result your application requires.
What do you think happens?
A calculation needs the base 10 logarithm of 100. Which function should be selected?
Reveal answer
math.log10(100)
math.log10() is the function defined for base 10 logarithms. math.log() uses the natural logarithm by default, so it does not represent the same calculation unless its optional base argument is set to 10.
Tracing the Calculation
Trace a logarithm call in two stages. First, inspect the function name and its arguments to identify the intended base. Next, verify that the input is positive. math.log10(x) selects base 10 directly. math.log(x) selects base e when no second argument is supplied, and math.log(x, base) selects the explicitly supplied base.
Base 10 in Practice
Use math.log10() when the problem is expressed on a base 10 scale. The source identifies decibels, pH scales, and other common logarithmic scales as situations where base 10 logarithms are essential. A signal-to-noise ratio can therefore be passed to math.log10() when preparing a base 10 logarithmic step in a decibel calculation.
2.0The input is 100, and math.log10() asks for its base 10 logarithm. Because 10 raised to the power 2 equals 100, the result is 2.0. The important tracing detail is that the function name, not the variable name, selects the logarithm base.
Natural and Custom Bases
| Call | Default or selected base | Use when |
|---|---|---|
| math.log(x) | Natural logarithm, base e | The calculation requires a natural logarithm |
| math.log10(x) | Base 10 | The calculation uses a base 10 logarithmic scale |
| math.log(x, base) | The explicitly supplied base | A specialized base is required |
The function call determines which logarithm base is used.
import math
value = 100
natural_result = math.log(value)
base10_result = math.log10(value)
custom_result = math.log(value, 10)
print(natural_result)
print(base10_result)
print(custom_result)- 1The variable value is set to 100.
- 2
natural_resultcallsmath.log()with one argument, so the calculation uses the natural logarithm, base e. - 3
base10_resultcallsmath.log10(), so the calculation uses base 10. - 4
custom_resultcallsmath.log()with 10 as the optional base, so it also uses base 10. - 5The two base 10 expressions represent the same logarithm calculation.
math.log(value)uses base e because no optional base is supplied.math.log10(value)directly requests base 10.math.log(value, 10)requests base 10 throughmath.log()'s optional base parameter.
The first result is the natural logarithm of 100. The second and third results are base 10 logarithms of 100, so both base 10 calculations produce 2.0.math.log() is especially relevant when working with natural logarithms, which the source associates with calculus, exponential modeling, and statistics. Its optional base parameter also supports specialized applications, including information theory with base 2. Use the second argument deliberately: math.log(value, 10) is a custom-base form of the same base 10 calculation that math.log10(value) expresses directly.
Input Validation
The positive-input check belongs before the logarithm call. In this example, the call proceeds because 100 is positive. The same validation principle applies whether the selected function is math.log10(), math.log(), or math.log() with an explicit base.
Common Selection Mistakes
Common mistake Using math.log() when the task requires a base 10 logarithm
What it looks likemath.log(100)Why it failsWith no second argument,
math.log()computes the natural logarithm, not the base 10 logarithm.Do this insteadUse math.log10(100), or use math.log(100, 10) when an explicit base argument is appropriate.
Common mistake Assuming math.log10() and math.log() always produce the same result
What it looks likeComparing math.log10(value) with math.log(value) without checking the bases
Why it failsThe two calls use different default bases.
Do this insteadIdentify the required base before selecting the function.
Common mistake Passing zero or a negative value to a logarithm
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 making the call.
Common mistake Forgetting that math.log() accepts an optional base
What it looks likeUsing math.log(value) when a specialized base is required
Why it failsLeaving out the second argument selects base e.
Do this insteadUse math.log(value, base) when the calculation needs an explicit base such as base 2 or base 10.
Choose the Call
For each requirement, choose the most direct logarithm call: a base 10 logarithm for a common logarithmic scale, a natural logarithm for an exponential modeling calculation, and a base 2 logarithm for an information theory calculation. Then state the input validation condition that should be checked before each call.
Hints
- Use
math.log10()for the base 10 case. - Use
math.log()with no base for the natural logarithm case. - Use
math.log(value, 2)for the explicit base 2 case. - In every case, the input must be positive.
Tracing a base 10 requirement
A calculation needs the base 10 logarithm of 100. Which call should be used, and what result should be expected?
- 1Identify the required base
The requirement explicitly says base 10.
- 2Select the function
math.log10()directly computes base 10 logarithms. - 3Check the input
The input 100 is positive, so it satisfies the logarithm domain condition.
- 4Evaluate the call
The base 10 logarithm of 100 is 2, so a Python floating-point result is represented as 2.0.
Use math.log10(100), which produces 2.0.
Key Takeaways
- 1
math.log10(x)computes the base 10 logarithm of x. - 2
math.log(x)computes the natural logarithm, using base e by default. - 3
math.log(x, base)allows an explicit base, including specialized bases such as 2. - 4Use the function whose base matches the application, such as base 10 for decibels, pH scales, and other common logarithmic scales.
- 5Validate that every logarithm input is positive because zero and negative values are outside the logarithm's domain.
At a glance
- Choose
math.log10()when the required logarithm has base 10. - Choose
math.log()for natural logarithms, or provide its optional base argument for another base. - A signal-to-noise ratio can be passed through
math.log10()as part of a base 10 logarithmic step in decibel calculations. - Trace both the function name and its arguments before interpreting a logarithm result.
- Check that the input is positive before calculating.