Skip to content

Commit af3618f

Browse files
committed
add tests and fix bugs in modrem
1 parent 674be88 commit af3618f

File tree

2 files changed

+21
-22
lines changed

2 files changed

+21
-22
lines changed

src/math/arithmetic/modpi.jl

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
## This work was supported in part by the U.S. Department of Energy, Office of Science, Office of Workforce Development for Teachers and Scientists (WDTS) under the Science Undergraduate Laboratory Internships Program (SULI) program at Oak Ridge National Laboratory, administered by the Oak Ridge Institute for Science and Education.
33

44
function mod2pi(x::DoubleFloat{T}) where {T<:IEEEFloat}
5-
s = signbit(x)
5+
s = x < 0
66
c = s ? -1 : 1
77
y = c * x
88
y < pi2o1(DoubleFloat{T}) && return s ? pi2o1(DoubleFloat{T}) - y : y
@@ -96,26 +96,6 @@ end
9696

9797
# ========================================== ^^^^^ =========================================
9898

99-
const pi_1o1_t64_hi = pi_1o1_t64[1]
100-
const pi_1o1_t64_md = pi_1o1_t64[2]
101-
const pi_1o1_t64_lo = pi_1o1_t64[3]
102-
103-
const pi_1o4_t64_hi = pi_1o4_t64[1]
104-
const pi_1o4_t64_md = pi_1o4_t64[2]
105-
const pi_1o4_t64_lo = pi_1o4_t64[3]
106-
107-
function mod1pipm(x::DoubleFloat{Float64})
108-
abs(x) < onepi_d64 && return x
109-
w1 = mul323(inv_pi_1o1_t64, HILO(x))
110-
w2 = two_sum(w1[1] - trunc(w1[1]), w1[2], w1[3])
111-
y = mul322(pi_1o1_t64, w2)
112-
z = Double64(y)
113-
return z
114-
end
115-
116-
mod1pipm(x::DoubleFloat{Float32}) = DoubleFloat{Float32}(mod1pipm(DoubleFloat{Float64}(x)))
117-
mod1pipm(x::DoubleFloat{Float16}) = DoubleFloat{Float16}(mod1pipm(DoubleFloat{Float64}(x)))
118-
11999
#=
120100
rem2pi(x) = x - 2π*round(x/(2π),r)
121101
rem2pi(x, RoundDown) == mod2pi(x)
@@ -127,9 +107,17 @@ mod1pipm(x::DoubleFloat{Float16}) = DoubleFloat{Float16}(mod1pipm(DoubleFloat{Fl
127107
• if r == RoundUp, then the result is in the interval [-2π, 0].
128108
=#
129109

110+
function rem2pi(x::DoubleFloat{Float64}, rounding::RoundingMode{:Nearest})
111+
abs(x) < onepi && return x
112+
w1 = mul323(inv_pi_2o1_t64, HILO(x))
113+
w2 = two_sum(w1[1] - round(w1[1]), w1[2], w1[3])
114+
y = mul322(pi_2o1_t64, w2)
115+
z = Double64(y)
116+
return z
117+
end
118+
130119
rem2pi(x::DoubleFloat{Float64}, rounding::RoundingMode{:Down}) = mod2pi(x)
131120
rem2pi(x::DoubleFloat{Float64}, rounding::RoundingMode{:Up}) = -rem2pi(-x, RoundDown)
132-
rem2pi(x::DoubleFloat{Float64}, rounding::RoundingMode{:Nearest}) = mod1pipm(x)
133121
rem2pi(x::DoubleFloat{Float64}, rounding::RoundingMode{:ToZero}) =
134122
signbit(x) ? rem2pi(x, RoundUp) : rem2pi(x, RoundDown)
135123

test/modrem.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
@testset "modpi" begin
2+
3+
@test mod2pi(Double64(-13)) mod2pi(big(-13.)) atol = 1e-31
4+
25
x=cbrt(41)*sqrt(Double64(pi))
36

47
@test DoubleFloats.mod2pi(x) == Double64(6.111805926475162, -1.667563077572613e-16)
@@ -13,4 +16,12 @@ end
1316

1417
@test DoubleFloats.rem2pi(x) == Double64(6.111805926475162, -1.667563077572613e-16)
1518
@test DoubleFloats.rem2pi(-x) == Double64(-0.1713793807044248, 4.647966647701768e-18)
19+
20+
v = Double64.(-7:7)
21+
bigv = big.(v)
22+
for rounding in (RoundUp, RoundDown, RoundNearest, RoundToZero)
23+
drem = rem2pi.(v, Ref(rounding))
24+
bigrem = rem2pi.(bigv, Ref(rounding))
25+
@test all(abs.(bigrem-drem) .< 1e-31)
26+
end
1627
end

0 commit comments

Comments
 (0)