Introduction to the Math Module
In Python, math.pi provides an approximation of π accurate to about 15 decimal places.
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 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.
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.
- 1Start with the angle measured in degrees.
- 2Divide the degree value by 360.0 because 360 degrees represents one full circle.
- 3Multiply by 2 *
math.pibecause one full circle is 2π radians. - 4Use the resulting value as the argument for the trigonometric function.
A 45-Degree Calculation
Converting 45 degrees before finding its sine
Find the radian equivalent of 45 degrees and use that value for a sine calculation.
- 1Begin with the degree measurement
The starting angle is 45 degrees.
- 2Apply the conversion formula
Calculate 45 / 360.0 * 2 *
math.pi. - 3Read the converted angle
The result is approximately 0.785 radians.
- 4Use the radian value
Pass approximately 0.785 radians to the sine function rather than passing 45 directly.
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?
Reveal 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.
Degree-Radian Mistakes
Common mistake Passing a degree value directly to sin, cos, or tan.
What it looks likeUsing 45 as though the function will interpret it as 45 degrees.
Why it failsPython interprets the argument as 45 radians, not 45 degrees, so the result is not the trigonometric value for the intended angle.
Do this insteadConvert 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 likeReplacing math.pi with 3.14159 in the conversion.
Why it failsThe hardcoded value has less precision than the constant supplied by the math module.
Do this insteadUse math.pi, which is accurate to about 15 decimal places.
Common mistake Forgetting which unit a value represents.
What it looks likeTreating the converted value 0.785 as though it were still a degree measurement.
Why it failsThe value produced by the conversion is in radians, and the trigonometric function expects that radian value.
Do this insteadUse clear names such as degrees and radians and keep track of the unit at each step.
Conversion Practice
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.
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
- 1Python's trigonometric functions, including sin, cos, and tan, expect radians rather than degrees.
- 2Convert degrees with radians = degrees / 360.0 * 2 *
math.pi. - 3A 45-degree angle converts to approximately 0.785 radians, whose sine is approximately 0.707.
- 4Use
math.piinstead of a hardcoded approximation for more accurate calculations. - 5Passing 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.pifor 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.