np.cos() behaves exactly like np.sin() in terms of expecting radians and being bounded between -1 and 1, but computes the cosine instead of the sine — the two are phase-shifted versions of the same underlying wave, with cos(x) equal to sin(x + pi/2). Together, sin and cos are the building blocks for representing rotations, oscillations, and periodic signals, and they satisfy the identity sin(x) squared plus cos(x) squared equals 1 for any real x.
1Understanding np.cos()
np.cos() behaves exactly like np.sin() in terms of expecting radians and being bounded between -1 and 1, but computes the cosine instead of the sine — the two are phase-shifted versions of the same underlying wave, with cos(x) equal to sin(x + pi/2). Together, sin and cos are the building blocks for representing rotations, oscillations, and periodic signals, and they satisfy the identity sin(x) squared plus cos(x) squared equals 1 for any real x.
Just like np.sin(), np.cos() expects radians, not degrees — use np.radians() to convert degree values first if that's the form your input data is in.
import numpy as np
angles = np.array([0, np.pi / 2, np.pi])
print(np.cos(angles))2Practical Example
Here is a real-world application of np.cos() showing how it is used in production NumPy code.
import numpy as np
theta = np.linspace(0, 2 * np.pi, 4, endpoint=False)
x = np.cos(theta)
y = np.sin(theta)
print(np.round(x, 2))
print(np.round(y, 2))3Best Practices
Follow these guidelines when working with np.cos():
1. Convert degree values to radians with np.radians() before calling np.cos(), the same as for np.sin()
2. Use np.cos() and np.sin() together for rotation calculations or generating circular/periodic coordinate data
3. Use np.isclose() rather than == when validating trigonometric identities or expected results numerically, due to floating-point precision
Tip: Just like np.sin(), np.cos() expects radians, not degrees — use np.radians() to convert degree values first if that's the form your input data is in.
import numpy as np
angles = np.array([0, np.pi / 2, np.pi])
print(np.cos(angles))