Skip to content

Commit e21de86

Browse files
dpsanderslbenet
authored andcommitted
Add rand(X) for Intervals and IntervalBoxes (#260)
* Add rand(X) for Intervals and IntervalBoxes * Add StaticArrays to test file * Correct tests
1 parent ecf16f5 commit e21de86

File tree

4 files changed

+32
-1
lines changed

4 files changed

+32
-1
lines changed

src/IntervalArithmetic.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import Base:
3333
expm1, log1p,
3434
precision,
3535
isfinite, isnan, isinf, iszero,
36-
abs, abs2,
36+
abs, abs2,
3737
show,
3838
isinteger, setdiff,
3939
parse, hash
@@ -112,6 +112,7 @@ include("multidim/multidim.jl")
112112
include("bisect.jl")
113113
include("decorations/decorations.jl")
114114

115+
include("rand.jl")
115116
include("parsing.jl")
116117
include("display.jl")
117118

src/rand.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Base.rand(X::Interval{T}) where {T} = X.lo + rand(T) * (X.hi - X.lo)
2+
3+
Base.rand(X::IntervalBox) = rand.(X)

test/rand.jl

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using IntervalArithmetic
2+
using Test
3+
using StaticArrays
4+
5+
@testset "rand tests" begin
6+
7+
X = 3..4
8+
for i in 1:100
9+
@test rand(X) X
10+
end
11+
12+
Y = IntervalBox(3..4, 5..6)
13+
for i in 1:100
14+
@test rand(Y) Y
15+
end
16+
17+
for T in (Float32, Float64, BigFloat)
18+
X = Interval{T}(3, 4)
19+
@test rand(X) isa T
20+
21+
Y = IntervalBox(X, X)
22+
@test rand(Y) isa SVector{2,T}
23+
end
24+
25+
end

test/runtests.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ setformat(:full)
1515
include("interval_tests/intervals.jl")
1616
include("decoration_tests/decoration_tests.jl")
1717

18+
include("rand.jl")
19+
1820
# Display tests:
1921
include("display_tests/display.jl")
2022

0 commit comments

Comments
 (0)