Skip to content

Commit 61c90cc

Browse files
authored
Merge pull request #172 from SymbolicML/fix-affine-promotion
fix: affine unit promotion in ustrip
2 parents 4756b19 + a4df72b commit 61c90cc

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

src/affine_dimensions.jl

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,13 @@ julia> ustrip(ua"degF", 300u"K")
131131
```
132132
"""
133133
function ustrip(unit::AffineUnit, q::UnionAbstractQuantity)
134-
unit.basedim == dimension(q) || throw(DimensionError(unit, q))
135-
return (ustrip(q) - unit.offset) / unit.scale
134+
q_promoted = _promote_quantity_for_affine(unit, q)
135+
unit.basedim == dimension(q_promoted) || throw(DimensionError(unit, q_promoted))
136+
return (ustrip(q_promoted) - unit.offset) / unit.scale
137+
end
138+
139+
function _promote_quantity_for_affine(unit::AffineUnit, q::UnionAbstractQuantity{T,D}) where {T,D}
140+
D_new = promote_type(D, typeof(unit.basedim))
141+
Q_new = with_type_parameters(typeof(q), T, D_new)
142+
return convert(Q_new, q)
136143
end

test/unittests.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2098,6 +2098,10 @@ end
20982098
@test ustrip(ua"degC", 22ua"degC") 22.0
20992099
@test ustrip(ua"degF", 22ua"degC") 71.6
21002100
@test_throws DimensionError ustrip(ua"degC", 1.0u"m")
2101+
2102+
# Test symbolic units with ustrip
2103+
@test ustrip(ua"degC", 273.15us"K") 0.0
2104+
@test ustrip(ua"degC", 55us"K") -218.15
21012105
end
21022106

21032107
@testset "Test div" begin

0 commit comments

Comments
 (0)