Concepts / Introduction to the Math Module

Introduction to the Math Module

In Python, math.pi provides an approximation of π accurate to about 15 decimal places.

  • 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 Angle-Unit Trap

When you use Python's trigonometric functions, the number you provide is interpreted as a radian measurement. It is not interpreted as a degree measurement. This matters because a value such as 45 may look like 45 degrees to a person, while Python interprets it as 45 radians. Passing degrees directly to a trigonometric function therefore produces an incorrect result for the intended angle.

360 degreesfull circle45 degreesdegree measurementsin, cos, tanradian arguments2π radiansfull circle0.785 radiansequivalent anglesame scalesame scaleaccepted input
How does the same angle measurement map differently between degrees and radians, and why do Python's trigonometric functions expect radians?

From Degrees to Radians

The conversion is based on the fact that a full circle is 360 degrees and also 2π radians. To convert any degree measurement, divide the degree value by 360.0 and multiply by 2 * math.pi. This scales the measurement from the degree system into the radian system.

Formula, step by step
  1. 1
    Start with the angle measured in degrees.
  2. 2
    Divide the degree value by 360.0 because 360 degrees represents one full circle.
  3. 3
    Multiply by 2 * math.pi because one full circle is 2π radians.
  4. 4
    Use the resulting value as the argument for the trigonometric function.
45 degreesstarting measurementDivide by 360.045 / 360.0Multiply by 2πscale to radians0.785 radiansequivalent measurementdegree valuefraction of a full circleconverted value
What happens step by step when a degree value is converted into its equivalent radian value?

A 45-Degree Calculation

Worked example

Converting 45 degrees before finding its sine

Find the radian equivalent of 45 degrees and use that value for a sine calculation.

  1. 1
    Begin with the degree measurement

    The starting angle is 45 degrees.

  2. 2
    Apply the conversion formula

    Calculate 45 / 360.0 * 2 * math.pi.

  3. 3
    Read the converted angle

    The result is approximately 0.785 radians.

  4. 4
    Use the radian value

    Pass approximately 0.785 radians to the sine function rather than passing 45 directly.

Result

The sine calculation uses approximately 0.785 radians and produces approximately 0.707, the mathematically correct sine of 45 degrees.

What do you think happens?

If a trigonometric function receives 45 directly, will Python treat that value as 45 degrees or as 45 radians?

  • 45 degrees

  • 45 radians

Reveal answer
Answer:

45 radians

Python's trigonometric functions require radian arguments. Therefore, passing 45 directly does not calculate the trigonometric value for 45 degrees. Convert the degree value first.

The name radians is useful because it records the unit expected by the trigonometric function. After converting 45 degrees, the approximate value 0.785 is a radian measurement. That converted value is the one to use for the sine calculation, which gives approximately 0.707.

Why math.pi Matters

The conversion formula uses π because radians define a full circle as 2π. In Python, math.pi provides an approximation of π accurate to about 15 decimal places. This is more than sufficient for practical calculations and is more accurate than replacing π with a short hardcoded approximation such as 3.14159.

360 degreesfull circle180 degreeshalf circle2π radiansfull circleπ radianshalf circlemath.piapproximately 15-digitprecisionsame full circlesame half circlerepresented by
How does math.pi connect a full circle, 180 degrees, and π radians in a trigonometric calculation?

Degree-Radian Mistakes

45 degreesintended angle45 degreesintended angle45interpreted as radians0.785 radiansconverted inputpassed without conversionconverted before use
What changes when a degree value is passed directly to a function that expects radians?
  • Common mistake

    Passing a degree value directly to sin, cos, or tan.

    What it looks like

    Using 45 as though the function will interpret it as 45 degrees.

    Why it fails

    Python interprets the argument as 45 radians, not 45 degrees, so the result is not the trigonometric value for the intended angle.

    Do this instead

    Convert the degree value with degrees / 360.0 * 2 * math.pi before passing it to the function.

  • Common mistake

    Using a hardcoded short approximation for π.

    What it looks like

    Replacing math.pi with 3.14159 in the conversion.

    Why it fails

    The hardcoded value has less precision than the constant supplied by the math module.

    Do this instead

    Use math.pi, which is accurate to about 15 decimal places.

  • Common mistake

    Forgetting which unit a value represents.

    What it looks like

    Treating the converted value 0.785 as though it were still a degree measurement.

    Why it fails

    The value produced by the conversion is in radians, and the trigonometric function expects that radian value.

    Do this instead

    Use clear names such as degrees and radians and keep track of the unit at each step.

Conversion Practice

EASY

A calculation needs the sine of a 90-degree angle. Describe the conversion you must perform before using the trigonometric function, and identify the Python constant you should use for π.

Hints
  • Python's trigonometric functions require radians.
  • Use degrees / 360.0 * 2 * math.pi.
  • The required constant is math.pi.
MEDIUM

A result seems wrong when calculating the sine of 45 degrees. Check whether the value 45 was passed directly to the trigonometric function. If it was, explain why that is a unit mistake and state the corrected approach.

Hints
  • Ask how Python interprets the number 45.
  • The direct value is treated as radians.
  • Convert 45 degrees to approximately 0.785 radians before calculating the sine.

Key Takeaways

  1. 1
    Python's trigonometric functions, including sin, cos, and tan, expect radians rather than degrees.
  2. 2
    Convert degrees with radians = degrees / 360.0 * 2 * math.pi.
  3. 3
    A 45-degree angle converts to approximately 0.785 radians, whose sine is approximately 0.707.
  4. 4
    Use math.pi instead of a hardcoded approximation for more accurate calculations.
  5. 5
    Passing degrees directly to a trigonometric function produces an incorrect result for the intended angle.

At a glance

  • Python's trigonometric functions use radians, not degrees.
  • Convert a degree measurement by dividing by 360.0 and multiplying by 2 * math.pi.
  • Use math.pi for an accurate representation of π.
  • A direct degree value is interpreted as radians and can produce an incorrect result.
  • Track angle units carefully by naming and converting values before calculation.

More concepts starting with I