3 ms·
I like this question! Let's assume a<b, x<y, and intervals are open. Okay: max(a,x)<min(b,y), using three comparisons. Better: (a<x)?(b>x):(a<y), using two.
by cousin_it 5y ago
I like this question! Let's assume a<b, x<y, and intervals are open.
Okay: max(a,x)<min(b,y), using three comparisons.
Better: (a<x)?(b>x):(a<y), using two.
Best: (a<y)&&(b>x), using either one or two.
- pklausler 5y agoThose are all fine to me, and I'll follow up with a "how do you know that's right?" if needed. Looping over all valid values in one interval, testing for membership in the other, is kind of a red flag. A predicate that is incorrect and not caught by testing might be a very bad sign.