Skip to content

Commit 45abc46

Browse files
committed
Implementation for strictly negative divisors for mod
1 parent ff47910 commit 45abc46

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

src/intervals/functions.jl

+7-2
Original file line numberDiff line numberDiff line change
@@ -378,8 +378,13 @@ end
378378
Calculate `x mod y` where `x` is an interval and `y` is a positive divisor.
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

test/interval_tests/numeric.jl

+12-2
Original file line numberDiff line numberDiff line change
@@ -446,20 +446,30 @@ 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
461-
462-
@test_throws AssertionError mod(x, -1)
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
463473

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

0 commit comments

Comments
 (0)