Skip to content

Commit

Permalink
Implementation for strictly negative divisors for mod
Browse files Browse the repository at this point in the history
  • Loading branch information
petvana committed May 26, 2022
1 parent ff47910 commit 45abc46
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
9 changes: 7 additions & 2 deletions src/intervals/functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -378,8 +378,13 @@ end
Calculate `x mod y` where `x` is an interval and `y` is a positive divisor.
"""
function mod(x::Interval, y::Real)
@assert y > zero(y) "modulo is currently implemented only for a positive divisor."
@assert y != zero(y) """mod(x::Interval, y::Real)
is currently implemented only for a strictly positive or negative divisor y."""
division = x / y
fl = floor(division)
fl.lo < fl.hi ? Interval(zero(y), y) : y * (division - fl)
if !isthin(fl)
return y > zero(y) ? Interval(zero(y), y) : Interval(y, zero(y))
else
return y * (division - fl)
end
end
14 changes: 12 additions & 2 deletions test/interval_tests/numeric.jl
Original file line number Diff line number Diff line change
Expand Up @@ -446,20 +446,30 @@ end
@test mod(x, 2) == mod(x, 2.0) x
@test mod(x, 2.5) x
@test mod(x, 0.5) == 0..0.5
@test mod(x, -1) == mod(x, -1.0) == -1..0
@test mod(x, -2) == mod(x, -2.0) -2+x
@test mod(x, -2.5) -2.5+x
@test mod(x, -0.5) == -0.5..0

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

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

@test_throws AssertionError mod(x, -1)
@test mod(x, -1) == mod(x, -1.0) == -1..0
@test mod(x, -2) == mod(x, -2.0) == -2..0
@test mod(x, -2.5) == -2.5..0
@test mod(x, -0.5) == -0.5..0

# TODO - implement mod for two intervals
@test_throws TypeError mod(1..2, 1.4..1.5)
Expand Down

0 comments on commit 45abc46

Please sign in to comment.