Skip to content

Commit 6fdc809

Browse files
committed
Merge branch 'simplified-mod' of github.com:petvana/IntervalArithmetic.jl into simplified-mod
2 parents 439723f + d2603d6 commit 6fdc809

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

src/intervals/functions.jl

+8-3
Original file line numberDiff line numberDiff line change
@@ -375,13 +375,18 @@ function nthroot(a::Interval{T}, n::Integer) where T
375375
end
376376

377377
"""
378-
Calculate `x mod y` where `x` is an interval and `y` is a positive divisor.
378+
Calculate `x::Interval mod y::Real`, limited by `y != 0`.
379379
"""
380380
function mod(x::Interval, y::Real)
381-
@assert y > zero(y) "modulo is currently implemented only for a positive divisor."
381+
@assert y != zero(y) """mod(x::Interval, y::Real)
382+
is currently implemented only for a strictly positive or negative divisor y."""
382383
division = x / y
383384
fl = floor(division)
384-
fl.lo < fl.hi ? Interval(zero(y), y) : y * (division - fl)
385+
if !isthin(fl)
386+
return y > zero(y) ? Interval(zero(y), y) : Interval(y, zero(y))
387+
else
388+
return y * (division - fl)
389+
end
385390
end
386391

387392
mod(x:T, y::Interval) where T = throw(ArgumentError("mod not defined for interval as divisor `y`"))

test/interval_tests/numeric.jl

+14-1
Original file line numberDiff line numberDiff line change
@@ -446,18 +446,31 @@ end
446446
@test mod(x, 2) == mod(x, 2.0) x
447447
@test mod(x, 2.5) x
448448
@test mod(x, 0.5) == 0..0.5
449+
@test mod(x, -1) == mod(x, -1.0) == -1..0
450+
@test mod(x, -2) == mod(x, -2.0) -2+x
451+
@test mod(x, -2.5) -2.5+x
452+
@test mod(x, -0.5) == -0.5..0
449453

450454
x = (-1+r) .. -r
451455
@test mod(x, 1) == mod(x, 1.0) 1+x
452456
@test mod(x, 2) == mod(x, 2.0) 2+x
453457
@test mod(x, 2.5) 2.5+x
454458
@test mod(x, 0.5) == 0..0.5
459+
@test mod(x, -1) == mod(x, -1.0) x
460+
@test mod(x, -2) == mod(x, -2.0) x
461+
@test mod(x, -2.5) x
462+
@test mod(x, -0.5) == -0.5..0
455463

456464
x = -r .. 1-r
457465
@test mod(x, 1) == mod(x, 1.0) == 0..1
458466
@test mod(x, 2) == mod(x, 2.0) == 0..2
459467
@test mod(x, 2.5) == 0..2.5
460468
@test mod(x, 0.5) == 0..0.5
469+
@test mod(x, -1) == mod(x, -1.0) == -1..0
470+
@test mod(x, -2) == mod(x, -2.0) == -2..0
471+
@test mod(x, -2.5) == -2.5..0
472+
@test mod(x, -0.5) == -0.5..0
461473

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

0 commit comments

Comments
 (0)