Skip to content

Commit

Permalink
Merge branch 'simplified-mod' of github.com:petvana/IntervalArithmeti…
Browse files Browse the repository at this point in the history
…c.jl into simplified-mod
  • Loading branch information
petvana committed May 27, 2022
2 parents 439723f + d2603d6 commit 6fdc809
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
11 changes: 8 additions & 3 deletions src/intervals/functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -375,13 +375,18 @@ function nthroot(a::Interval{T}, n::Integer) where T
end

"""
Calculate `x mod y` where `x` is an interval and `y` is a positive divisor.
Calculate `x::Interval mod y::Real`, limited by `y != 0`.
"""
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

mod(x:T, y::Interval) where T = throw(ArgumentError("mod not defined for interval as divisor `y`"))
15 changes: 14 additions & 1 deletion test/interval_tests/numeric.jl
Original file line number Diff line number Diff line change
Expand Up @@ -446,18 +446,31 @@ 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 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

@test_throws AssertionError mod(x, -1)
# TODO - implement mod for two intervals
@test_throws TypeError mod(1..2, 1.4..1.5)
end

0 comments on commit 6fdc809

Please sign in to comment.