Skip to content

Commit fea1b34

Browse files
committed
Introduce simplified version of mod
1 parent f8a5b5e commit fea1b34

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

src/IntervalArithmetic.jl

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import Base:
2424
in, zero, one, eps, typemin, typemax, abs, abs2, real, min, max,
2525
sqrt, exp, log, sin, cos, tan, cot, inv, cbrt, csc, hypot, sec,
2626
exp2, exp10, log2, log10,
27+
mod,
2728
asin, acos, atan,
2829
sinh, cosh, tanh, coth, csch, sech, asinh, acosh, atanh, sinpi, cospi,
2930
union, intersect, isempty,

src/intervals/functions.jl

+10
Original file line numberDiff line numberDiff line change
@@ -373,3 +373,13 @@ function nthroot(a::Interval{T}, n::Integer) where T
373373
b = nthroot(bigequiv(a), n)
374374
return convert(Interval{T}, b)
375375
end
376+
377+
"""
378+
Calculate `x mod y` where `x` is an interval and `y` is a positive divisor.
379+
"""
380+
function mod(x::Interval, y::Real)
381+
@assert y > 0 "modulo is currently implemented only for a positive divisor."
382+
division = x / y
383+
fl = floor(division)
384+
fl.lo < fl.hi ? 0..y : y * (division - fl)
385+
end

test/interval_tests/numeric.jl

+24
Original file line numberDiff line numberDiff line change
@@ -434,3 +434,27 @@ end
434434
@test nthroot(Interval{BigFloat}(-81, -16), -4) ==
435435
@test nthroot(Interval{BigFloat}(-81, -16), 1) == Interval{BigFloat}(-81, -16)
436436
end
437+
438+
# approximation used in this testing (not to rely on ≈ for intervals)
439+
(x::Interval, y::Interval) = x.lo y.lo && x.hi y.hi
440+
441+
@testset "`mod`" begin
442+
r = 0.0625
443+
x = r..(1+r)
444+
@test mod(x, 1) == mod(x, 1.0) == 0..1
445+
@test mod(x, 2) == mod(x, 2.0) x
446+
@test mod(x, 2.5) x
447+
@test mod(x, 0.5) == 0..0.5
448+
449+
x = (-1+r) .. -r
450+
@test mod(x, 1) == mod(x, 1.0) 1+x
451+
@test mod(x, 2) == mod(x, 2.0) 2+x
452+
@test mod(x, 2.5) 2.5+x
453+
@test mod(x, 0.5) == 0..0.5
454+
455+
x = -r .. 1-r
456+
@test mod(x, 1) == mod(x, 1.0) == 0..1
457+
@test mod(x, 2) == mod(x, 2.0) == 0..2
458+
@test mod(x, 2.5) == 0..2.5
459+
@test mod(x, 0.5) == 0..0.5
460+
end

0 commit comments

Comments
 (0)