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

comparing positive and negative angles #1344

Open
dcherian opened this issue Jul 8, 2021 · 5 comments
Open

comparing positive and negative angles #1344

dcherian opened this issue Jul 8, 2021 · 5 comments

Comments

@dcherian
Copy link

dcherian commented Jul 8, 2021

import pint
ureg = pint.UnitRegistry()

-140 * ureg.degrees == 220 * ureg.degrees

return False. Is it possible for it to return True instead?

xref xarray-contrib/pint-xarray#119

@cpascual
Copy link
Contributor

cpascual commented Jul 9, 2021

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)

@jules-ch
Copy link
Collaborator

jules-ch commented Jul 9, 2021

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

@keewis
Copy link
Contributor

keewis commented Jul 9, 2021

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.

@hgrecco
Copy link
Owner

hgrecco commented Jul 9, 2021

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.

@cpascual
Copy link
Contributor

+1 to adding @jules-ch 's examples to the docs (specially the 3rd one which is both very compact and self-explanatory)

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

No branches or pull requests

5 participants