Main Content

sinpi

Compute sin(X*pi) accurately

Description

Y = sinpi(X) computes sin(X*pi) without explicitly computing X*pi. This calculation is more accurate than sin(X*pi) because the floating-point value of pi is an approximation of π. In particular:

  • For integers, sinpi(n) is exactly zero.

  • For odd integers, sinpi(n/2) is +1 or -1.

example

Examples

collapse all

Compare the accuracy of sinpi(X) vs. sin(X*pi).

Create a vector of values.

X = [0 1/2 1 3/2 2];

Calculate the sine of X*pi using the normal sin function.

Y = sin(X*pi)
Y = 1×5

         0    1.0000    0.0000   -1.0000   -0.0000

The results contain small numerical errors due to the fact that pi is a floating-point approximation of the true value of π. For instance, Y(3) is not exactly zero even though sin(π)=0.

Y(3)
ans = 
1.2246e-16

Use sinpi to calculate the same values. In this case, the results are exact.

Z = sinpi(X)
Z = 1×5

     0     1     0    -1     0

Z(3)
ans = 
0

Input Arguments

collapse all

Input array, specified as a scalar, vector, matrix, multidimensional array, table, or timetable.

Data Types: single | double | table | timetable
Complex Number Support: Yes

Extended Capabilities

C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.

Thread-Based Environment
Run code in the background using MATLAB® backgroundPool or accelerate code with Parallel Computing Toolbox™ ThreadPool.

Version History

Introduced in R2018b

expand all

See Also

| |