From 45abc460ab885ba1648c3b83fb74a96053c169f9 Mon Sep 17 00:00:00 2001 From: Petr Vana Date: Thu, 26 May 2022 11:30:14 +0200 Subject: [PATCH] Implementation for strictly negative divisors for mod --- src/intervals/functions.jl | 9 +++++++-- test/interval_tests/numeric.jl | 14 ++++++++++++-- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/intervals/functions.jl b/src/intervals/functions.jl index cc9d6f2b1..5fb2f03ef 100644 --- a/src/intervals/functions.jl +++ b/src/intervals/functions.jl @@ -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 diff --git a/test/interval_tests/numeric.jl b/test/interval_tests/numeric.jl index d44a44122..d933f9c7c 100644 --- a/test/interval_tests/numeric.jl +++ b/test/interval_tests/numeric.jl @@ -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)