2
$\begingroup$

How does this:

(a > b && a > c && b <= c) ||
(a > b && a <= c && b < c)

simplify down to this:

a > b && b <= c

Whereas this similar expression

(a <= c && c >= b && a <= b) ||
(a <= c && c < b && a < b)

does not simplify to

a <= c && a <= b

but instead to

(b <= c && a <= b) ||
(b > c && a <= c)

?

Edit: Just to clarify, I can work it out in my head, but I'm trying to implement the simplification in code for general cases, and I'm struggling to understand the logic that separates the above two equations.

$\endgroup$

1 Answer 1

2
$\begingroup$

For the first part:

If a > b && b <= c, a > b && a > c && b <= c simplifies to a > c.

Similarly, a > b && a <= c && b < c simplifies to a <= c && b < c.

Now, let a > b && b <= c. Then there are two cases:

  1. a > c. Then the first condition will hold.
  2. a <= c. Then b < a <= c, so b < c holds and the second inequality is true.

This second step is not possible in the other case. You cannot derive a similar order, because the arrangement of variables is changed.

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .