Skip to content

Commit

Permalink
Cast offset to Decimal if value is Decimal in ; Fix hgrecco#1950
Browse files Browse the repository at this point in the history
  • Loading branch information
biagiodistefano committed Mar 9, 2024
1 parent 60a507d commit b9fb12c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Pint Changelog
- Add `dim_sort` function to _formatter_helpers.
- Add `dim_order` and `default_sort_func` properties to FullFormatter.
(PR #1926, fixes Issue #1841)
- Cast offset to Decimal if value is Decimal in `pint.facets.nonmultiplicative.definitions.OffsetConverter`.
(Fixes #1950)


0.23 (2023-12-08)
Expand Down
11 changes: 7 additions & 4 deletions pint/facets/nonmultiplicative/definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from __future__ import annotations

from dataclasses import dataclass
from decimal import Decimal

from ..._typing import Magnitude
from ...compat import HAS_NUMPY, exp, log
Expand All @@ -26,20 +27,22 @@ def is_multiplicative(self):
return self.offset == 0

def to_reference(self, value: Magnitude, inplace: bool = False) -> Magnitude:
offset = Decimal(self.offset) if isinstance(value, Decimal) else self.offset
if inplace:
value *= self.scale
value += self.offset
value += offset
else:
value = value * self.scale + self.offset
value = value * self.scale + offset

return value

def from_reference(self, value: Magnitude, inplace: bool = False) -> Magnitude:
offset = Decimal(self.offset) if isinstance(value, Decimal) else self.offset
if inplace:
value -= self.offset
value -= offset
value /= self.scale
else:
value = (value - self.offset) / self.scale
value = (value - offset) / self.scale

return value

Expand Down

0 comments on commit b9fb12c

Please sign in to comment.