Skip to content

Commit a66ec1d

Browse files
authored
Merge branch 'main' into more-functions-3
2 parents 17b01b9 + cd30223 commit a66ec1d

File tree

6 files changed

+60
-8
lines changed

6 files changed

+60
-8
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "DynamicQuantities"
22
uuid = "06fc5a27-2a28-4c7c-a15d-362465fb6821"
33
authors = ["MilesCranmer <[email protected]> and contributors"]
4-
version = "0.8.0"
4+
version = "0.8.2"
55

66
[deps]
77
Compat = "34da2185-b29b-5c13-b0c7-acf172513d20"

ext/DynamicQuantitiesUnitfulExt.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ Base.convert(::Type{Unitful.Quantity}, x::DynamicQuantities.Quantity) =
2828
validate_upreferred()
2929
cumulator = DynamicQuantities.ustrip(x)
3030
dims = DynamicQuantities.dimension(x)
31+
if dims isa DynamicQuantities.SymbolicDimensions
32+
throw(ArgumentError("Conversion of a `DynamicQuantities.Quantity` to a `Unitful.Quantity` is not defined with dimensions of type `SymbolicDimensions`. Instead, you can first use the `uexpand` function to convert the dimensions to their base SI form of type `Dimensions`, then convert this quantity to a `Unitful.Quantity`."))
33+
end
3134
equiv = unitful_equivalences()
3235
for dim in keys(dims)
3336
value = dims[dim]

src/units.jl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ end
6868
kg,
6969
)
7070
@doc(
71-
"Time in seconds. Available variants: `fs`, `ps`, `ns`, `μs` (/`us`), `ms`, `min`, `h` (/`hr`), `day`, `yr`, `kyr`, `Myr`, `Gyr`.",
71+
"Time in seconds. Available variants: `fs`, `ps`, `ns`, `μs` (/`us`), `ms`, `min` (/`minute`), `h` (/`hr`), `day` (/`d`), `wk`, `yr`, `kyr`, `Myr`, `Gyr`.",
7272
s,
7373
)
7474
@doc(
@@ -158,15 +158,21 @@ end
158158
# Common assorted units
159159
## Time
160160
@register_unit min 60 * s
161+
@register_unit minute min
161162
@register_unit h 60 * min
162163
@register_unit hr h
163164
@register_unit day 24 * h
165+
@register_unit d day
166+
@register_unit wk 7 * day
164167
@register_unit yr 365.25 * day
165168

166169
@add_prefixes min ()
170+
@add_prefixes minute ()
167171
@add_prefixes h ()
168172
@add_prefixes hr ()
169173
@add_prefixes day ()
174+
@add_prefixes d ()
175+
@add_prefixes wk ()
170176
@add_prefixes yr (k, M, G)
171177

172178
## Volume

src/utils.jl

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,29 @@ end
4242
function Base.promote_rule(::Type{<:Quantity{T1,D1}}, ::Type{<:Quantity{T2,D2}}) where {T1,T2,D1,D2}
4343
return Quantity{promote_type(T1,T2),promote_type(D1,D2)}
4444
end
45+
46+
# Define promotion rules for all basic numeric types, individually.
47+
# We don't want to define an opinionated promotion on <:Number,
48+
# or even <:AbstractFloat, as it could conflict with other
49+
# abstract number packages which may try to do the same thing.
50+
# (which would lead to ambiguities)
51+
const BASE_NUMERIC_TYPES = Union{
52+
Bool, Int8, UInt8, Int16, UInt16, Int32, UInt32,
53+
Int64, UInt64, Int128, UInt128, Float16, Float32,
54+
Float64, BigFloat, BigInt, ComplexF16, ComplexF32,
55+
ComplexF64, Complex{BigFloat}, Rational{Int8}, Rational{UInt8},
56+
Rational{Int16}, Rational{UInt16}, Rational{Int32}, Rational{UInt32},
57+
Rational{Int64}, Rational{UInt64}, Rational{Int128}, Rational{UInt128},
58+
Rational{BigInt},
59+
}
60+
for (type, _, _) in ABSTRACT_QUANTITY_TYPES
61+
@eval function Base.promote_rule(::Type{Q}, ::Type{T2}) where {T,D,Q<:$type{T,D},T2<:BASE_NUMERIC_TYPES}
62+
return with_type_parameters(Q, promote_type(T, T2), D)
63+
end
64+
@eval function Base.convert(::Type{Q}, x::BASE_NUMERIC_TYPES) where {T,D,Q<:$type{T,D}}
65+
return new_quantity(Q, convert(T, x), D())
66+
end
67+
end
4568
function Base.promote_rule(::Type{<:AbstractQuantity}, ::Type{<:Number})
4669
return Number
4770
end

test/test_unitful.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,8 @@ x = 1.0u"scale"
3838
@test typeof(x) <: Unitful.Quantity{Float64, MyScaleUnit.𝐒}
3939
@test_throws ErrorException convert(DynamicQuantities.Quantity, x)
4040
# These are not supported because there is no SI equivalency
41+
42+
# issue 79
43+
symbolic = DynamicQuantities.us"s"
44+
@test_throws ArgumentError convert(Unitful.Quantity, symbolic)
45+
@test convert(Unitful.Quantity, DynamicQuantities.uexpand(symbolic)) == 1.0 * Unitful.u"s"

test/unittests.jl

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -467,15 +467,30 @@ end
467467
q2 = Quantity(2, mass=1)
468468
@test typeof(promote(q1, q2)) == typeof((q1, q1))
469469

470-
x = [0.5, 0.5u"km/s"]
471-
@test x isa Vector{Number}
472-
473-
x = [0.5, GenericQuantity(0.5u"km/s")]
474-
@test x isa Vector{Any}
470+
q = 0.5u"km/s"
471+
x = [0.5, q]
472+
@test x isa Vector{typeof(q)}
473+
@test x[1] == convert(typeof(q), 0.5)
474+
475+
q = GenericQuantity(0.5u"km/s")
476+
x = [0.5, q]
477+
@test x isa Vector{typeof(q)}
478+
479+
# Promotion with custom numeric type:
480+
@eval struct MyNumber <: Real
481+
x::Float64
482+
end
483+
a = 0.5u"km/s"
484+
b = MyNumber(0.5)
485+
ar = [a, b]
486+
@test ar isa Vector{Number}
487+
@test a === ar[1]
488+
@test b === ar[2]
489+
@test promote_type(MyNumber, typeof(a)) == Number
475490

476491
# Explicit conversion so coverage can see it:
477492
D = DEFAULT_DIM_TYPE
478-
@test promote_type(Quantity{Float32,D}, Float64) == Number
493+
@test promote_type(Quantity{Float32,D}, Float64) == Quantity{Float64,D}
479494
@test promote_type(Quantity{Float32,D}, Quantity{Float64,D}) == Quantity{Float64,D}
480495
@test promote_type(Quantity{Float32,D}, GenericQuantity{Float64,D}) == GenericQuantity{Float64,D}
481496
@test promote_type(GenericQuantity{Float32,D}, GenericQuantity{Float64,D}) == GenericQuantity{Float64,D}

0 commit comments

Comments
 (0)