Skip to content
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

Fix use of offset units for higher dimensional units (e.g. gauge pressure #1066) #1409

Merged
merged 7 commits into from
Feb 3, 2022
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
2 changes: 2 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Pint Changelog
0.19 (unreleased)
-----------------

- Fix a bug for offset units of higher dimension, e.g. gauge pressure.
(Issue #1066, thanks dalito)
- Fix type hints of function wrapper (Issue #1431)
- Upgrade min version of uncertainties to 3.1.4
- Fix setting options of the application registry (Issue #1403).
Expand Down
10 changes: 5 additions & 5 deletions pint/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
from ._typing import F, QuantityOrUnitLike
from .compat import HAS_BABEL, babel_parse, tokenizer
from .context import Context, ContextChain
from .converters import LogarithmicConverter, ScaleConverter
from .converters import ScaleConverter
from .definitions import (
AliasDefinition,
Definition,
Expand Down Expand Up @@ -1463,10 +1463,10 @@ def _validate_and_extract(self, units):

return None

def _add_ref_of_log_unit(self, offset_unit, all_units):
def _add_ref_of_log_or_offset_unit(self, offset_unit, all_units):

slct_unit = self._units[offset_unit]
if isinstance(slct_unit.converter, LogarithmicConverter):
if slct_unit.is_logarithmic or (not slct_unit.is_multiplicative):
# Extract reference unit
slct_ref = slct_unit.reference
# If reference unit is not dimensionless
Expand Down Expand Up @@ -1534,13 +1534,13 @@ def _convert(self, value, src, dst, inplace=False):
value = self._units[src_offset_unit].converter.to_reference(value, inplace)
src = src.remove([src_offset_unit])
# Add reference unit for multiplicative section
src = self._add_ref_of_log_unit(src_offset_unit, src)
src = self._add_ref_of_log_or_offset_unit(src_offset_unit, src)

# clean dst units from offset units
if dst_offset_unit:
dst = dst.remove([dst_offset_unit])
# Add reference unit for multiplicative section
dst = self._add_ref_of_log_unit(dst_offset_unit, dst)
dst = self._add_ref_of_log_or_offset_unit(dst_offset_unit, dst)

# Convert non multiplicative units to the dst.
value = super()._convert(value, src, dst, inplace, False)
Expand Down
11 changes: 11 additions & 0 deletions pint/testsuite/test_issues.py
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,17 @@ def test_issue1062_issue1097(self):
q = ureg.Quantity(1, "nm")
q.to("J")

def test_issue1066(self):
"""Verify calculations for offset units of higher dimension"""
ureg = UnitRegistry()
ureg.define("barga = 1e5 * Pa; offset: 1e5")
ureg.define("bargb = 1 * bar; offset: 1")
q_4barg_a = ureg.Quantity(4, ureg.barga)
q_4barg_b = ureg.Quantity(4, ureg.bargb)
q_5bar = ureg.Quantity(5, ureg.bar)
helpers.assert_quantity_equal(q_4barg_a, q_5bar)
helpers.assert_quantity_equal(q_4barg_b, q_5bar)

def test_issue1086(self):
# units with prefixes should correctly test as 'in' the registry
assert "bits" in ureg
Expand Down