Skip to content

Commit b7163bd

Browse files
eeshan9815dpsanders
authored andcommitted
Add hashing for Intervals (#161)
* Add hashing for Intervals * Add more tests * Add docstring
1 parent 37f126e commit b7163bd

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

src/IntervalArithmetic.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import Base:
2727
isfinite, isnan, isinf, iszero,
2828
show, showall,
2929
isinteger, setdiff,
30-
parse
30+
parse, hash
3131

3232
import Base: # for IntervalBox
3333
broadcast, dot, length,

src/intervals/intervals.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,3 +156,8 @@ end
156156

157157
a ± b = (a-b)..(a+b)
158158
±(a::Interval, b) = (a.lo - b)..(a.hi + b)
159+
160+
"""
161+
Computes the integer hash code for an `Interval` using the method for composite types used in `AutoHashEquals.jl`
162+
"""
163+
hash(x::Interval, h::UInt) = hash(x.hi, hash(x.lo, hash(:Interval, h)))

test/interval_tests/construction.jl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,3 +311,21 @@ end
311311
@testset "a..b with a > b" begin
312312
@test_throws ArgumentError 3..2
313313
end
314+
315+
@testset "Hashing of Intervals" begin
316+
x = Interval{Float64}(1, 2)
317+
y = Interval{BigFloat}(1, 2)
318+
@test isequal(x, y)
319+
@test isequal(hash(x), hash(y))
320+
321+
x = @interval(0.1)
322+
y = IntervalArithmetic.big53(x)
323+
@test isequal(x, y)
324+
@test isequal(hash(x), hash(y))
325+
326+
x = interval(1, 2)
327+
y = interval(1, 3)
328+
@test !isequal(x, y)
329+
@test !isequal(hash(x), hash(y))
330+
331+
end

0 commit comments

Comments
 (0)