Skip to content

Commit 0e45219

Browse files
Merge pull request #122 from klinemichael/modpi_fix
Fixed mod2pi, mod1pi, modhalfpi, and modqrtrpi
2 parents a6d234a + 012a638 commit 0e45219

File tree

5 files changed

+27
-28
lines changed

5 files changed

+27
-28
lines changed

.DS_Store

6 KB
Binary file not shown.

src/.DS_Store

6 KB
Binary file not shown.

src/math/.DS_Store

6 KB
Binary file not shown.

src/math/arithmetic/modpi.jl

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1+
## Edited by Michael Kline, MIT License
2+
## 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.
3+
14
function mod2pi(x::DoubleFloat{T}) where {T<:IEEEFloat}
25
s = signbit(x)
3-
if s
4-
x = -x
5-
end
6-
x < pi2o1(DoubleFloat{T}) && return x
7-
x < pi4o1(DoubleFloat{T}) && return mod2pi(x - pi2o1(DoubleFloat{T}))
8-
himdlo = mul323(inv_pi_2o1_t64, HILO(x))
6+
c = s ? -1 : 1
7+
y = c * x
8+
y < pi2o1(DoubleFloat{T}) && return s ? pi2o1(DoubleFloat{T}) - y : y
9+
y < pi4o1(DoubleFloat{T}) && return mod2pi(x - c*pi2o1(DoubleFloat{T}))
10+
himdlo = mul323(inv_pi_2o1_t64, HILO(y))
911
hi, md, lo = three_sum([modf(x)[1] for x in himdlo]...,)
1012
if hi >= 1.0
1113
hi = hi - 1.0
@@ -23,12 +25,11 @@ end
2325

2426
function mod1pi(x::DoubleFloat{T}) where {T<:IEEEFloat}
2527
s = signbit(x)
26-
if s
27-
x = -x
28-
end
29-
x < pi1o1(DoubleFloat{T}) && return x
30-
x < pi2o1(DoubleFloat{T}) && return mod1pi(x - pi1o1(DoubleFloat{T}))
31-
himdlo = mul323(inv_pi_1o1_t64, HILO(x))
28+
c = s ? -1 : 1
29+
y = c * x
30+
x < pi1o1(DoubleFloat{T}) && return s ? pi1o1(DoubleFloat{T}) - y : y
31+
x < pi2o1(DoubleFloat{T}) && return mod1pi(x - c*pi1o1(DoubleFloat{T}))
32+
himdlo = mul323(inv_pi_1o1_t64, HILO(y))
3233
hi, md, lo = three_sum([modf(x)[1] for x in himdlo]...,)
3334
if hi >= 1.0
3435
hi = hi - 1.0
@@ -46,12 +47,11 @@ end
4647

4748
function modhalfpi(x::DoubleFloat{T}) where {T<:IEEEFloat}
4849
s = signbit(x)
49-
if s
50-
x = -x
51-
end
52-
x < pi1o2(DoubleFloat{T}) && return x
53-
x < pi1o1(DoubleFloat{T}) && return modhalfpi(x - pi1o2(DoubleFloat{T}))
54-
himdlo = mul323(inv_pi_1o2_t64, HILO(x))
50+
c = s ? -1 : 1
51+
y = c * x
52+
x < pi1o2(DoubleFloat{T}) && return s ? pi1o2(DoubleFloat{T}) - y : y
53+
x < pi1o1(DoubleFloat{T}) && return modhalfpi(x - c*pi1o2(DoubleFloat{T}))
54+
himdlo = mul323(inv_pi_1o2_t64, HILO(y))
5555
hi, md, lo = three_sum([modf(x)[1] for x in himdlo]...,)
5656
if hi >= 1.0
5757
hi = hi - 1.0
@@ -69,12 +69,11 @@ end
6969

7070
function modqrtrpi(x::DoubleFloat{T}) where {T<:IEEEFloat}
7171
s = signbit(x)
72-
if s
73-
x = -x
74-
end
75-
x < pi1o4(DoubleFloat{T}) && return x
76-
x < pi1o2(DoubleFloat{T}) && return modqrtrpi(x - pi1o4(DoubleFloat{T}))
77-
himdlo = mul323(inv_pi_1o4_t64, HILO(x))
72+
c = s ? -1 : 1
73+
y = c * x
74+
x < pi1o4(DoubleFloat{T}) && return s ? pi1o4(DoubleFloat{T}) - y : y
75+
x < pi1o2(DoubleFloat{T}) && return modqrtrpi(x - c*pi1o4(DoubleFloat{T}))
76+
himdlo = mul323(inv_pi_1o4_t64, HILO(y))
7877
hi, md, lo = three_sum([modf(x)[1] for x in himdlo]...,)
7978
if hi >= 1.0
8079
hi = hi - 1.0
@@ -132,7 +131,7 @@ end
132131
# within (pi/2) quadrant determine nearest multiple of pi/16
133132

134133
function whichquadrant(x::DoubleFloat{T}) where {T<:IEEEFloat}
135-
x = negpi_pospi(x)
134+
x = negpi_pospi(x)
136135
if signbit(x) # quadrant -2 or -1
137136
quadrant = -x < DoubleFloat{T}(pi)/2 ? -2 : -1
138137
else # quadrant 1 or 2

test/modrem.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
@test DoubleFloats.mod2pi(x) == Double64(6.111805926475162, -1.667563077572613e-16)
55
@test DoubleFloats.mod1pi(x) == Double64(2.9702132728853683, 1.54868222178066e-16)
6-
# these tests are unusable, sometimes they pass and othertimes they fail
6+
# these tests are unusable, sometimes they pass and othertimes they fail
77
# @test DoubleFloats.modhalfpi(x) == Double64(1.3994169460904717, 9.362948122802731e-17)
88
# @test DoubleFloats.modqrtrpi(x) == Double64(0.6140187826930236, -4.8008991213172176e-17)
99
end
@@ -13,6 +13,6 @@ end
1313

1414
@test DoubleFloats.rem2pi(x) == Double64(6.111805926475162, -1.667563077572613e-16)
1515
@test DoubleFloats.rem1pi(x) == Double64(2.9702132728853683, 1.54868222178066e-16)
16-
@test DoubleFloats.rem2pi(-x) == Double64(-6.111805926475162, 1.667563077572613e-16)
17-
@test DoubleFloats.rem1pi(-x) == Double64(-2.9702132728853683, -1.54868222178066e-16)
16+
@test DoubleFloats.rem2pi(-x) == Double64(-0.1713793807044248, 4.647966647701768e-18)
17+
@test DoubleFloats.rem1pi(-x) == Double64(2.9702132728853683, 1.54868222178066e-16)
1818
end

0 commit comments

Comments
 (0)