Skip to content

Commit 7bfad94

Browse files
authored
Merge pull request #562 from OlivierHnt/1.0-dev-trig
1.0-dev: fix stack overflow error (#513, #538)
2 parents 8b9f4fd + 2f16df2 commit 7bfad94

File tree

3 files changed

+33
-10
lines changed

3 files changed

+33
-10
lines changed

src/IntervalArithmetic.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ import Base:
2323
<, >, ==, !=, , ^, <=, >=,
2424
in, zero, one, eps, typemin, typemax, abs, abs2, min, max,
2525
sqrt, exp, log, exp2, exp10, log2, log10, inv, cbrt, hypot,
26-
sin, cos, tan, cot, csc, sec, asin, acos, atan, acot, sinpi, cospi,
26+
rad2deg, deg2rad,
27+
sin, cos, tan, cot, csc, sec, asin, acos, atan, acot, sinpi, cospi, sincospi,
2728
sinh, cosh, tanh, coth, csch, sech, asinh, acosh, atanh, acoth,
2829
union, intersect, isempty,
2930
convert, eltype, size,

src/intervals/arithmetic/trigonometric.jl

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@
22
# Section 9.1 of the IEEE Standard 1788-2015 and required for set-based flavor
33
# in Section 10.5.3
44

5+
# needed to prevent stack overflow
6+
7+
rad2deg(x::Interval) = (x / π) * 180
8+
9+
deg2rad(x::Interval) = (x * π) / 180
10+
11+
sincospi(x::Interval) = (sinpi(x), cospi(x))
12+
13+
#
14+
515
const halfpi = π / 2
616

717
half_pi(::Type{T}) where {T<:NumTypes} = unsafe_scale(convert(T, 0.5), interval(T, π))
@@ -132,10 +142,7 @@ function sin(a::Interval{Float64})
132142
end
133143
end
134144

135-
function sinpi(a::Interval{T}) where {T<:NumTypes}
136-
isempty(a) && return a
137-
return sin(a * interval(T, π))
138-
end
145+
sinpi(a::Interval) = sin(a * π)
139146

140147
"""
141148
cos(a::Interval)
@@ -225,10 +232,7 @@ function cos(a::Interval{Float64})
225232
end
226233
end
227234

228-
function cospi(a::Interval{T}) where {T<:NumTypes}
229-
isempty(a) && return a
230-
return cos(a * interval(T, π))
231-
end
235+
cospi(a::Interval) = cos(a * π)
232236

233237
"""
234238
tan(a::Interval)

test/interval_tests/trig.jl

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
using Test
22
using IntervalArithmetic
33

4+
@testset "rad2deg/deg2rad" begin
5+
@test rad2deg.. 2π) interval(180, 360)
6+
@test deg2rad(180 .. 360) interval(π, 2interval(π))
7+
end
8+
49
@testset "sin" begin
510
@test sin(interval(0.5)) interval(0.47942553860420295, 0.47942553860420301)
611
@test sin(interval(0.5, 1.67)) interval(4.7942553860420295e-01, 1.0)
@@ -37,8 +42,8 @@ end
3742

3843
@testset "sinpi" begin
3944
@test sinpi(∅)
40-
@test sinpi(0.5 .. 1.5) interval(-1 , 1)
4145
@test sinpi(1 .. 2) interval(-1 , 0)
46+
@test sinpi(0.5 .. 1.5) interval(-1 , 1)
4247
@test sinpi(0.25 .. 0.75) interval(1/sqrt(2) , 1)
4348
@test sinpi(-0.25 .. 0.25) interval(-1/sqrt(2) , 1/sqrt(2))
4449
end
@@ -51,6 +56,19 @@ end
5156
@test cospi(-0.25 .. 0.25) interval(1/sqrt(2) , 1)
5257
end
5358

59+
@testset "sincospi" begin
60+
x = sincospi(∅)
61+
@test (x[1] == ∅) & (x[2] == ∅)
62+
x = sincospi(1 .. 2)
63+
@test (x[1] interval(-1 , 0)) & (x[2] interval(-1 , 1))
64+
x = sincospi(0.5 .. 1.5)
65+
@test (x[1] interval(-1 , 1)) & (x[2] interval(-1 , 0))
66+
x = sincospi(0.25 .. 0.75)
67+
@test (x[1] interval(1/sqrt(2) , 1)) & (x[2] interval(-1/sqrt(2) , 1/sqrt(2)))
68+
x = sincospi(-0.25 .. 0.25)
69+
@test (x[1] interval(-1/sqrt(2) , 1/sqrt(2))) & (x[2] interval(1/sqrt(2) , 1))
70+
end
71+
5472
@testset "tan" begin
5573
@test tan(interval(0.5)) interval(0.54630248984379048, 0.5463024898437906)
5674
@test tan(interval(0.5, 1.67)) entireinterval()

0 commit comments

Comments
 (0)