-
Notifications
You must be signed in to change notification settings - Fork 477
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
Converting a Decimal number results in a loss of precision #2107
Comments
Hi I experience the same issue with different units. import decimal, pint
ureg = pint.UnitRegistry(non_int_type=decimal.Decimal)
flow = decimal.Decimal('2')*ureg.L/ureg.h
print(flow.to(ureg.L/ureg.day)) gives I read through #967 and assumed this was fixed. Maybe this is a new issue? |
The trouble appears to have it's roots in fractions. For my own case the conversion required computation of
To fix this the following change is required to def _get_root_units_recurse(
self, ref: UnitsContainer, exp: Scalar, accumulators: dict[str | None, int]
) -> None:
"""
accumulators None keeps the scalar prefactor not associated with a specific unit.
"""
for key in ref:
exp2 = exp * ref[key]
key = self.get_name(key)
reg = self._units[key]
if reg.is_base:
accumulators[key] += exp2
else:
# Use division operator for division
if exp2 < 0:
accumulators[None] /= reg.converter.scale**abs(exp2)
else:
accumulators[None] *= reg.converter.scale**abs(exp2)
if reg.reference is not None:
self._get_root_units_recurse(reg.reference, exp2, accumulators) With this change import decimal, pint
ureg = pint.UnitRegistry(non_int_type=decimal.Decimal)
flow = decimal.Decimal('2')*ureg.L/ureg.h
print(flow.to(ureg.L/ureg.day).magnitude) gives |
Evidently there are additional problems for your example, @cdwilson. To do the conversion from inch to thou pint performs the following calculation with my patch listed above: thou = Decimal('0.02777777777777777777777777778') * Decimal('0.9144') / Decimal('0.001') / Decimal('0.02777777777777777777777777778') / Decimal('0.9144')
print(thou) which returns I certainly think it could be really interesting to adapt Updated: |
Discussed in #2105
Converting a
Decimal
number frominch
tothou
results in a loss of precision. I would expect that this conversion does the equivalent ofDecimal("1.0") * 1000
, i.e. the conversion returnsDecimal('1000.0')
.Originally posted by cdwilson December 31, 2024
The text was updated successfully, but these errors were encountered: