🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEnumpy

numpy Documentation

LOADING ENGINE...

np.sin()

AI & DATA SCIENCE // np-sin

np.sin() computes the trigonometric sine of each element in an array, with input angles interpreted in radians.

Syntax

np.sin(arr)

Deep Dive Course

np.sin(), along with np.cos() and np.tan(), expects its input in radians, not degrees — a common source of bugs for people used to degree-based trigonometry elsewhere. To convert degrees to radians before calling it, use np.radians(), and np.degrees() to convert a result back if needed. Sine is periodic and bounded between -1 and 1 for any real input, which makes it a common building block for generating smooth, repeating waveforms.

1Understanding np.sin()

np.sin(), along with np.cos() and np.tan(), expects its input in radians, not degrees — a common source of bugs for people used to degree-based trigonometry elsewhere. To convert degrees to radians before calling it, use np.radians(), and np.degrees() to convert a result back if needed. Sine is periodic and bounded between -1 and 1 for any real input, which makes it a common building block for generating smooth, repeating waveforms.

💡

Always convert degrees to radians with np.radians() before passing angle values to np.sin()/np.cos()/np.tan() — passing raw degree values directly is one of the most common trigonometry bugs in NumPy code.

editor.html
import numpy as np

angles = np.array([0, np.pi / 2, np.pi])
print(np.sin(angles))
localhost:3000

2Practical Example

Here is a real-world application of np.sin() showing how it is used in production NumPy code.

editor.html
import numpy as np

degrees = np.array([0, 90, 180])
radians = np.radians(degrees)
print(np.sin(radians))
localhost:3000

3Best Practices

Follow these guidelines when working with np.sin():

1. Convert degree values to radians with np.radians() before calling any trigonometric function, since they all expect radians

2. Use np.linspace() to generate an evenly-spaced set of angle inputs when plotting a smooth sine curve

3. Remember floating-point precision means sin(pi) evaluates to a tiny non-zero number rather than exactly 0 — use np.isclose() rather than == when checking trigonometric results against expected exact values

⚠️

Tip: Always convert degrees to radians with np.radians() before passing angle values to np.sin()/np.cos()/np.tan() — passing raw degree values directly is one of the most common trigonometry bugs in NumPy code.

editor.html
import numpy as np

angles = np.array([0, np.pi / 2, np.pi])
print(np.sin(angles))
localhost:3000

Examples

Example 01Basic Usage
import numpy as np

angles = np.array([0, np.pi / 2, np.pi])
print(np.sin(angles))
Example 02Advanced Example
import numpy as np

degrees = np.array([0, 90, 180])
radians = np.radians(degrees)
print(np.sin(radians))

Best Practices

  • Convert degree values to radians with np.radians() before calling any trigonometric function, since they all expect radians
  • Use np.linspace() to generate an evenly-spaced set of angle inputs when plotting a smooth sine curve
  • Remember floating-point precision means sin(pi) evaluates to a tiny non-zero number rather than exactly 0 — use np.isclose() rather than == when checking trigonometric results against expected exact values

Interview Question

Why does np.sin(np.pi) return a tiny number like 1.2246e-16 instead of exactly 0?

Hint: Think about floating-point representation, not a bug in NumPy's sine implementation.

np.pi itself is only a finite-precision floating-point approximation of the true mathematical constant pi, which is irrational and can't be represented exactly in binary. Since the input to np.sin() is already a tiny bit off from the true value of pi, the mathematically exact output of 0 is also perturbed by a correspondingly tiny amount, resulting in a value extremely close to, but not exactly, zero. This is a general property of floating-point computation, not specific to NumPy's sine implementation, and it's why comparing floating-point trigonometric results to exact expected values should use a tolerance-based comparison like np.isclose() instead of ==.

Exercises

MediumPractice using np.sin() in a real scenario.
View Solution
import numpy as np

angles = np.array([0, np.pi / 2, np.pi])
print(np.sin(angles))

Frequently Asked Questions

Why does np.sin(np.pi) return a tiny number like 1.2246e-16 instead of exactly 0?

np.pi itself is only a finite-precision floating-point approximation of the true mathematical constant pi, which is irrational and can't be represented exactly in binary. Since the input to np.sin() is already a tiny bit off from the true value of pi, the mathematically exact output of 0 is also perturbed by a correspondingly tiny amount, resulting in a value extremely close to, but not exactly, zero. This is a general property of floating-point computation, not specific to NumPy's sine implementation, and it's why comparing floating-point trigonometric results to exact expected values should use a tolerance-based comparison like np.isclose() instead of ==.

Related Functions

np-cosnp-tannp-isclose