Basic Mathematical Functions
An angle such as 45 can be written in degrees, but Python's trigonometric functions interpret a number as radians.
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 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.
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?
Reveal 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.
import math
radians = 45 / 360.0 * 2 * math.pi
result = math.sin(radians)
print(result)- 1The import makes the math module available.
- 2The conversion divides 45 by 360.0 and multiplies by 2 *
math.pi. This produces approximately 0.785 radians. - 3
math.sin()receives the converted radian value rather than the original degree value. - 4The printed result is approximately 0.707.
- The math module provides
math.piandmath.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.
Approximately 0.707Converting 45 Degrees
Find the radian equivalent of 45 degrees and use it as the input to a sine calculation.
- 1Start with the degree measurement
The angle is 45 degrees.
- 2Scale 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. - 3Evaluate the converted angle
The conversion produces approximately 0.785 radians. Passing this value to
math.sin()produces approximately 0.707.
The sine of 45 degrees is calculated by passing its radian equivalent, approximately 0.785, to math.sin(). The result is approximately 0.707.
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.
- 1Begin with the angle measured in degrees.
- 2Divide by 360.0 to express the angle as a fraction of a full circle.
- 3Multiply by 2 *
math.pibecause a full circle equals 2π radians. - 4Use the resulting value as the argument to a trigonometric function.
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.
Repairing Unit Mix-Ups
import math
result = math.sin(45)The function receives 45 as radians, even though the intended angle is 45 degrees.
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.
Common mistake Passing a degree value directly to a trigonometric function.
What it looks likemath.sin(45)Why it failsPython interprets 45 as 45 radians, not 45 degrees.
Do this insteadConvert 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 likeangle = 45Why it failsThe number 45 does not tell Python whether the intended unit is degrees or radians.
Do this insteadUse 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 likeradians = degrees / 360.0 * 2 * 3.14159Why it failsA typed approximation is less accurate than Python's
math.piconstant.Do this insteadUse math.pi in the conversion formula.
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
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.
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.pirather than a manually typed approximation of π.
Takeaways
- 1Python's trigonometric functions take radians, not degrees.
- 2Convert degrees with radians = degrees / 360.0 * 2 *
math.pi. - 3A full circle is 360 degrees or 2π radians.
- 4Use
math.pifor an accurate representation of π. - 5Passing degrees directly to
math.sin(),math.cos(), ormath.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.piprovides an accurate value for π and should be preferred over a hardcoded approximation.- Clear unit tracking prevents incorrect results caused by mixing degrees and radians.