The math.sin(x) function computes the trigonometric sine of an angle x measured in radians. The return value is a floating-point number strictly bounded in the range [-1.0, 1.0], corresponding to the vertical y-coordinate on a standard unit circle.
Companion Functions: For the horizontal unit coordinate, see math.cos(). To convert a sine ratio back into an angle, use the inverse arc sine math.asin().
The math.sin() function is part of the standard math module:
import math
sine_val = math.sin(x)
| Parameter | Type | Required? | Description |
|---|---|---|---|
x |
float or int |
Yes | The angle measured strictly in radians. Supports positive, negative, and zero values. |
Return Value: Returns a floating-point number (float) bounded in the interval $[-1.0, 1.0]$.
Standard angles evaluated using math.sin():
import math
print("sin(0): ", math.sin(0)) # Output: 0.0
print("sin(pi / 2): ", math.sin(math.pi / 2)) # Output: 1.0 (90 degrees)
print("sin(1 radian):", math.sin(1)) # Output: 0.8414709848078965
print("sin(3*pi / 2):", math.sin(3 * math.pi / 2))# Output: -1.0 (270 degrees)
sin(0): 0.0
sin(pi / 2): 1.0
sin(1 radian): 0.8414709848078965
sin(3*pi / 2): -1.0
A common error is passing degree values directly (such as math.sin(90)), which evaluates $90$ radians (≈ $0.894$) rather than $90^\circ$. Always convert degrees to radians first using math.radians():
import math
angles_deg = [0, 30, 45, 60, 90, 180, 270, 360]
for deg in angles_deg:
rad = math.radians(deg)
print(f"sin({deg:3d}°) = {math.sin(rad):6.3f}")
sin( 0°) = 0.000
sin( 30°) = 0.500
sin( 45°) = 0.707
sin( 60°) = 0.866
sin( 90°) = 1.000
sin(180°) = 0.000
sin(270°) = -1.000
sin(360°) = 0.000
Mathematically, $\sin(\pi) = \sin(180^\circ) = 0$. Because $\pi$ is an irrational number approximated in 64-bit IEEE 754 floats, evaluating $\sin(\pi)$ produces a tiny near-zero residue:
import math
raw_val = math.sin(math.pi)
print("sin(pi) raw: ", raw_val) # 1.2246467991473532e-16
print("Is close to 0.0?", math.isclose(raw_val, 0.0, abs_tol=1e-15)) # True
Best Practice: Avoid testing if math.sin(x) == 0:. Always use math.isclose(math.sin(x), 0.0, abs_tol=1e-12) to account for floating-point imprecision.
The sine function satisfies key fundamental mathematical identities:
import math
theta = 1.45
print("Odd symmetry: ", math.isclose(math.sin(-theta), -math.sin(theta))) # True
print("Pythagorean: ", math.isclose(math.sin(theta)**2 + math.cos(theta)**2, 1.0)) # True
Sine waves form the mathematical foundation of audio signal synthesis, alternating current (AC) electricity, and harmonic oscillation. Visualize a sine wave using Matplotlib:
import math
import matplotlib.pyplot as plt
x_vals = [i * 0.1 for i in range(0, 70)] # 0 to 7 radians (~2*pi)
y_vals = [math.sin(x) for x in x_vals]
plt.plot(x_vals, y_vals, label="sin(x)", color="#0b8043")
plt.axhline(0, color="gray", linewidth=0.8, linestyle="--")
plt.title("Harmonic Sine Wave")
plt.xlabel("Radians")
plt.ylabel("sin(x)")
plt.grid(True)
plt.show()
In physics and robotics, the displacement $y(t)$ of an oscillating spring or pendulum over time $t$ with amplitude $A$ and frequency $f$ is modeled by: $$y(t) = A \cdot \sin(2\pi f t + \phi)$$ Let's build an oscillation simulator verified with unit assertions:
import math
def calculate_shm_displacement(amplitude, frequency_hz, time_seconds, phase_radians=0.0):
omega = 2.0 * math.pi * frequency_hz
y = amplitude * math.sin(omega * time_seconds + phase_radians)
return round(y, 4)
# 1 Hz oscillator with amplitude 5.0 meters (full cycle = 1.0 second)
assert calculate_shm_displacement(5.0, 1.0, 0.0) == 0.0 # t=0s -> equilibrium
assert calculate_shm_displacement(5.0, 1.0, 0.25) == 5.0 # t=0.25s (quarter cycle, 90 deg) -> max +A
assert calculate_shm_displacement(5.0, 1.0, 0.50) == 0.0 # t=0.50s (half cycle, 180 deg) -> equilibrium
assert calculate_shm_displacement(5.0, 1.0, 0.75) == -5.0 # t=0.75s (three-quarters, 270 deg) -> min -A
assert calculate_shm_displacement(5.0, 1.0, 1.00) == 0.0 # t=1.00s (full cycle, 360 deg) -> returns to 0
print("Displacement at t=0.25s:", calculate_shm_displacement(5.0, 1.0, 0.25), "m")
print("Displacement at t=0.75s:", calculate_shm_displacement(5.0, 1.0, 0.75), "m")
print("All harmonic motion displacement assertions passed successfully!")
Open in Google Colab View on GitHub
Run all math.sin() examples interactively, test degree conversions, verify the Pythagorean identity, and model harmonic oscillations with unit assertions directly in Google Colab.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.