From ff4791034526c3104889ee7105b7bcfb3809cd93 Mon Sep 17 00:00:00 2001 From: Petr Vana Date: Thu, 26 May 2022 11:11:35 +0200 Subject: [PATCH 1/3] Add todo for mod with between two intervals --- test/interval_tests/numeric.jl | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test/interval_tests/numeric.jl b/test/interval_tests/numeric.jl index 3d88a91a0..d44a44122 100644 --- a/test/interval_tests/numeric.jl +++ b/test/interval_tests/numeric.jl @@ -460,4 +460,7 @@ end @test mod(x, 0.5) == 0..0.5 @test_throws AssertionError mod(x, -1) + + # TODO - implement mod for two intervals + @test_throws TypeError mod(1..2, 1.4..1.5) end From 45abc460ab885ba1648c3b83fb74a96053c169f9 Mon Sep 17 00:00:00 2001 From: Petr Vana Date: Thu, 26 May 2022 11:30:14 +0200 Subject: [PATCH 2/3] 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) From d2603d6c4524ab884aeaabaf2265f8d123b13d28 Mon Sep 17 00:00:00 2001 From: Petr Vana Date: Thu, 26 May 2022 11:34:04 +0200 Subject: [PATCH 3/3] Update docs --- src/intervals/functions.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/intervals/functions.jl b/src/intervals/functions.jl index 5fb2f03ef..11b82571e 100644 --- a/src/intervals/functions.jl +++ b/src/intervals/functions.jl @@ -375,7 +375,7 @@ 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) """mod(x::Interval, y::Real)