Skip to content

Commit

Permalink
Add extended_mod(a, y), with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Luis Benet committed May 4, 2018
1 parent 583f97d commit f886a50
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/IntervalArithmetic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export
RoundTiesToEven, RoundTiesToAway,
cancelminus, cancelplus, isunbounded,
.., @I_str, ±,
pow, extended_div,
pow, extended_div, extended_mod,
setformat, @format

export
Expand Down
30 changes: 30 additions & 0 deletions src/intervals/functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -286,3 +286,33 @@ function mod(a::Interval, y::T) where {T<:Real}
return interval(mod(a.lo, y), mod(a.hi, y))
end
end


function extended_mod(a::Interval, y::T) where {T<:Real}
yy = abs(y)
fld_lo = fld(a.lo, yy)
fld_hi = fld(a.hi, yy)
z = zero(fld_lo)
S = typeof( z )
ee = emptyinterval(S)

if fld_lo + 1 == fld_hi
# `a` includes one discontinuity
if y > 0
return interval(mod(a.lo, y), y), interval(z, mod(a.hi, y)), ee
else
return interval(mod(a.lo, y), z), interval(y, mod(a.hi, y)), ee
end
elseif fld_lo +1 < fld_hi
# `a` includes more discontinuities
if y > 0
return interval(mod(a.lo, y), y), interval(z, y), interval(z, mod(a.hi, y))
else
return interval(mod(a.lo, y), z), interval(y, z), interval(y, mod(a.hi, y))
end
else
# no discontinuity crossed within `a`
return interval(mod(a.lo, y), mod(a.hi, y)), ee, ee
end

end
56 changes: 48 additions & 8 deletions test/interval_tests/numeric.jl
Original file line number Diff line number Diff line change
Expand Up @@ -282,26 +282,66 @@ end
@testset "`mod`" begin
r = 0.0625
a = r..(1+r)
@test mod(a, 1) == mod(a, 1.0) == interval(0,1)
@test mod(a, 1) == mod(a, 1.0) == 0..1
@test mod(a, 2) == mod(a, 2.0) == a
@test mod(a, 2.5) == a
@test mod(a, -1) == mod(a, -1.0) == interval(-1,0)
@test mod(a, 0.5) == 0..0.5
@test mod(a, -1) == mod(a, -1.0) == -1..0
@test mod(a, -2) == mod(a, -2.0) == -2+a
@test mod(a, -2.5) == -2.5+a
@test mod(a, -0.5) == -0.5..0

a = (-1+r) .. -r
@test mod(a, 1) == mod(a, 1.0) == 1+a
@test mod(a, 2) == mod(a, 2.0) == 2+a
@test mod(a, 2.5) == 2.5+a
@test mod(a, 0.5) == 0..0.5
@test mod(a, -1) == mod(a, -1.0) == a
@test mod(a, -2) == mod(a, -2.0) == a
@test mod(a, -2.5) == a
@test mod(a, -0.5) == -0.5..0

a = -r .. 1-r
@test mod(a, 1) == mod(a, 1.0) == interval(0,1)
@test mod(a, 2) == mod(a, 2.0) == interval(0,2)
@test mod(a, 2.5) == interval(0,2.5)
@test mod(a, -1) == mod(a, -1.0) == interval(-1,0)
@test mod(a, -2) == mod(a, -2.0) == interval(-2,0)
@test mod(a, -2.5) == interval(-2.5,0)
@test mod(a, 1) == mod(a, 1.0) == 0..1
@test mod(a, 2) == mod(a, 2.0) == 0..2
@test mod(a, 2.5) == 0..2.5
@test mod(a, 0.5) == 0..0.5
@test mod(a, -1) == mod(a, -1.0) == -1..0
@test mod(a, -2) == mod(a, -2.0) == -2..0
@test mod(a, -2.5) == -2.5..0
@test mod(a, -0.5) == -0.5..0
end

@testset "`extended_mod`" begin
r = 0.0625
a = r..(1+r)
ee = emptyinterval(Float64)
@test extended_mod(a, 1) == (r..1, 0..r, ee)
@test extended_mod(a, 2) == (a, ee, ee)
@test extended_mod(a, 2.5) == (a, ee, ee)
@test extended_mod(a, 0.5) == (r..0.5, 0..0.5, 0..r)
@test extended_mod(a, -1) == ((-1+r)..0, -1..(-1+r), ee)
@test extended_mod(a, -2) == ((-2+r)..(-1+r), ee, ee)
@test extended_mod(a, -2.5) == ((-2.5+r)..(-1.5+r), ee, ee)
@test extended_mod(a, -0.5) == ((-0.5+r)..0, -0.5..0, -0.5..(-0.5+r))

a = (-1+r) .. -r
@test extended_mod(a, 1) == (1+a, ee, ee)
@test extended_mod(a, 2) == (2+a, ee, ee)
@test extended_mod(a, 2.5) == (2.5+a, ee, ee)
@test extended_mod(a, 0.5) == (r..0.5, 0..(0.5-r), ee)
@test extended_mod(a, -1) == ((-1+r) .. -r, ee, ee)
@test extended_mod(a, -2) == ((-1+r) .. -r, ee, ee)
@test extended_mod(a, -2.5) == ((-1+r) .. -r, ee, ee)
@test extended_mod(a, -0.5) == ((-0.5+r)..0, -0.5 .. -r, ee)

a = -r .. 1-r
@test extended_mod(a, 1) == ((1-r)..1, 0..(1-r), ee)
@test extended_mod(a, 2) == ((2-r)..2, 0..(1-r), ee)
@test extended_mod(a, 2.5) == ((2.5-r)..2.5, 0..(1-r), ee)
@test extended_mod(a, 0.5) == ((0.5-r)..0.5, 0..0.5, 0..(0.5-r))
@test extended_mod(a, -1) == (-r..0, -1..(-r), ee)
@test extended_mod(a, -2) == (-r..0, -2..(-1-r), ee)
@test extended_mod(a, -2.5) == (-r..0, (-2.5)..(-1.5-r), ee)
@test extended_mod(a, -0.5) == (-r..0, -0.5..0, -0.5..(-r))
end

0 comments on commit f886a50

Please sign in to comment.