From 06773a04f6d96e32b2e4f9624a3b85ff8b6d1c45 Mon Sep 17 00:00:00 2001 From: Ronan Arraes Jardim Chagas Date: Thu, 22 Nov 2018 17:08:53 -0200 Subject: [PATCH 1/6] Add sincosd function The function `sincosd` simultaneously computes the sine and cosine in which the input angle is in degrees. --- base/exports.jl | 1 + base/math.jl | 2 +- base/special/trig.jl | 20 ++++++++++++++++++++ test/math.jl | 15 +++++++++++++++ 4 files changed, 37 insertions(+), 1 deletion(-) diff --git a/base/exports.jl b/base/exports.jl index 2d84157686774..4b24efcc24bd8 100644 --- a/base/exports.jl +++ b/base/exports.jl @@ -324,6 +324,7 @@ export sin, sinc, sincos, + sincosd, sind, sinh, sinpi, diff --git a/base/math.jl b/base/math.jl index 0a9fac443c602..9e87b66a7f394 100644 --- a/base/math.jl +++ b/base/math.jl @@ -6,7 +6,7 @@ export sin, cos, sincos, tan, sinh, cosh, tanh, asin, acos, atan, asinh, acosh, atanh, sec, csc, cot, asec, acsc, acot, sech, csch, coth, asech, acsch, acoth, sinpi, cospi, sinc, cosc, - cosd, cotd, cscd, secd, sind, tand, + cosd, cotd, cscd, secd, sind, tand, sincosd, acosd, acotd, acscd, asecd, asind, atand, rad2deg, deg2rad, log, log2, log10, log1p, exponent, exp, exp2, exp10, expm1, diff --git a/base/special/trig.jl b/base/special/trig.jl index 882023069ec5a..b4b2f9eba9303 100644 --- a/base/special/trig.jl +++ b/base/special/trig.jl @@ -1073,6 +1073,26 @@ end tand(x::Real) = sind(x) / cosd(x) +""" + sincosd(x) + +Simultaneously compute the sine and cosine of `x`, where `x` is in degrees. + +""" +@inline function sincosd(x::Real) + if isinf(x) + return throw(DomainError(x, "sincosd(x) is only defined for finite `x`.")) + elseif isnan(x) + return ( oftype(x,NaN), oftype(x,NaN) ) + end + + # It turns out that calling those functions separately yielded better + # performance than considering each case and calling `sincos_kernel`. + ( sind(x), cosd(x) ) +end + +sincosd(::Missing) = (missing, missing) + for (fd, f, fn) in ((:sind, :sin, "sine"), (:cosd, :cos, "cosine"), (:tand, :tan, "tangent")) name = string(fd) @eval begin diff --git a/test/math.jl b/test/math.jl index 9887f7c7a1289..13cc2091a66fb 100644 --- a/test/math.jl +++ b/test/math.jl @@ -348,9 +348,14 @@ end @testset "degree-based trig functions" begin @testset "$T" for T = (Float32,Float64,Rational{Int}) fT = typeof(float(one(T))) + fTsc = typeof( (float(one(T)), float(one(T))) ) for x = -400:40:400 @test sind(convert(T,x))::fT ≈ convert(fT,sin(pi/180*x)) atol=eps(deg2rad(convert(fT,x))) @test cosd(convert(T,x))::fT ≈ convert(fT,cos(pi/180*x)) atol=eps(deg2rad(convert(fT,x))) + + s,c = sincosd(convert(T,x)) + @test s::fT ≈ convert(fT,sin(pi/180*x)) atol=eps(deg2rad(convert(fT,x))) + @test c::fT ≈ convert(fT,cos(pi/180*x)) atol=eps(deg2rad(convert(fT,x))) end @testset "sind" begin @test sind(convert(T,0.0))::fT === zero(fT) @@ -366,6 +371,16 @@ end @test cosd(convert(T,-90))::fT === zero(fT) @test cosd(convert(T,-270))::fT === zero(fT) end + @testset "sincosd" begin + @test sincosd(convert(T,-360))::fTsc === ( -zero(fT), one(fT) ) + @test sincosd(convert(T,-270))::fTsc === ( one(fT), zero(fT) ) + @test sincosd(convert(T,-180))::fTsc === ( -zero(fT), -one(fT) ) + @test sincosd(convert(T, -90))::fTsc === ( -one(fT), zero(fT) ) + @test sincosd(convert(T, 0))::fTsc === ( zero(fT), one(fT) ) + @test sincosd(convert(T, 90))::fTsc === ( one(fT), zero(fT) ) + @test sincosd(convert(T, 180))::fTsc === ( zero(fT), -one(fT) ) + @test sincosd(convert(T, 270))::fTsc === ( -one(fT), zero(fT) ) + end @testset "sinpi and cospi" begin for x = -3:0.3:3 From 8905370ff4b1538d42218103c993a777efb5db3d Mon Sep 17 00:00:00 2001 From: Lyndon White Date: Sun, 25 Nov 2018 17:45:26 -0200 Subject: [PATCH 2/6] Apply suggestions from code review Co-Authored-By: ronisbr --- base/special/trig.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/base/special/trig.jl b/base/special/trig.jl index b4b2f9eba9303..5df2282a8e7c8 100644 --- a/base/special/trig.jl +++ b/base/special/trig.jl @@ -1083,12 +1083,12 @@ Simultaneously compute the sine and cosine of `x`, where `x` is in degrees. if isinf(x) return throw(DomainError(x, "sincosd(x) is only defined for finite `x`.")) elseif isnan(x) - return ( oftype(x,NaN), oftype(x,NaN) ) + return (oftype(x,NaN), oftype(x,NaN)) end # It turns out that calling those functions separately yielded better # performance than considering each case and calling `sincos_kernel`. - ( sind(x), cosd(x) ) + return (sind(x), cosd(x)) end sincosd(::Missing) = (missing, missing) From 5d8aa1d601664250db9a70a1127694cc0bb363c1 Mon Sep 17 00:00:00 2001 From: Ronan Arraes Jardim Chagas Date: Sat, 13 Apr 2019 10:57:12 -0300 Subject: [PATCH 3/6] Remove @inline annotation from sincosd --- base/special/trig.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/base/special/trig.jl b/base/special/trig.jl index 5df2282a8e7c8..63ec4103a0e6c 100644 --- a/base/special/trig.jl +++ b/base/special/trig.jl @@ -1079,7 +1079,7 @@ tand(x::Real) = sind(x) / cosd(x) Simultaneously compute the sine and cosine of `x`, where `x` is in degrees. """ -@inline function sincosd(x::Real) +function sincosd(x::Real) if isinf(x) return throw(DomainError(x, "sincosd(x) is only defined for finite `x`.")) elseif isnan(x) From 38ef1a3770efaa454ce39010df76e6f038059d87 Mon Sep 17 00:00:00 2001 From: Ronan Arraes Jardim Chagas Date: Tue, 28 May 2019 09:08:52 -0300 Subject: [PATCH 4/6] Add compat annotation for sincosd --- base/special/trig.jl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/base/special/trig.jl b/base/special/trig.jl index 63ec4103a0e6c..4a9eb226fe652 100644 --- a/base/special/trig.jl +++ b/base/special/trig.jl @@ -1078,6 +1078,8 @@ tand(x::Real) = sind(x) / cosd(x) Simultaneously compute the sine and cosine of `x`, where `x` is in degrees. +!!! compat "Julia 1.3" + This function requires at least Julia 1.3. """ function sincosd(x::Real) if isinf(x) From 5f20cbed232b7e3bb3502325f00907f20fbd7807 Mon Sep 17 00:00:00 2001 From: Ronan Arraes Jardim Chagas Date: Tue, 28 May 2019 09:13:43 -0300 Subject: [PATCH 5/6] Add NEWS for sincosd --- NEWS.md | 1 + 1 file changed, 1 insertion(+) diff --git a/NEWS.md b/NEWS.md index 04ee2158d70d3..a68bbf6bca4c5 100644 --- a/NEWS.md +++ b/NEWS.md @@ -5,6 +5,7 @@ New language features --------------------- * Support for Unicode 12.1.0 ([#32002]). +* Added `sincosd(x)` to simultaneously compute the sine and cosine of `x`, where `x` is in degrees. Language changes ---------------- From 81632d5d14788a281de39582fa2cddcce661ae3a Mon Sep 17 00:00:00 2001 From: Ronan Arraes Jardim Chagas Date: Sat, 3 Aug 2019 17:01:46 -0300 Subject: [PATCH 6/6] Update NEWS.md Co-Authored-By: Alex Arslan --- NEWS.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index 0e24d26e3017c..a4eca97480707 100644 --- a/NEWS.md +++ b/NEWS.md @@ -6,7 +6,7 @@ New language features * Support for Unicode 12.1.0 ([#32002]). * Methods can now be added to an abstract type ([#31916]). -* Added `sincosd(x)` to simultaneously compute the sine and cosine of `x`, where `x` is in degrees. +* Added `sincosd(x)` to simultaneously compute the sine and cosine of `x`, where `x` is in degrees ([#30134]). Language changes ----------------