Skip to content

Commit 850c6e4

Browse files
ronisbrJeffBezanson
authored andcommitted
Add sincosd function (#30134)
The function `sincosd` simultaneously computes the sine and cosine in which the input angle is in degrees.
1 parent 3419e3c commit 850c6e4

File tree

5 files changed

+40
-1
lines changed

5 files changed

+40
-1
lines changed

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ New language features
66

77
* Support for Unicode 12.1.0 ([#32002]).
88
* Methods can now be added to an abstract type ([#31916]).
9+
* Added `sincosd(x)` to simultaneously compute the sine and cosine of `x`, where `x` is in degrees ([#30134]).
910

1011
Language changes
1112
----------------

base/exports.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,7 @@ export
324324
sin,
325325
sinc,
326326
sincos,
327+
sincosd,
327328
sind,
328329
sinh,
329330
sinpi,

base/math.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export sin, cos, sincos, tan, sinh, cosh, tanh, asin, acos, atan,
66
asinh, acosh, atanh, sec, csc, cot, asec, acsc, acot,
77
sech, csch, coth, asech, acsch, acoth,
88
sinpi, cospi, sinc, cosc,
9-
cosd, cotd, cscd, secd, sind, tand,
9+
cosd, cotd, cscd, secd, sind, tand, sincosd,
1010
acosd, acotd, acscd, asecd, asind, atand,
1111
rad2deg, deg2rad,
1212
log, log2, log10, log1p, exponent, exp, exp2, exp10, expm1,

base/special/trig.jl

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,6 +1073,28 @@ end
10731073

10741074
tand(x::Real) = sind(x) / cosd(x)
10751075

1076+
"""
1077+
sincosd(x)
1078+
1079+
Simultaneously compute the sine and cosine of `x`, where `x` is in degrees.
1080+
1081+
!!! compat "Julia 1.3"
1082+
This function requires at least Julia 1.3.
1083+
"""
1084+
function sincosd(x::Real)
1085+
if isinf(x)
1086+
return throw(DomainError(x, "sincosd(x) is only defined for finite `x`."))
1087+
elseif isnan(x)
1088+
return (oftype(x,NaN), oftype(x,NaN))
1089+
end
1090+
1091+
# It turns out that calling those functions separately yielded better
1092+
# performance than considering each case and calling `sincos_kernel`.
1093+
return (sind(x), cosd(x))
1094+
end
1095+
1096+
sincosd(::Missing) = (missing, missing)
1097+
10761098
for (fd, f, fn) in ((:sind, :sin, "sine"), (:cosd, :cos, "cosine"), (:tand, :tan, "tangent"))
10771099
name = string(fd)
10781100
@eval begin

test/math.jl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,9 +369,14 @@ end
369369
@testset "degree-based trig functions" begin
370370
@testset "$T" for T = (Float32,Float64,Rational{Int})
371371
fT = typeof(float(one(T)))
372+
fTsc = typeof( (float(one(T)), float(one(T))) )
372373
for x = -400:40:400
373374
@test sind(convert(T,x))::fT convert(fT,sin(pi/180*x)) atol=eps(deg2rad(convert(fT,x)))
374375
@test cosd(convert(T,x))::fT convert(fT,cos(pi/180*x)) atol=eps(deg2rad(convert(fT,x)))
376+
377+
s,c = sincosd(convert(T,x))
378+
@test s::fT convert(fT,sin(pi/180*x)) atol=eps(deg2rad(convert(fT,x)))
379+
@test c::fT convert(fT,cos(pi/180*x)) atol=eps(deg2rad(convert(fT,x)))
375380
end
376381
@testset "sind" begin
377382
@test sind(convert(T,0.0))::fT === zero(fT)
@@ -387,6 +392,16 @@ end
387392
@test cosd(convert(T,-90))::fT === zero(fT)
388393
@test cosd(convert(T,-270))::fT === zero(fT)
389394
end
395+
@testset "sincosd" begin
396+
@test sincosd(convert(T,-360))::fTsc === ( -zero(fT), one(fT) )
397+
@test sincosd(convert(T,-270))::fTsc === ( one(fT), zero(fT) )
398+
@test sincosd(convert(T,-180))::fTsc === ( -zero(fT), -one(fT) )
399+
@test sincosd(convert(T, -90))::fTsc === ( -one(fT), zero(fT) )
400+
@test sincosd(convert(T, 0))::fTsc === ( zero(fT), one(fT) )
401+
@test sincosd(convert(T, 90))::fTsc === ( one(fT), zero(fT) )
402+
@test sincosd(convert(T, 180))::fTsc === ( zero(fT), -one(fT) )
403+
@test sincosd(convert(T, 270))::fTsc === ( -one(fT), zero(fT) )
404+
end
390405

391406
@testset "sinpi and cospi" begin
392407
for x = -3:0.3:3

0 commit comments

Comments
 (0)