Batu Lab NotesPractical developer guides

Convert degrees to radians for a math API

By Batu ยท English technical notes

Also published in our Blogger archive.

Convert degrees to radians for a math API

Many interfaces display angles in degrees, but Python's trigonometric functions take radians. Passing 90 directly to math.sin therefore means 90 radians, not a quarter turn. Convert at the boundary between the degree-oriented input and the radian-oriented math API.

Here math.radians(90) produces the radian value for a right angle. That result is passed to math.sin, whose result is close to 1.0. The assertion uses math.isclose because radians and trigonometric results are floating-point values; exact equality would make the example depend on representational rounding. Formatting the result to six decimal places makes the printed output stable and easy to inspect.

math.radians converts degrees to radians, and math.sin expects its argument in radians. Both are standard-library functions and available in all modern Python 3 releases. This conversion does not normalize an angle: 450 degrees remains the radian equivalent of 450 degrees, although periodic functions may have the same result as a normalized angle. It also does not validate whether an input represents a meaningful direction in a particular domain.

See the official math.radians and trigonometric-function documentation.

AI assistance disclosure: This article was drafted with AI assistance and should be checked against the target API's angle convention.

import math

angle_degrees = 90
angle_radians = math.radians(angle_degrees)
sine = math.sin(angle_radians)

assert math.isclose(angle_radians, math.pi / 2)
assert math.isclose(sine, 1.0, abs_tol=1e-12)

print(f"sin({angle_degrees} degrees)={sine:.6f}")
sin(90 degrees)=1.000000

Sources