Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Arithmetic operations with integers and intervals
Browse files Browse the repository at this point in the history
lbenet committed Dec 6, 2023
1 parent 742a26f commit 68ba393
Showing 2 changed files with 22 additions and 12 deletions.
10 changes: 10 additions & 0 deletions src/intervals/arithmetic/basic.jl
Original file line number Diff line number Diff line change
@@ -193,6 +193,16 @@ Implement the rational division; this is semantically equivalent to `a / b`.

//(a::Interval, b::Interval) = /(a, b)

# Arithmetic operations with intervals yield guaranteed results
for (T, fc) in ((:BareInterval, :(bareinterval)), (:Interval, :(interval)))
for op in (:+, :-, :*, :/, ://)
@eval begin
$(op)(n::Integer, x::$T) = $(op)($fc(n), x)
$(op)(x::$T, n::Integer) = $(op)(x, $fc(n))
end
end
end

"""
muladd(a::BareInterval, b::BareInterval c::BareInterval)
muladd(a::Interval, b::Interval c::Interval)
24 changes: 12 additions & 12 deletions test/interval_tests/display.jl
Original file line number Diff line number Diff line change
@@ -54,7 +54,7 @@ setprecision(BigFloat, 256) do

@testset "Interval" begin
a = interval(1, 2)
a_NG = a/1
a_G = a/1
b = interval(-floatmin(Float64), 1.3)
b32 = interval(-floatmin(Float32), parse(Float32, "1.3"))
b16 = interval(-floatmin(Float16), parse(Float16, "1.3"))
@@ -73,10 +73,10 @@ setprecision(BigFloat, 256) do
setdisplay(; decorations = true)

@test sprint(show, MIME("text/plain"), emptyinterval()) == "∅_trv"
@test sprint(show, MIME("text/plain"), emptyinterval()/1) == "_trv_NG"
@test sprint(show, MIME("text/plain"), emptyinterval()/1) == "_trv"

@test sprint(show, MIME("text/plain"), a) == "[1.0, 2.0]_com"
@test sprint(show, MIME("text/plain"), a_NG) == "[1.0, 2.0]_com_NG"
@test sprint(show, MIME("text/plain"), a_G) == "[1.0, 2.0]_com"
@test sprint(show, MIME("text/plain"), b) == "[-2.22508e-308, 1.30001]_com"
@test sprint(show, MIME("text/plain"), b32) == "[-1.1755f-38, 1.30001f0]_com"
@test sprint(show, MIME("text/plain"), b16) == "[Float16(-6.104e-5), Float16(1.29981)]_com"
@@ -90,10 +90,10 @@ setprecision(BigFloat, 256) do
setdisplay(; decorations = false)

@test sprint(show, MIME("text/plain"), emptyinterval()) == ""
@test sprint(show, MIME("text/plain"), emptyinterval()/1) == "_NG"
@test sprint(show, MIME("text/plain"), emptyinterval()/1) == ""

@test sprint(show, MIME("text/plain"), a) == "[1.0, 2.0]"
@test sprint(show, MIME("text/plain"), a_NG) == "[1.0, 2.0]_NG"
@test sprint(show, MIME("text/plain"), a_G) == "[1.0, 2.0]"
@test sprint(show, MIME("text/plain"), b) == "[-2.22508e-308, 1.30001]"
@test sprint(show, MIME("text/plain"), b32) == "[-1.1755f-38, 1.30001f0]"
@test sprint(show, MIME("text/plain"), b16) == "[Float16(-6.104e-5), Float16(1.29981)]"
@@ -108,7 +108,7 @@ setprecision(BigFloat, 256) do
setdisplay(; sigdigits = 20, decorations = true)

@test sprint(show, MIME("text/plain"), a) == "[1.0, 2.0]_com"
@test sprint(show, MIME("text/plain"), a_NG) == "[1.0, 2.0]_com_NG"
@test sprint(show, MIME("text/plain"), a_G) == "[1.0, 2.0]_com"
@test sprint(show, MIME("text/plain"), b) == "[-2.2250738585072014e-308, 1.3000000000000000445]_com"
@test sprint(show, MIME("text/plain"), b32) == "[-1.1754944f-38, 1.2999999523162841797f0]_com"
@test sprint(show, MIME("text/plain"), b16) == "[Float16(-6.104e-5), Float16(1.2998046875000000001)]_com"
@@ -124,10 +124,10 @@ setprecision(BigFloat, 256) do
setdisplay(:full; sigdigits = 100, decorations = false)

@test sprint(show, MIME("text/plain"), emptyinterval()) == "Interval{Float64}(∅, trv)"
@test sprint(show, MIME("text/plain"), emptyinterval()/1) == "Interval{Float64}(∅, trv, NG)"
@test sprint(show, MIME("text/plain"), emptyinterval()/1) == "Interval{Float64}(∅, trv)"

@test sprint(show, MIME("text/plain"), a) == "Interval{Float64}(1.0, 2.0, com)"
@test sprint(show, MIME("text/plain"), a_NG) == "Interval{Float64}(1.0, 2.0, com, NG)"
@test sprint(show, MIME("text/plain"), a_G) == "Interval{Float64}(1.0, 2.0, com)"
@test sprint(show, MIME("text/plain"), b) == "Interval{Float64}(-2.2250738585072014e-308, 1.3, com)"
@test sprint(show, MIME("text/plain"), b32) == "Interval{Float32}(-1.1754944f-38, 1.3f0, com)"
@test sprint(show, MIME("text/plain"), b16) == "Interval{Float16}(Float16(-6.104e-5), Float16(1.3), com)"
@@ -144,10 +144,10 @@ setprecision(BigFloat, 256) do
setdisplay(; decorations = true)

@test sprint(show, MIME("text/plain"), emptyinterval()) == "∅_trv"
@test sprint(show, MIME("text/plain"), emptyinterval()/1) == "_trv_NG"
@test sprint(show, MIME("text/plain"), emptyinterval()/1) == "_trv"

@test sprint(show, MIME("text/plain"), a) == "(1.5 ± 0.5)_com"
@test sprint(show, MIME("text/plain"), a_NG) == "(1.5 ± 0.5)_com_NG"
@test sprint(show, MIME("text/plain"), a_G) == "(1.5 ± 0.5)_com"
@test sprint(show, MIME("text/plain"), b) == "(0.65 ± 0.650001)_com"
@test sprint(show, MIME("text/plain"), b32) == "(0.65f0 ± 0.650001f0)_com"
@test sprint(show, MIME("text/plain"), b16) == "(Float16(0.649902) ± Float16(0.649903))_com"
@@ -161,10 +161,10 @@ setprecision(BigFloat, 256) do
setdisplay(; decorations = false)

@test sprint(show, MIME("text/plain"), emptyinterval()) == ""
@test sprint(show, MIME("text/plain"), emptyinterval()/1) == "_NG"
@test sprint(show, MIME("text/plain"), emptyinterval()/1) == ""

@test sprint(show, MIME("text/plain"), a) == "1.5 ± 0.5"
@test sprint(show, MIME("text/plain"), a_NG) == "(1.5 ± 0.5)_NG"
@test sprint(show, MIME("text/plain"), a_G) == "1.5 ± 0.5"
@test sprint(show, MIME("text/plain"), b) == "0.65 ± 0.650001"
@test sprint(show, MIME("text/plain"), b32) == "0.65f0 ± 0.650001f0"
@test sprint(show, MIME("text/plain"), b16) == "Float16(0.649902) ± Float16(0.649903)"

0 comments on commit 68ba393

Please sign in to comment.