How to Use np.sin() in NumPy

A NumPy calculation may need the sine of one angle or a whole set of angles. The np.sin() function computes sine from radian inputs and applies the calculation element by element to arrays.

This article shows how to calculate sine and convert degree inputs with np.deg2rad().

TL;DR

np.sin() computes sine for angles in radians and applies the operation element by element to array inputs.

  • Convert degrees with np.deg2rad() before calling np.sin().
  • Expect results close to zero, rather than exactly zero, for some mathematically zero angles.

What Is np.sin()?

np.sin() is NumPy’s elementwise sine function, a universal function (ufunc) that accepts a number or array-like input. NumPy expects angle inputs in radians.

A radian measures an angle as arc length divided by radius. A half-turn is π radians, and a quarter-turn is π/2.

On the unit circle, sine is the vertical coordinate of the point at angle θ. Its value stays between −1 and 1, repeats every 2π, and equals zero at multiples of π.

Integer inputs still represent radians, and the resulting sine values need not be integers.

To find an angle from a sine value, use np.arcsin(), which returns values in its principal range.

Choose np.sin() when the input is an array and the same calculation should apply to every element. A scalar-only script can use math.sin() without creating a NumPy array. The NumPy arrays guide explains the array structure used in these examples.

How to Use np.sin() Step by Step

Start with a known angle in radians, then pass it to np.sin().

Step 1: Compute sine from radians

The value π/2 represents 90 degrees in radians, so its sine is 1. The following program also evaluates several degree inputs after conversion.

import numpy as np

angles_degrees = np.array([0, 30, 90, 180])
angles_radians = np.deg2rad(angles_degrees)
print("Radians:", angles_radians)
print("Sine:", np.sin(angles_radians))
print("Direct sin(90):", np.sin(90))
print("Converted sin(90 degrees):", np.sin(np.deg2rad(90)))

Running python3 sin_demo.py prints:

Radians: [0.         0.52359878 1.57079633 3.14159265]
Sine: [0.0000000e+00 5.0000000e-01 1.0000000e+00 1.2246468e-16]
Direct sin(90): 0.8939966636005579
Converted sin(90 degrees): 1.0
NumPy sine results for radian and converted degree inputs

The first input is zero radians and the last is π radians. The final output is a tiny residual, not an exact zero. The direct call uses 90 radians.

Step 2: Convert degree arrays

Use np.deg2rad() to convert degree values. It accepts scalar and array-like input.

radians = np.deg2rad(angles_degrees)
sines = np.sin(radians)

The converted array retains the input order, so each result remains paired with its original angle.

Step 3: Sample a continuous interval

np.linspace() creates evenly spaced radian values for a curve or numerical sample. Its endpoints are included by default.

x = np.linspace(-np.pi, np.pi, 5)
y = np.sin(x)

This produces five samples from −π to π. The x values are coordinates and y contains their sine values. A plotting library can use those paired arrays to draw a curve.

np.sin() Edge Cases and Choices

Use the input type and expected numerical precision to choose a call and a comparison. The table maps common situations to a practical next step.

CaseWhat happensWhat to do
Degrees passed directly90 is treated as 90 radiansUse np.sin(np.deg2rad(90))
Near-zero resultπ-related arithmetic can leave a tiny residualUse np.isclose(value, 0) for a numerical check
Python scalar inputNumPy returns a scalar-like resultUse math.sin() when working only with scalar values
Array inputEach element is transformed independentlyUse np.sin() to retain array-oriented operations

I ran the direct and converted calls. np.sin(90) returned 0.8939966636005579, while converting first returned 1.0.

Use np.isclose(value, 0) for a near-zero check, and set its tolerance to suit the calculation’s scale and accuracy.

For a very large degree value, reduction modulo 360 can limit conversion size if full turns are equivalent in the application. Retain the original value when rotation count carries meaning.

The ufunc also accepts options such as out, which places results in an existing array, and where, which selects elements to calculate. These options are useful when an operation must reuse a destination or apply a mask. The NumPy np.sin reference documents their exact behavior.

More Ways to Prepare Sine Inputs

np.sin() takes its input in radians, so the values you prepare determine whether the output answers the question you meant to ask. This matters when angles arrive from a user interface, a spreadsheet, or a data file labeled in degrees. Keep the unit clear where values enter your calculation.

A degree-to-radian conversion applies to every entry when its input is an array. That lets the same expression serve one angle or thousands of angles without a Python loop. The output position corresponds to the input position, which is useful when you later pair sine values with timestamps, labels, or other measurements.

For example, a temperature sensor may report a phase angle in degrees while a later formula expects radians. Convert the phase array once, then use that converted array in the formula. Naming the converted values with a unit in the variable name can prevent a later expression from accidentally treating degrees as radians.

The conversion is not a sine-specific operation. np.deg2rad() changes the unit representation, while np.sin() evaluates the trigonometric function. Keeping those responsibilities separate helps check intermediate values and reuse converted angles for cosine.

  • Label incoming values with their unit.
  • Convert degrees before evaluating sine.
  • Reuse radians when another trigonometric function needs them.

Understanding the Shape and Data Flow

For array input, np.sin() applies its operation elementwise and returns corresponding results in an array. The shape communicates how those values are arranged. A one-dimensional list of four angles yields four corresponding sine values, while a two-dimensional input keeps its row-and-column layout.

