TExcelTAD
Home › Algebra › Compound Inequalities

Compound Inequalities

A compound inequality joins two inequalities with "and" or "or". An "and" inequality such as −2<x≤5-2 < x \le 5 describes the numbers between two bounds, an interval; an "or" inequality such as x<−1x < -1 or x>4x > 4 describes numbers outside a range. This lesson solves both kinds, writes the answers as intervals, and maps them onto the range checks and boolean logic in code.

"And": the overlap

"x>−2x > -2 and x≤5x \le 5" holds only where both are true, the intersection of the two sets. It is usually written as one chain:

−2<x≤5interval (−2,5]-2 < x \le 5 \qquad \text{interval } (-2, 5]

To solve a chained inequality, do the same operation to all three parts:

−7≤3x+2<11−9≤3x<9subtract 2 from all three parts−3≤x<3divide all three by 3answer: [−3,3)\begin{aligned} -7 \le 3x + 2 &< 11 \\ -9 \le 3x &< 9 && \text{subtract 2 from all three parts} \\ -3 \le x &< 3 && \text{divide all three by 3} \\ &\text{answer: } [-3, 3) \end{aligned}

With a negative coefficient, flip both signs, then rewrite the chain so the smaller number is on the left:

1<5−2x≤9−4<−2x≤4subtract 52>x≥−2divide by −2: both signs flip−2≤x<2same statement, read from the leftanswer: [−2,2)\begin{aligned} 1 < 5 - 2x &\le 9 \\ -4 < -2x &\le 4 && \text{subtract 5} \\ 2 > x &\ge -2 && \text{divide by } {-2}\text{: both signs flip} \\ -2 \le x &< 2 && \text{same statement, read from the left} \\ &\text{answer: } [-2, 2) \end{aligned}

Note: A chain a<x<ba < x < b only makes sense when a<ba < b. "5<x<15 < x < 1" describes no numbers at all, and an "and" inequality can have an empty answer.

"Or": the union

"x<−1x < -1 or x>4x > 4" holds where at least one is true, the union. It cannot be written as a single chain; use two intervals joined by ∪\cup:

2x+3<1orx−4≥22x<−2orx≥6x<−1orx≥6answer: (−∞,−1)∪[6,∞)\begin{aligned} 2x + 3 < 1 \quad &\text{or} \quad x - 4 \ge 2 \\ 2x < -2 \quad &\phantom{\text{or}} \quad x \ge 6 \\ x < -1 \quad &\text{or} \quad x \ge 6 \\[1ex] &\text{answer: } (-\infty, -1) \cup [6, \infty) \end{aligned}

If the two pieces overlap, an "or" can cover every number: x<5x < 5 or x>2x > 2 is true for all real xx.

Type Logic Set operation Typical shape
a<xa < x and x<bx < b both intersection ∩\cap one bounded interval
x<ax < a or x>bx > b at least one union ∪\cup two rays pointing away

In SymPy, solve each part with solveset and combine the sets: & is the intersection ("and") and | the union ("or"):

python · try it yourself
from sympy import symbols, solveset, S

x = symbols("x")
def sol(ineq):
    return solveset(ineq, x, S.Reals)

print(sol(-7 <= 3*x + 2) & sol(3*x + 2 < 11))
print(sol(1 < 5 - 2*x) & sol(5 - 2*x <= 9))
print(sol(2*x + 3 < 1) | sol(x - 4 >= 2))
print(sol(x < 3) & sol(x > 6))          # an empty "and"
print(sol(x < 5) | sol(x > 2))          # an "or" that covers everything
Interval.Ropen(-3, 3)
Interval.Ropen(-2, 2)
Union(Interval.open(-oo, -1), Interval(6, oo))
EmptySet
Interval(-oo, oo)

Interval.Ropen(-3, 3) means "right-open", [−3,3)[-3, 3), and Interval(-oo, oo) is the whole number line.

In software: range checks

Python is one of the few languages that lets you write a chain exactly as in algebra: 0 <= x < width means 0 <= x and x < width. In most C-family languages you must write x >= 0 && x < width; the chain 0 <= x < width in C compares the boolean result of 0 <= x (0 or 1) with width, a classic bug.

The half-open interval [0,n)[0, n) is the programmer's favourite: array indices run from 0 up to but not including the length, range(0, n) and slices a[i:j] are half-open, and adjacent half-open ranges [0,10)[0, 10), [10,20)[10, 20) tile without overlap or gap.

