Skip to content

Commit 889dbc5

Browse files
Fixed Box construction
Promotion should happen regardless of conversion to StaticArray.
1 parent b8e8dc7 commit 889dbc5

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

src/array.jl

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,22 @@ end
2222

2323
function Box(lower, upper; convert_to_static::Bool=false)
2424
@assert size(lower) == size(upper)
25-
sz = size(lower)
26-
continuous_lower = convert(AbstractArray{float(eltype(lower))}, lower)
27-
continuous_upper = convert(AbstractArray{float(eltype(upper))}, upper)
25+
T = promote_type(eltype(lower), eltype(upper)) |> float
26+
continuous_lower = convert(AbstractArray{T}, lower)
27+
continuous_upper = convert(AbstractArray{T}, upper)
2828
if convert_to_static
29-
final_lower = SArray{Tuple{sz...}}(continuous_lower)
30-
final_upper = SArray{Tuple{sz...}}(continuous_upper)
29+
final_lower = SArray{Tuple{size(continuous_lower)...}}(continuous_lower)
30+
final_upper = SArray{Tuple{size(continuous_upper)...}}(continuous_upper)
3131
else
32-
final_lower, final_upper = promote(continuous_lower, continuous_upper)
32+
final_lower, final_upper = continuous_lower, continuous_upper
3333
end
3434
return Box{typeof(final_lower)}(final_lower, final_upper)
3535
end
3636

37+
function Base.:(==)(b1::T, b2::T) where {T <: Box}
38+
return (b1.lower == b2.lower) && (b1.upper == b2.upper)
39+
end
40+
3741
# By default, convert builtin arrays to static
3842
Box(lower::Array, upper::Array) = Box(lower, upper; convert_to_static=true)
3943

test/array.jl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,26 @@
5858
@test Box([1,2], [3,4]) != Box([1,3], [3,4])
5959
end
6060

61+
@testset "Box type check" begin
62+
T = [
63+
BigFloat, Float64, Float32, Float16,
64+
BigInt, Int128, Int64, Int32, Int16, Int8,
65+
UInt128, UInt64, UInt16, UInt32, UInt8
66+
]
67+
for T1 in T, T2 in T
68+
x, y = [1,2], [3,4]
69+
box = Box(T1.(x), T2.(y))
70+
T_goal = float(promote_type(T1, T2))
71+
box_goal = Box{SVector{2, T_goal}}(
72+
SVector{2,T_goal}(T_goal.(x)),
73+
SVector{2,T_goal}(T_goal.(y))
74+
)
75+
@testset "$T1, $T2" begin
76+
@test box == box_goal
77+
end
78+
end
79+
end
80+
6181
@testset "Interval to box conversion" begin
6282
@test convert(Box, 1..2) == Box([1], [2])
6383
end

0 commit comments

Comments
 (0)