This elementwise behavior differs from matrix multiplication. Sine transforms each entry independently rather than combining rows and columns. In a matrix of angles, each cell is one angle to transform, and the shape remains available for later calculations.

Python lists are accepted as array-like inputs, so a small calculation can call np.sin([0, np.pi / 2]) directly. In a longer program, explicitly creating an ndarray with np.array() makes the data structure clear and gives you an object to reuse in later NumPy operations. The NumPy arrays guide covers construction and array properties.

Shape errors usually arise later, when you combine the sine result with another array of an incompatible shape. Check both shapes before changing the sine call. Broadcasting combines compatible shapes, but does not match values by labels or physical units.

Verify that each angle is paired with its intended measurement.

CheckWhat to inspect
DimensionsCompare the shapes before combining arrays
AlignmentConfirm each angle maps to its measurement

For a single value, np.sin() is still valid, though it returns a NumPy scalar rather than an array with multiple entries. Use the standard-library math.sin() when a program only needs scalar math and already works with ordinary Python numbers. Use the NumPy ufunc when the input participates in array calculations or when elementwise behavior is useful.

Choosing Sample Points for a Sine Curve

np.linspace() is useful when you want a fixed number of evenly spaced radian samples over an interval. The earlier example takes five points from minus π to π, including both endpoints. That interval spans one full turn, so the first and last sine values are mathematically equal.

Choosing the interval and sample count is part of the modeling decision. A small count gives fewer coordinates, while a denser sample gives more points for a chart or numerical calculation.

np.sin() transforms supplied values. It does not select the sampling density or draw a curve.

When the input is expressed as degrees, build or load those degree samples first and pass them through np.deg2rad() before evaluating sine. When the interval is already described in radians, use radian endpoints directly. Mixing a degree endpoint such as 180 with a radian endpoint such as np.pi in the same sequence gives the samples an inconsistent unit and invalidates the interpretation.

Endpoints affect comparisons. A full turn returns to its starting point, so including both ends gives two mathematically equivalent positions.

That suits a line plot. A periodic calculation that treats samples as unique phases may exclude the repeated endpoint.

  • Use both endpoints when the plotted line should meet at the same value.
  • Exclude the repeated endpoint when each periodic sample must be unique.

Numerical Results Near Sine’s Zeros

Floating-point values approximate many numbers, and π cannot be represented exactly with a finite binary floating-point value. Calculations at multiples of π can therefore produce a tiny residual instead of zero. The result for π in the output is 1.2246468e-16.

That small result is not evidence that the angle conversion failed. It is a consequence of finite-precision computation. Exact equality checks such as value == 0 are therefore a poor way to decide whether a calculated value is numerically zero.

Use np.isclose(value, 0) for a tolerance-based comparison. The default tolerance is intended for general numerical comparisons, not every scientific scale. When values are very small or have different units, specify atol and rtol to reflect the acceptable error in the application, and understand how the reference value affects the relative tolerance.

A tolerance is a decision about acceptable error, not a way to make a result mathematically exact. If two computed values must be judged at a particular precision, make that precision explicit and test representative values around the boundary. Avoid rounding every result just to make a display look clean, because rounding discards information that later calculations may need.

NeedCheck
Near zeroUse np.isclose(value, 0)
Application-specific precisionSet atol and rtol deliberately
Clean displayRound only for presentation

Practical Input Checks

Before applying np.sin(), check that inputs represent angles and use consistent units. A valid Python number can still have the wrong unit for the calculation. Converting degrees to radians does not fix a mislabeled source column or a value that is not an angle.

For data loaded from a file, inspect a few entries and the associated column name or documentation. Values around 0, 90, and 180 are common degree examples, while their radian counterparts are 0, π/2, and π. This quick check can expose a unit assumption before the sine values enter a larger formula.

Also inspect the dtype when an input comes from mixed or external data. NumPy’s ufunc supports numerical arrays, and the values must be convertible to a suitable numeric representation for the operation. Text values such as a degree sign embedded in each string should be parsed into numeric values and separated from their unit before calculation.

If a computation writes results into an existing array, the ufunc’s out parameter can direct results there. A where mask selects elements for evaluation.

These arguments affect storage and selection, not units. Inputs remain radians. Check the documentation for masked locations and output initialization.

For introductory calculations, np.sin(np.deg2rad(angles_degrees)) is a clear expression. Separate variables help when debugging, checking units, or reusing converted angles. Keep a known-angle check near the calculation when the input source may change.

  • Confirm the values are angles.
  • Check the source unit and dtype.
  • Use a known angle to validate the result.

Conclusion

For further reading, see the NumPy trigonometric functions guide and the NumPy degree conversion reference.

FAQ

These answers address common questions about units and choosing a sine function.

Does np.sin() use radians or degrees?

np.sin() uses radians. Convert degrees first with np.deg2rad().

Why does np.sin(90) not equal 1?

The value 90 is interpreted as radians. Use np.sin(np.deg2rad(90)) for 90 degrees.

Can np.sin() calculate sine for an array?

Yes. It applies the sine operation to each element and returns corresponding array values.

Kumar Akshat
Kumar Akshat
Articles: 27