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

py27 loss of precision #871

Closed
crusaderky opened this issue Sep 5, 2019 · 0 comments · Fixed by #908
Closed

py27 loss of precision #871

crusaderky opened this issue Sep 5, 2019 · 0 comments · Fixed by #908

Comments

@crusaderky
Copy link
Contributor

There are precision issues in python 2.7:

>>> ureg.define("mylen = 1.000000000001 m")
>>> ureg.Quantity(1, "mylen").to("m")
<Quantity(1.0, 'meter')>

In Python 3:

1.000000000001 <Unit('meter')>

This is caused by incorrectly using str(float) instead of repr(float) across the codebase. In Python 2.7, the two are not the same:

>>> str(1.000000000001)
'1.0'
>>> repr(1.000000000001)
'1.000000000001'

I noticed this issue after my eye fell on one of these cases:

pint/pint/registry.py

Lines 751 to 754 in c777e3c

if isinstance(value, Decimal):
factor = Decimal(str(factor))
elif isinstance(value, Fraction):
factor = Fraction(str(factor))

Personal note: I hold no interest in Python 2.7 so I'm afraid I'm not volunteering to fix this. I strongly suggest canning support for Python < 3.5 ASAP.

@bors bors bot closed this as completed in 5f89246 Dec 3, 2019
@bors bors bot closed this as completed in #908 Dec 3, 2019
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

Successfully merging a pull request may close this issue.

1 participant