Skip to content

Fix numeric promotion rules #84

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,29 @@ end
function Base.promote_rule(::Type{<:Quantity{T1,D1}}, ::Type{<:Quantity{T2,D2}}) where {T1,T2,D1,D2}
return Quantity{promote_type(T1,T2),promote_type(D1,D2)}
end

# Define promotion rules for all basic numeric types, individually.
# We don't want to define an opinionated promotion on <:Number,
# or even <:AbstractFloat, as it could conflict with other
# abstract number packages which may try to do the same thing.
# (which would lead to ambiguities)
const BASE_NUMERIC_TYPES = Union{
Bool, Int8, UInt8, Int16, UInt16, Int32, UInt32,
Int64, UInt64, Int128, UInt128, Float16, Float32,
Float64, BigFloat, BigInt, ComplexF16, ComplexF32,
ComplexF64, Complex{BigFloat}, Rational{Int8}, Rational{UInt8},
Rational{Int16}, Rational{UInt16}, Rational{Int32}, Rational{UInt32},
Rational{Int64}, Rational{UInt64}, Rational{Int128}, Rational{UInt128},
Rational{BigInt},
}
for (type, _, _) in ABSTRACT_QUANTITY_TYPES
@eval function Base.promote_rule(::Type{Q}, ::Type{T2}) where {T,D,Q<:$type{T,D},T2<:BASE_NUMERIC_TYPES}
return with_type_parameters(Q, promote_type(T, T2), D)
end
@eval function Base.convert(::Type{Q}, x::BASE_NUMERIC_TYPES) where {T,D,Q<:$type{T,D}}
return new_quantity(Q, convert(T, x), D())
end
end
function Base.promote_rule(::Type{<:AbstractQuantity}, ::Type{<:Number})
return Number
end
Expand Down
27 changes: 21 additions & 6 deletions test/unittests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -452,15 +452,30 @@ end
q2 = Quantity(2, mass=1)
@test typeof(promote(q1, q2)) == typeof((q1, q1))

x = [0.5, 0.5u"km/s"]
@test x isa Vector{Number}

x = [0.5, GenericQuantity(0.5u"km/s")]
@test x isa Vector{Any}
q = 0.5u"km/s"
x = [0.5, q]
@test x isa Vector{typeof(q)}
@test x[1] == convert(typeof(q), 0.5)

q = GenericQuantity(0.5u"km/s")
x = [0.5, q]
@test x isa Vector{typeof(q)}

# Promotion with custom numeric type:
@eval struct MyNumber <: Real
x::Float64
end
a = 0.5u"km/s"
b = MyNumber(0.5)
ar = [a, b]
@test ar isa Vector{Number}
@test a === ar[1]
@test b === ar[2]
@test promote_type(MyNumber, typeof(a)) == Number

# Explicit conversion so coverage can see it:
D = DEFAULT_DIM_TYPE
@test promote_type(Quantity{Float32,D}, Float64) == Number
@test promote_type(Quantity{Float32,D}, Float64) == Quantity{Float64,D}
@test promote_type(Quantity{Float32,D}, Quantity{Float64,D}) == Quantity{Float64,D}
@test promote_type(Quantity{Float32,D}, GenericQuantity{Float64,D}) == GenericQuantity{Float64,D}
@test promote_type(GenericQuantity{Float32,D}, GenericQuantity{Float64,D}) == GenericQuantity{Float64,D}
Expand Down