pint
pint copied to clipboard
comparing positive and negative angles
import pint
ureg = pint.UnitRegistry()
-140 * ureg.degrees == 220 * ureg.degrees
return False. Is it possible for it to return True instead?
xref https://github.com/xarray-contrib/pint-xarray/issues/119
I personally do not think it is a good idea. While in some contexts it makes total sense, in some others both angles represent totally different things: think , e.g. if you are describing an orientation change (one would be clock-wise and the other counter-clockwise).
I would suggest that, if in your context both are equal, you compare using mod() explicitly:
import pint
import numpy
ureg = pint.UnitRegistry()
numpy.mod(-140 * ureg.degrees, 360 * ureg.degrees) == numpy.mod(220 * ureg.degrees, 360 * ureg.degrees)
Same here, if you want this I'd suggest wrapping this in a function.
I know it sounds appealing at first, but I'd rather put those kind of examples in the docs.
from math import pi
import pint
ureg = pint.UnitRegistry()
-140 * ureg.degrees % (360 * ureg.degrees) == 220 * ureg.degrees
-140 * ureg.degrees % (1 * ureg.turn) == 220 * ureg.degrees
-140 * ureg.degrees % (2 * pi) == 220 * ureg.degrees
I think there are multiple concepts here: "relative angles" or orientation changes, which can describe multiple turns and can be positive or negative (clockwise and counter-clockwise, depending on the definition), and "absolute angles" or "positions on a circle" which are always in the range of [0, 2π[ (or [-π, π], not sure which bound should be excluded?), using mod to map values outside of that range.
I think we should try to support both, probably by adding special "absolute angle" units or using contexts. I guess I would prefer the special units because it might make sense to use both variants in the same equation.
I agree wth @cpascual and @jules-ch, I think such a feature (while tempting in particular applications) would complicate the code and generate unexpected results for some user.
Regarding @keewis suggestions, not sure how to do it.
What I usually do in my code is having a function to normalize angles which I run after any code that computes an angle. I am not sure if this belongs in pint, and if it does, is it a function or a quantity method.
+1 to adding @jules-ch 's examples to the docs (specially the 3rd one which is both very compact and self-explanatory)