pint icon indicating copy to clipboard operation
pint copied to clipboard

Equality checks are inconsistent

Open jheintz opened this issue 1 year ago • 2 comments

Hi all, just getting into pint now and loving it so far! Ran into the following confusion. What am I supposed to be doing here?

In code I had expected every == to be True.

Python 3.11.4 (main, Jun 20 2023, 16:59:59) [Clang 14.0.3 (clang-1403.0.22.14.1)] on darwin
>>> from pint import UnitRegistry
>>> ureg = UnitRegistry()

>>> day = 7 * ureg.days
>>> week = 1 * ureg.week
>>> day == week
False
>>> daytoweek = day.to(ureg.week)
>>> daytoweek == week
False
>>> weektoday = week.to(ureg.day)
>>> weektoday == day
True
>>> day, week, daytoweek, weektoday
(<Quantity(7, 'day')>, <Quantity(1, 'week')>, <Quantity(1.0, 'week')>, <Quantity(7.0, 'day')>)

jheintz avatar Aug 20 '23 20:08 jheintz

It's caused by a floating point error. I'll check that deeper if there is a quick fix that does not affect performance

jules-ch avatar Sep 12 '23 12:09 jules-ch

I actually found something similar:

>>> from pint import Quantity as Q_
>>> Q_(8.0, 'mm') == Q_(0.008, 'm')
True
>>> Q_(9.0, 'mm') == Q_(0.009, 'm')
False

which doesn't make sense... but is solved if using np.allclose:

>>> np.allclose(Q_(9.0, 'mm'), Q_(0.009, 'm'))
True

seb5g avatar Jul 19 '24 10:07 seb5g