Concepts / Trigonometric Functions in the Math Module

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.

  • Programming
Interactive lab

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

  1. 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.
  2. 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.
  3. math.sin, math.cos and math.tan read their argument as radians: convert degrees with degrees / 360.0 * 2 * math.pi.
  4. 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.
  5. 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').

Educational simulation

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?

  • math.log10(100)

  • math.log(100)

  • Either function always gives the same result

Reveal answer
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

Positive inputxLogarithm functionmath.log10 or math.logBase 10 resultmath.log10(x)Natural logarithmmath.log(x)Custom-base resultmath.log(x, base)provide xmath.log10()math.log() with no basemath.log() with a base
What happens when code calls math.log10() or math.log(), and which path produces a base 10 result?

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.

python
Output
2.0

The 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.

Signal-to-noiseratio100math.log10()base 10Logarithmic value2.0inputcompute
How does a signal-to-noise ratio move through math.log10() to become a base 10 logarithmic value used in decibel calculations?

Natural and Custom Bases

CallDefault or selected baseUse when
math.log(x)Natural logarithm, base eThe calculation requires a natural logarithm
math.log10(x)Base 10The calculation uses a base 10 logarithmic scale
math.log(x, base)The explicitly supplied baseA specialized base is required

The function call determines which logarithm base is used.

Code walkthrough
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)
  1. 1
    The variable value is set to 100.
  2. 2
    natural_result calls math.log() with one argument, so the calculation uses the natural logarithm, base e.
  3. 3
    base10_result calls math.log10(), so the calculation uses base 10.
  4. 4
    custom_result calls math.log() with 10 as the optional base, so it also uses base 10.
  5. 5
    The 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 through math.log()'s optional base parameter.
Output
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

python

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 like
    math.log(100)
    Why it fails

    With no second argument, math.log() computes the natural logarithm, not the base 10 logarithm.

    Do this instead

    Use 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 like

    Comparing math.log10(value) with math.log(value) without checking the bases

    Why it fails

    The two calls use different default bases.

    Do this instead

    Identify the required base before selecting the function.

  • Common mistake

    Passing zero or a negative value to a logarithm

    What it looks like
    math.log10(0)
    Why it fails

    Logarithms are undefined for zero and negative values.

    Do this instead

    Validate that the input is positive before making the call.

  • Common mistake

    Forgetting that math.log() accepts an optional base

    What it looks like

    Using math.log(value) when a specialized base is required

    Why it fails

    Leaving out the second argument selects base e.

    Do this instead

    Use math.log(value, base) when the calculation needs an explicit base such as base 2 or base 10.

Choose the Call

MEDIUM

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.
Worked example

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?

  1. 1
    Identify the required base

    The requirement explicitly says base 10.

  2. 2
    Select the function

    math.log10() directly computes base 10 logarithms.

  3. 3
    Check the input

    The input 100 is positive, so it satisfies the logarithm domain condition.

  4. 4
    Evaluate the call

    The base 10 logarithm of 100 is 2, so a Python floating-point result is represented as 2.0.

Result

Use math.log10(100), which produces 2.0.

Key Takeaways

  1. 1
    math.log10(x) computes the base 10 logarithm of x.
  2. 2
    math.log(x) computes the natural logarithm, using base e by default.
  3. 3
    math.log(x, base) allows an explicit base, including specialized bases such as 2.
  4. 4
    Use the function whose base matches the application, such as base 10 for decibels, pH scales, and other common logarithmic scales.
  5. 5
    Validate 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.

Related concepts

More concepts starting with T