Concepts / Error Handling and Input Validation

Error Handling and Input Validation

Input validation is not an optional cleanup step.

  • 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…

Why Function Choice Matters

A logarithm calculation is not fully specified by its input value alone. The function you choose also determines the base. In Python, math.log10() computes a base-10 logarithm, while math.log() computes a natural logarithm with base e unless an optional base is supplied. Choosing the wrong function can therefore produce a result that does not match the scale or model you are working with.

Key point

First identify the required base. Use math.log10() when the calculation requires base 10. Use math.log() for natural logarithms or for a custom base supplied through its optional base parameter.

Tracing the Selected Function

Trace a logarithm calculation in three steps. First, identify the function name. Next, identify the base that function uses. Finally, interpret the result according to that base. A call using math.log10() follows the base-10 path. A call using math.log() follows the natural-logarithm path unless its optional base argument changes the calculation.

Logarithm callfunction and inputmath.log10()base 10Base-10 resultmath.log()base eNatural-log resultmath.log(value, base)specified baseCustom-base resultname is log10name is loglog has base argumentuses base 10uses base euses supplied base
Which logarithm function executes, what base does it use, and what kind of result should you expect?

Two Bases, Two Uses

FunctionBaseUse described in the source
math.log10()10Decibels, pH scales, and other common logarithmic scales
math.log()eCalculus, exponential modeling, and statistics
math.log(value, base)The supplied baseSpecialized applications such as information theory with base 2

The function name and optional argument determine the logarithm base.

The most important distinction is the default base. math.log10() is explicit: it computes base 10. math.log() is different: it computes a natural logarithm, meaning a logarithm with base e, when no optional base is provided. The same math.log() function can also accept a base argument, allowing a custom base for a specialized calculation.

Worked example

Selecting a Function for a Scale

A calculation is part of a decibel-related or other common logarithmic scale. Which logarithm function should be selected?

  1. 1
    Identify the required scale

    The calculation belongs to a common logarithmic scale, and the source identifies decibels and similar scales with base-10 logarithms.

  2. 2
    Match the function to the base

    Select math.log10() because it computes base-10 logarithms.

  3. 3
    Reject the tempting alternative

    Do not substitute math.log() without checking the requirement, because its default is the natural logarithm rather than the base-10 logarithm.

Result

Use math.log10() when the required logarithm base is 10.

math.log()base eSame inputNatural-log resultmath.log10()base 10Same inputBase-10 resultreceives valuereceives valuebase ebase 10
How do the two functions differ when the same input is passed to them?

Validating the Input

Before performing a logarithm calculation, validate that the input is positive. The source specifies that logarithms are undefined for zero and negative values. This validation belongs before the logarithm call in your reasoning: first check whether the input is valid, then select the appropriate logarithm function and calculate the result.

Input receivedPositive inputCalculate logarithmZeroReject inputNegative inputReject inputvalidatevalidatevalidatecontinueundefinedundefined
What happens next when a logarithm input is positive, zero, or negative?

Mistakes in Function Selection

  • Common mistake

    Assuming math.log() means base 10

    What it looks like

    Choosing math.log() for a calculation that requires a base-10 logarithm

    Why it fails

    math.log() computes the natural logarithm by default, not the base-10 logarithm.

    Do this instead

    Use math.log10() when the required base is 10.

  • Common mistake

    Ignoring the optional base parameter

    What it looks like

    Treating every math.log() call as though it must use base e

    Why it fails

    math.log(value, base) enables a custom base.

    Do this instead

    Check whether a base argument is supplied before deciding which base the call uses.

  • Common mistake

    Passing zero or a negative value

    What it looks like

    Attempting a logarithm calculation without checking the input sign

    Why it fails

    Logarithms are undefined for zero and negative values.

    Do this instead

    Validate that the input is positive before calculating.

Practice the Trace

MEDIUM

For each situation, decide which function or form of the function matches the requirement: a base-10 logarithm for a common logarithmic scale; a natural logarithm for exponential modeling; a specialized base-2 calculation; or an input that must be rejected before calculation because it is not positive.

Hints
  • Match the requested application to the base described in the source.
  • Remember that math.log() uses base e when no optional base is supplied.
  • A custom base can be supplied to math.log(value, base).
  • Check positivity before selecting a logarithm function.

What do you think happens?

A logarithm call is intended for a decibel-related calculation. Which function should be selected?

  • math.log10()

  • math.log() with no base argument

  • Reject every positive input

Reveal answer
Answer:

math.log10()

The source identifies math.log10() as the base-10 logarithm and connects base-10 logarithms with decibels and other common logarithmic scales.

Key Takeaways

  1. 1
    math.log10() computes base-10 logarithms and is suited to decibels, pH scales, and other common logarithmic scales.
  2. 2
    math.log() computes natural logarithms with base e when no optional base is supplied.
  3. 3
    math.log(value, base) supports a custom base, including specialized uses such as base 2.
  4. 4
    Trace the function name and its arguments before interpreting the result.
  5. 5
    Validate that the input is positive because logarithms are undefined for zero and negative values.

At a glance

  • Choose the logarithm function by identifying the required base.
  • Use math.log10() for base-10 applications such as decibels and pH scales.
  • Use math.log() for natural logarithms, or provide its optional base for a custom base.
  • Validate that logarithm inputs are positive before calculating.

Related concepts

More concepts starting with E