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

Ordering problem in intervals #64

Closed
jefftrull opened this issue Sep 10, 2021 · 3 comments
Closed

Ordering problem in intervals #64

jefftrull opened this issue Sep 10, 2021 · 3 comments
Labels
invalid This doesn't seem right

Comments

@jefftrull
Copy link
Contributor

Intervals implement the special comparison methods, but in some corner cases they behave in surprising ways:

import portion as P

a = P.openclosed(0, 1)
b = P.closedopen(1, 2)

print(b > a)
print(b == a)
print(b >= a)

prints:

False
False
True

Similarly,

print(a < b)
print(a == b)
print(a <= b)

prints

False
False
True

This violates the expectation that (x >= y) == ((x > y) or (x == y)).

In my view the right fix is to make each of the first expressions above - that is, b > a and a < b - return true for these cases.

@AlexandreDecan
Copy link
Owner

AlexandreDecan commented Sep 10, 2021

Hello,

While it may seem incorrect, this is the expected behaviour of comparison operators. As stated in the documentation (see https://github.com/AlexandreDecan/portion#comparison-operators):

Moreover, intervals are comparable using >, >=, < or <=. These comparison operators have a different behaviour than the usual ones. For instance, a < b holds if a is entirely on the left of the lower bound of b and a > b holds if a is entirely on the right of the upper bound of b. [...] Similarly, a <= b holds if a is entirely on the left of the upper bound of b, and a >= b holds if a is entirely on the right of the lower bound of b. [...] Note that all these semantics differ from classical comparison operators. As a consequence, some intervals are never comparable in the classical sense.

For example:

>>> P.closed(0, 4) <= P.closed(1, 2) or P.closed(0, 4) >= P.closed(1, 2)
False
>>> P.closed(0, 4) < P.closed(1, 2) or P.closed(0, 4) > P.closed(1, 2)
False
>>> P.empty() < P.empty()
True
>>> P.empty() < P.closed(0, 1) and P.empty() > P.closed(0, 1)
True

@AlexandreDecan AlexandreDecan added the invalid This doesn't seem right label Sep 10, 2021
@jefftrull
Copy link
Contributor Author

Wow! OK, thanks. I missed that part in the docs.

@AlexandreDecan
Copy link
Owner

No problem ;-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
invalid This doesn't seem right
Projects
None yet
Development

No branches or pull requests

2 participants