Skip to content

Commit 3cb83b0

Browse files
committed
Fix promotion of WeakFloat64 and Int
1 parent 2c7f91b commit 3cb83b0

File tree

2 files changed

+16
-14
lines changed

2 files changed

+16
-14
lines changed

src/uparse.jl

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module UnitsParse
33
import ..Quantity
44
import ..DEFAULT_DIM_TYPE
55
import ..DEFAULT_VALUE_TYPE
6-
import ..Units: UNIT_SYMBOLS
6+
import ..Units: UNIT_SYMBOLS, DEFAULT_UNIT_TYPE
77
import ..Constants
88

99
function _generate_units_import()
@@ -30,11 +30,12 @@ the quantity corresponding to the speed of light multiplied by Hertz,
3030
squared.
3131
"""
3232
function uparse(s::AbstractString)
33-
return as_quantity(eval(Meta.parse(s)))::Quantity{DEFAULT_VALUE_TYPE,DEFAULT_DIM_TYPE}
33+
return as_quantity(eval(Meta.parse(s)))::DEFAULT_UNIT_TYPE
3434
end
3535

36-
as_quantity(q::Quantity) = q
37-
as_quantity(x::Number) = Quantity(convert(DEFAULT_VALUE_TYPE, x), DEFAULT_DIM_TYPE)
36+
as_quantity(q::Quantity) = convert(DEFAULT_UNIT_TYPE, q)
37+
as_quantity(x::Number) = DEFAULT_UNIT_TYPE(x)
38+
# Quantity(convert(DEFAULT_VALUE_TYPE, x), DEFAULT_DIM_TYPE)
3839
as_quantity(x) = error("Unexpected type evaluated: $(typeof(x))")
3940

4041
"""

test/unittests.jl

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -379,20 +379,21 @@ end
379379
@test ustrip(z) 60 * 60 * 24 * 365.25
380380

381381
# Test type stability of extreme range of units
382-
@test typeof(u"1") == Quantity{DEFAULT_UNIT_TYPE,DEFAULT_DIM_TYPE}
383-
@test typeof(u"1f0") == Quantity{DEFAULT_UNIT_TYPE,DEFAULT_DIM_TYPE}
384-
@test typeof(u"s"^2) == Quantity{DEFAULT_UNIT_TYPE,DEFAULT_DIM_TYPE}
385-
@test typeof(u"") == Quantity{DEFAULT_UNIT_TYPE,DEFAULT_DIM_TYPE}
386-
@test typeof(u"Gyr") == Quantity{DEFAULT_UNIT_TYPE,DEFAULT_DIM_TYPE}
387-
@test typeof(u"fm") == Quantity{DEFAULT_UNIT_TYPE,DEFAULT_DIM_TYPE}
388-
@test typeof(u"fm"^2) == Quantity{DEFAULT_UNIT_TYPE,DEFAULT_DIM_TYPE}
382+
@test typeof(u"1") == DEFAULT_UNIT_TYPE
383+
@test typeof(u"1f0") == DEFAULT_UNIT_TYPE
384+
@test typeof(u"s"^2) == DEFAULT_UNIT_TYPE
385+
@test typeof(u"") == DEFAULT_UNIT_TYPE
386+
@test typeof(u"Gyr") == DEFAULT_UNIT_TYPE
387+
@test typeof(u"fm") == DEFAULT_UNIT_TYPE
388+
@test typeof(u"fm"^2) == DEFAULT_UNIT_TYPE
389389

390390
# Test type demotion
391-
@test typeof(1u"m") == Quantity{Int64,DEFAULT_DIM_TYPE}
391+
@test typeof(1u"m") == Quantity{Float64,DEFAULT_DIM_TYPE}
392392
@test typeof(1f0u"m") == Quantity{Float32,DEFAULT_DIM_TYPE}
393393
@test typeof(1.0u"m") == Quantity{Float64,DEFAULT_DIM_TYPE}
394+
@test typeof(Float16(1.0)u"m") == Quantity{Float16,DEFAULT_DIM_TYPE}
394395

395-
@test typeof(1u"m^2/s") == Quantity{Int64,DEFAULT_DIM_TYPE}
396+
@test typeof(1u"m^2/s") == Quantity{Float64,DEFAULT_DIM_TYPE}
396397
@test typeof(1f0u"m^2/s") == Quantity{Float32,DEFAULT_DIM_TYPE}
397398
@test typeof(1.0u"m^2/s") == Quantity{Float64,DEFAULT_DIM_TYPE}
398399

@@ -651,7 +652,7 @@ end
651652
@test promote(x, y) == (x, y)
652653
@test_throws ErrorException promote(x, convert(FixedRational{Int32,100}, 10))
653654
@test round(Missing, x) === missing
654-
@test promote_type(typeof(u"km/s"), typeof(convert(Quantity{Float32}, u"km/s"))) <: Quantity{Float64}
655+
@test promote_type(typeof(1.0u"km/s"), typeof(convert(Quantity{Float32}, u"km/s"))) <: Quantity{Float64}
655656

656657
x = 1.0u"m"
657658
y = missing

0 commit comments

Comments
 (0)