diff --git a/NEWS.md b/NEWS.md index fa76d1a9..06ee708c 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,6 @@ +- v0.2.2 + - Fixed a bug in promotion involving `ContextUnits` where the promotion context might + not be properly retained. - v0.2.1 - Fixed `isapprox` bug [#74](https://github.com/ajkeller34/Unitful.jl/pull/74). - Added `DimensionlessQuantity` methods for `exp`, `exp10`, `exp2`, `expm1`, `log1p`, diff --git a/src/Promotion.jl b/src/Promotion.jl index 3a3a32a7..3fb5b7dc 100644 --- a/src/Promotion.jl +++ b/src/Promotion.jl @@ -164,8 +164,9 @@ function Base.promote_rule{S1,S2,D,U1,U2}(::Type{Quantity{S1,D,U1}}, typeof(convfact(p,U1())), typeof(convfact(p,U2()))) if !isunitless(p) if U1 <: ContextUnits && U2 <: ContextUnits - if upreferred(U1()) === upreferred(U2()) - return Quantity{numtype,D,typeof(ContextUnits(p,p))} + up1 = upreferred(U1()) + if up1 === upreferred(U2()) + return Quantity{numtype,D,typeof(ContextUnits(p,up1))} else return Quantity{numtype,D,typeof(p)} end diff --git a/test/runtests.jl b/test/runtests.jl index 4bdc68ef..caa82030 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -267,6 +267,8 @@ end μm2mm = ContextUnits(μm,mm) @test @inferred(promote(1.0nm2μm, 1.0m)) === (0.001μm2μm, 1e6μm2μm) @test @inferred(promote(1.0m, 1.0μm2μm)) === (1e6μm2μm, 1.0μm2μm) + @test ===(upreferred.(unit.(promote(1.0nm2μm, 2nm2μm)))[1], ContextUnits(μm,μm)) + @test ===(upreferred.(unit.(promote(1.0nm2μm, 2nm2μm)))[2], ContextUnits(μm,μm)) # Context agreement @test @inferred(promote(1.0nm2μm, 1.0μm2μm)) ===