python · try it yourself
def in_bounds(x, y, width, height):
    return 0 <= x < width and 0 <= y < height

print(in_bounds(0, 0, 640, 480), in_bounds(639, 479, 640, 480))
print(in_bounds(640, 10, 640, 480), in_bounds(-1, 10, 640, 480))

def grade(score):        # half-open bands tile the range [0, 100]
    if 90 <= score: return "A"
    if 75 <= score < 90: return "B"
    if 50 <= score < 75: return "C"
    return "F"

print([grade(s) for s in [49.9, 50, 74.99, 75, 90]])
True True
False False
['F', 'C', 'C', 'B', 'A']

"Or" conditions describe what is outside a range: if temp < 2 or temp > 8: alarm() for a fridge that must stay in [2,8][2, 8]. By De Morgan's law, "not (2≤t≤8)\text{not } (2 \le t \le 8)" is exactly "t<2 or t>8t < 2 \text{ or } t > 8", which is how you turn an "is valid" check into an "is invalid" check.

Exercises

Exercise 1: A chain

Solve −5<2x−1≤7-5 < 2x - 1 \le 7 and give the interval.

Answer

−2<x≤4-2 < x \le 4, that is (−2,4](-2, 4]

Solution
−4<2x≤8add 1 to all parts−2<x≤4divide by 2\begin{aligned} -4 < 2x &\le 8 && \text{add 1 to all parts} \\ -2 < x &\le 4 && \text{divide by 2} \end{aligned}

Exercise 2: Negative coefficient

Solve −3≤4−x<6-3 \le 4 - x < 6.

Answer

−2<x≤7-2 < x \le 7, that is (−2,7](-2, 7]

Solution
−7≤−x<2subtract 47≥x>−2multiply by −1, flip both signs−2<x≤7check x=7: 4−7=−3, and −3≤−3 ✓check x=−2: 6<6 ×, so −2 excluded ✓\begin{aligned} -7 \le -x &< 2 && \text{subtract 4} \\ 7 \ge x &> -2 && \text{multiply by } {-1}\text{, flip both signs} \\ -2 < x &\le 7 \\[1ex] &\text{check } x = 7{:}\ 4 - 7 = -3,\ \text{and } {-3} \le -3\ \checkmark \qquad \text{check } x = -2{:}\ 6 < 6\ \boldsymbol{\times},\ \text{so } {-2} \text{ excluded}\ \checkmark \end{aligned}

Exercise 3: An "or"

Solve 3x−2<−83x - 2 < -8 or 2x+1>92x + 1 > 9.

Answer

x<−2x < -2 or x>4x > 4, that is (−∞,−2)∪(4,∞)(-\infty, -2) \cup (4, \infty)

Solution
3x<−6  ⇒  x<−22x>8  ⇒  x>4\begin{aligned} 3x < -6 \;&\Rightarrow\; x < -2 \\ 2x > 8 \;&\Rightarrow\; x > 4 \end{aligned}
python · try it yourself
from sympy import symbols, solveset, S
x = symbols("x")
print(solveset(3*x - 2 < -8, x, S.Reals) | solveset(2*x + 1 > 9, x, S.Reals))
Union(Interval.open(-oo, -2), Interval.open(4, oo))

Exercise 4: Invert a check

A sensor reading is valid when 10≤r≤9010 \le r \le 90. Write invalid(r) using "or", without not, and test it on 9.9, 10, 90, 90.1.

Answer

r < 10 or r > 90; results True, False, False, True.

Solution

By De Morgan, not (10≤r and r≤90)\text{not } (10 \le r \text{ and } r \le 90) is r<10 or r>90r < 10 \text{ or } r > 90.

python · try it yourself
def invalid(r):
    return r < 10 or r > 90

print([invalid(r) for r in [9.9, 10, 90, 90.1]])
[True, False, False, True]

Test yourself

Which interval is the solution of 2≤x+3<72 \le x + 3 < 7?

  1. [−1,4)[-1, 4)
  2. (−1,4](-1, 4]
  3. [5,10)[5, 10)
  4. [−1,4][-1, 4]

What is the solution of x<3x < 3 and x>6x > 6?

  1. (3,6)(3, 6)
  2. (−∞,3)∪(6,∞)(-\infty, 3) \cup (6, \infty)
  3. No numbers at all
  4. Every real number

Why are indices in Python described by the half-open interval [0,n)[0, n)?

  1. Because 0 is excluded
  2. Valid indices are 0 up to but not including nn
  3. Because nn is negative
  4. Because indices can be fractions