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

converting between units and equality #2059

Closed
yqiang opened this issue Oct 7, 2024 · 1 comment
Closed

converting between units and equality #2059

yqiang opened this issue Oct 7, 2024 · 1 comment

Comments

@yqiang
Copy link

yqiang commented Oct 7, 2024

pint 0.24.3

I would expect that when converting from say 1 lb to 16 oz and then back to lb would be equivalent, but it doesn't seem to be the case:

In [21]: from pint import UnitRegistry

In [22]: ureg = UnitRegistry()

In [23]: (1 * ureg.lb).to(ureg.oz)
Out[23]: <Quantity(16.0, 'ounce')>

In [24]: (1 * ureg.lb).to(ureg.oz) == 16 * ureg.oz
Out[24]: False

In [25]: (1 * ureg.lb).to(ureg.oz) == 16.0 * ureg.oz
Out[25]: False

It appears that it might have to do with floating point precision:

In [28]: (1 * ureg.lb).to(ureg.oz).magnitude
Out[28]: 16.000000000000004

Is this the expected behavior?

@andrewgsavage
Copy link
Collaborator

It appears that it might have to do with floating point precision:

correct

Is this the expected behavior?

yes, you must be careful when doing comparisons and equality checks for floats
the other option is to use decimals

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants