Skip to contents

These functions give the obvious trigonometric functions. They respectively compute the cosine, sine, tangent, arc-cosine, arc-sine, arc-tangent, and the two-argument arc-tangent.

cospi(x), sinpi(x), and tanpi(x), compute cos(pi*x), sin(pi*x), and tan(pi*x).

Usage

# S3 method for default
cos(x)

# S3 method for default
sin(x)

# S3 method for default
acos(x)

# S3 method for default
asin(x)

# S3 method for default
atan(x)

# S3 method for default
atan2(x, y)

Arguments

x, y

numeric or complex vectors.

Value

tanpi(0.5) is NaN. Similarly for other inputs with fractional part 0.5.

Details

The arc-tangent of two arguments atan2(y, x) returns the angle between the x-axis and the vector from the origin to \((x, y)\), i.e., for positive arguments atan2(y, x) == atan(y/x).

Angles are in radians, not degrees, for the standard versions (i.e., a right angle is \(\pi/2\)), and in ‘half-rotations’ for cospi etc.

cospi(x), sinpi(x), and tanpi(x) are accurate for x values which are multiples of a half.

All except atan2 are internal generic primitive functions: methods can be defined for them individually or via the Math group generic.

These are all wrappers to system calls of the same name (with prefix c for complex arguments) where available. (cospi, sinpi, and tanpi are part of a C11 extension and provided by e.g. macOS and Solaris: where not yet available call to cos etc are used, with special cases for multiples of a half.)

See also

Author

R core team and contributors

Examples

x <- seq(-3, 7, by = 1/8)
tx <- cbind(x, cos(pi*x), cospi(x), sin(pi*x), sinpi(x),
               tan(pi*x), tanpi(x), deparse.level=2)
#> Warning: NaNs produced
op <- options(digits = 4, width = 90) # for nice formatting
head(tx)
#>           x cos(pi * x... cospi(x) sin(pi * x... sinpi(x) tan(pi * x... tanpi(x)
#> [1,] -3.000    -1.000e+00  -1.0000    -3.674e-16   0.0000     3.674e-16   0.0000
#> [2,] -2.875    -9.239e-01  -0.9239    -3.827e-01  -0.3827     4.142e-01   0.4142
#> [3,] -2.750    -7.071e-01  -0.7071    -7.071e-01  -0.7071     1.000e+00   1.0000
#> [4,] -2.625    -3.827e-01  -0.3827    -9.239e-01  -0.9239     2.414e+00   2.4142
#> [5,] -2.500     3.062e-16   0.0000    -1.000e+00  -1.0000    -3.266e+15      NaN
#> [6,] -2.375     3.827e-01   0.3827    -9.239e-01  -0.9239    -2.414e+00  -2.4142
tx[ (x %% 1) %in% c(0, 0.5) ,]
#>          x cos(pi * x... cospi(x) sin(pi * x... sinpi(x) tan(pi * x... tanpi(x)
#>  [1,] -3.0    -1.000e+00       -1    -3.674e-16        0     3.674e-16        0
#>  [2,] -2.5     3.062e-16        0    -1.000e+00       -1    -3.266e+15      NaN
#>  [3,] -2.0     1.000e+00        1     2.449e-16        0     2.449e-16        0
#>  [4,] -1.5    -1.837e-16        0     1.000e+00        1    -5.444e+15      NaN
#>  [5,] -1.0    -1.000e+00       -1    -1.225e-16        0     1.225e-16        0
#>  [6,] -0.5     6.123e-17        0    -1.000e+00       -1    -1.633e+16      NaN
#>  [7,]  0.0     1.000e+00        1     0.000e+00        0     0.000e+00        0
#>  [8,]  0.5     6.123e-17        0     1.000e+00        1     1.633e+16      NaN
#>  [9,]  1.0    -1.000e+00       -1     1.225e-16        0    -1.225e-16        0
#> [10,]  1.5    -1.837e-16        0    -1.000e+00       -1     5.444e+15      NaN
#> [11,]  2.0     1.000e+00        1    -2.449e-16        0    -2.449e-16        0
#> [12,]  2.5     3.062e-16        0     1.000e+00        1     3.266e+15      NaN
#> [13,]  3.0    -1.000e+00       -1     3.674e-16        0    -3.674e-16        0
#> [14,]  3.5    -4.286e-16        0    -1.000e+00       -1     2.333e+15      NaN
#> [15,]  4.0     1.000e+00        1    -4.899e-16        0    -4.899e-16        0
#> [16,]  4.5     5.511e-16        0     1.000e+00        1     1.815e+15      NaN
#> [17,]  5.0    -1.000e+00       -1     6.123e-16        0    -6.123e-16        0
#> [18,]  5.5    -2.450e-15        0    -1.000e+00       -1     4.082e+14      NaN
#> [19,]  6.0     1.000e+00        1    -7.348e-16        0    -7.348e-16        0
#> [20,]  6.5    -9.803e-16        0     1.000e+00        1    -1.020e+15      NaN
#> [21,]  7.0    -1.000e+00       -1     8.573e-16        0    -8.573e-16        0
options(op)