Concepts / Basic Mathematical Functions

Basic Mathematical Functions

An angle such as 45 can be written in degrees, but Python's trigonometric functions interpret a number as radians.

  • 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 Unit Hidden in an Angle

An angle such as 45 can be written in degrees, but Python's trigonometric functions interpret a number as radians. This means math.sin(45) does not calculate the sine of 45 degrees. It calculates the sine of 45 radians. Before calling math.sin(), math.cos(), or math.tan(), make sure the value is expressed in radians.

45 degreesdegree measurement0.785 radiansconverted input0.707sine result45 radiansdirect inputdifferent resultnot sine of 45 degreesconvertmath.sinmath.sin
What changes when the same number is interpreted as degrees instead of radians by a Python trigonometric function?

A 45-Degree Trace

What do you think happens?

A 45-degree angle is converted to radians and then passed to math.sin(). What result should the calculation approach?

  • Approximately 0.707

  • Exactly 45

  • A result based on treating 45 as 45 radians

Reveal answer
Answer:

Approximately 0.707

The 45-degree value is first converted to approximately 0.785 radians. The sine of that radian value is approximately 0.707.

Code walkthrough
import math

radians = 45 / 360.0 * 2 * math.pi
result = math.sin(radians)
print(result)
  1. 1
    The import makes the math module available.
  2. 2
    The conversion divides 45 by 360.0 and multiplies by 2 * math.pi. This produces approximately 0.785 radians.
  3. 3
    math.sin() receives the converted radian value rather than the original degree value.
  4. 4
    The printed result is approximately 0.707.
  • The math module provides math.pi and math.sin().
  • The degree value 45 is converted to radians.
  • The converted value is passed to math.sin().
  • The result is approximately the sine of 45 degrees.
Output
Approximately 0.707
Worked example

Converting 45 Degrees

Find the radian equivalent of 45 degrees and use it as the input to a sine calculation.

  1. 1
    Start with the degree measurement

    The angle is 45 degrees.

  2. 2
    Scale to the radian measurement

    Use 45 / 360.0 * 2 * math.pi. Since a full circle is 360 degrees and 2π radians, this preserves the angle's position within a full circle.

  3. 3
    Evaluate the converted angle

    The conversion produces approximately 0.785 radians. Passing this value to math.sin() produces approximately 0.707.

Result

The sine of 45 degrees is calculated by passing its radian equivalent, approximately 0.785, to math.sin(). The result is approximately 0.707.

45degrees45 / 360.0fraction of a full circle0.785radiansdivide by 360.0multiply by 2 * math.pi
How does a degree measurement map to its equivalent radian measurement using radians = degrees / 360.0 * 2 * π?

The Conversion Formula

The conversion works because a full circle has two equivalent descriptions: 360 degrees and 2π radians. Dividing a degree measurement by 360.0 finds what fraction of a full circle it represents. Multiplying that fraction by 2π expresses the same position on the radian scale.

Formula, step by step
  1. 1
    Begin with the angle measured in degrees.
  2. 2
    Divide by 360.0 to express the angle as a fraction of a full circle.
  3. 3
    Multiply by 2 * math.pi because a full circle equals 2π radians.
  4. 4
    Use the resulting value as the argument to a trigonometric function.
Definition

math.pi is Python's representation of π. The source describes it as accurate to about 15 decimal places, making it suitable for practical trigonometric calculations.

360 degreesfull circlemath.piPython's π value2π radiansfull circleradian inputfor math.sin, math.cos, ormath.tansame full rotationforms 2 * math.piprovides scale
How does math.pi connect a full 360-degree rotation to 2π radians?

Repairing Unit Mix-Ups

Before
import math

result = math.sin(45)

The function receives 45 as radians, even though the intended angle is 45 degrees.

After
import math

radians = 45 / 360.0 * 2 * math.pi
result = math.sin(radians)

The degree value is converted to approximately 0.785 radians before the function call.

45intended as degreesmath.sinreceives radiansincorrect resultnot sine of 45 degrees45 degreesoriginal measurement0.785 radiansconverted input0.707approximatelypassed directlyevaluatesconvertmath.sin
What happens when a degree value is passed directly to a Python trigonometric function instead of being converted first?
  • Common mistake

    Passing a degree value directly to a trigonometric function.

    What it looks like
    math.sin(45)
    Why it fails

    Python interprets 45 as 45 radians, not 45 degrees.

    Do this instead

    Convert the degree value first, then pass the radian result to the function.

  • Common mistake

    Assuming a variable's numeric value reveals its unit.

    What it looks like
    angle = 45
    Why it fails

    The number 45 does not tell Python whether the intended unit is degrees or radians.

    Do this instead

    Use a clear name such as radians after conversion, and keep track of the unit at every step.

  • Common mistake

    Hardcoding a rough value for π.

    What it looks like
    radians = degrees / 360.0 * 2 * 3.14159
    Why it fails

    A typed approximation is less accurate than Python's math.pi constant.

    Do this instead

    Use math.pi in the conversion formula.

Best practice

Treat unit conversion as a separate step. Start with the degree measurement, calculate radians using degrees / 360.0 * 2 * math.pi, and only then call math.sin(), math.cos(), or math.tan(). Naming the converted value radians makes the expected input unit visible in the calculation.

Practice Check

EASY

A calculation needs the sine of 90 degrees. Write the conversion step that produces radians, then identify which value should be passed to math.sin().

Hints
  • Use radians = degrees / 360.0 * 2 * math.pi.
  • Substitute 90 for degrees.
  • Pass the converted radian value to math.sin(), not the original 90.
MEDIUM

A learner writes math.cos(45) while intending to calculate the cosine of 45 degrees. Explain the unit mistake and describe the correction without changing the intended angle.

Hints
  • Ask what unit math.cos() expects.
  • Convert 45 degrees to radians before calling math.cos().
  • Use math.pi rather than a manually typed approximation of π.

Takeaways

  1. 1
    Python's trigonometric functions take radians, not degrees.
  2. 2
    Convert degrees with radians = degrees / 360.0 * 2 * math.pi.
  3. 3
    A full circle is 360 degrees or 2π radians.
  4. 4
    Use math.pi for an accurate representation of π.
  5. 5
    Passing degrees directly to math.sin(), math.cos(), or math.tan() produces an incorrect result for the intended degree measurement.

At a glance

  • Python's trigonometric functions interpret their arguments as radians.
  • Convert a degree measurement before using it in a trigonometric calculation.
  • The conversion formula is radians = degrees / 360.0 * 2 * math.pi.
  • math.pi provides an accurate value for π and should be preferred over a hardcoded approximation.
  • Clear unit tracking prevents incorrect results caused by mixing degrees and radians.

More concepts starting with B