Whether for physical simulations, statistical processing, or controlling character movement in game development, there are many situations where trigonometric functions (sine, cosine, tangent) are required in programming.
Python’s standard library math module makes these calculations easy. However, you must be careful about the unit of the angle passed as an argument.
This article explains the basic usage of trigonometric functions and how to convert between degrees and radians.
Basic Trigonometric Functions
The math module provides the following three main functions:
- math.sin(x): Returns the sine.
- math.cos(x): Returns the cosine.
- math.tan(x): Returns the tangent.
[Important] Arguments must be specified in “Radians”
The argument x passed to these functions must be in “radians”, not the “degrees (°)” used in daily life.
- 180 degrees = π radians
- 360 degrees = 2π radians
If you want to calculate using values like “30 degrees” or “45 degrees,” you need to convert them to radians beforehand.
Converting Between Degrees and Radians
While you can calculate this using the formula ×π/180, the math module provides dedicated functions for conversion.
- math.radians(degrees): Converts degrees to radians.
- math.degrees(radians): Converts radians to degrees.
Practical Code Examples
1. Calculating Trigonometric Functions by Specifying Angles (Degrees)
Here is an example calculating sine, cosine, and tangent for an angle of 60 degrees.
import math
# Angle (in degrees)
angle_in_degrees = 60
# Convert degrees to radians
angle_in_radians = math.radians(angle_in_degrees)
print(f"Angle: {angle_in_degrees} degrees ({angle_in_radians:.4f} rad)")
# Calculate trigonometric functions
val_sin = math.sin(angle_in_radians)
val_cos = math.cos(angle_in_radians)
val_tan = math.tan(angle_in_radians)
print(f"sin(60°): {val_sin:.4f}")
print(f"cos(60°): {val_cos:.4f}")
print(f"tan(60°): {val_tan:.4f}")
Execution Result:
Angle: 60 degrees (1.0472 rad)
sin(60°): 0.8660
cos(60°): 0.5000
tan(60°): 1.7321
Since sin(60∘)=3/2≈0.866 and cos(60∘)=1/2=0.5, we can see that the calculation is correct.
2. Application: Finding the Length of a Side in a Right Triangle
Using trigonometric functions, you can find the length of other sides from an angle and the length of one side.
Example: If the angle of elevation when looking up at a tree from a certain point is 30 degrees, and the distance to the tree is 10 meters, what is the height of the tree? (Height = Distance ×tan(θ))
import math
# Distance to the tree (meters)
distance = 10.0
# Angle of elevation (degrees)
elevation_angle = 30.0
# Convert angle to radians
theta = math.radians(elevation_angle)
# Calculate height (using tan)
tree_height = distance * math.tan(theta)
print(f"Distance: {distance}m, Angle: {elevation_angle} degrees")
print(f"Tree height: {tree_height:.2f}m")
Execution Result:
Distance: 10.0m, Angle: 30.0 degrees
Tree height: 5.77m
Summary
import mathis required to use trigonometric functions in Python.- Use
math.sin(),math.cos(), andmath.tan(). - Arguments must be specified in radians.
- If you are using degrees, convert them using
math.radians()before passing them to the function.
