Solving Inequalities
An inequality such as describes a whole range of numbers rather than a single value. It is solved almost exactly like an equation, with one extra rule: multiplying or dividing both sides by a negative number reverses the direction of the inequality sign. This lesson explains the rule, the ways to write the answer, and how inequalities show up as range checks and clamping in code. The first look is Inequalities in Basic Math.
The symbols
| Symbol | Read as | Boundary included? | Interval for "" |
|---|---|---|---|
| less than | no | ||
| less than or equal to | yes | ||
| greater than | no | ||
| greater than or equal to | yes |
In interval notation, a round bracket ( ) means the endpoint is excluded and a square bracket [ ] means it is included. always gets a round bracket, since it is not a number. On a number line, an open circle ○ marks an excluded endpoint and a filled circle ● an included one.
The rules
You may do these to both sides without changing the direction:
- add or subtract any number,
- multiply or divide by a positive number.
If you multiply or divide by a negative number, flip the sign: becomes , becomes .
Why? Multiplying by −1 mirrors the number line. is true, but is false: after mirroring, −2 is to the right of −5, so .
a, b = 2, 5
print(a < b) # True
print(-a < -b) # False: the order reversed
print(-a > -b) # True after flipping the sign
print(a / -1 > b / -1) # dividing by a negative flips tooTrue
False
True
True
Worked examples
Tip: To check an inequality, test one number inside your answer (it should work) and one outside (it should fail). Testing the boundary itself tells you whether it belongs.
You can avoid dividing by a negative altogether by moving the terms to the side where the coefficient stays positive: becomes , so , the same answer.
SymPy's solveset gives the interval directly, and reduce_inequalities gives it as a condition:
from sympy import symbols, solveset, reduce_inequalities, S
x = symbols("x")
print(solveset(3*x - 4 < 11, x, S.Reals))
print(solveset(7 - 2*x >= 13, x, S.Reals))
print(solveset(5*(x - 1) > 2*x + 7, x, S.Reals))
print(reduce_inequalities(7 - 2*x >= 13, x))Interval.open(-oo, 5)
Interval(-oo, -3)
Interval.open(4, oo)
(-oo < x) & (x <= -3)
Interval(-oo, -3) is SymPy's way to print : the end is never actually included.
In software: validation and clamping
Inequalities are the language of input validation. A form that accepts an age of at least 13 checks age >= 13; a budget field that must not exceed the balance checks amount <= balance. Solving an inequality tells you the valid range of an input, for example "how many items can I buy for €50 if shipping is €4.50 and items are €6.50?": gives , so at most 7.
Clamping forces a value into a range, which is two inequalities enforced at once: the result is the nearest value with .
def clamp(v, lo, hi):
return max(lo, min(v, hi))
for v in [-12, 40, 130]:
print(v, "->", clamp(v, 0, 100))
# most items for 50 euros: 6.5n + 4.5 <= 50 -> n <= 7
n_max = int((50 - 4.5) // 6.5)
print("max items:", n_max, "cost:", 6.5 * n_max + 4.5)-12 -> 0
40 -> 40
130 -> 100
max items: 7 cost: 50.0
Volume sliders, health bars, camera angles and colour channels (0–255) are all clamped. In a game loop, health = clamp(health + heal, 0, max_health) is an inequality guarantee that a later lesson, Compound Inequalities, writes as .
Exercises
Exercise 1: Basic
Solve and write the answer in interval notation.
Answer
, that is
Solution
Dividing by +4 does not flip the sign, even though the right side is negative. Only the sign of the number you divide by matters.
Exercise 2: Negative coefficient
Solve .
Answer
, that is
Solution
Exercise 3: A budget
A gym membership costs €30 a month, or €12 a month plus €4 per class. For how many classes per month is the second option cheaper?
Answer
Fewer than 4.5 classes, so at most 4 classes a month.
Solution
Classes are whole numbers, so . At 5 classes it costs €32, more than €30.
from sympy import symbols, solveset, S
c = symbols("c")
print(solveset(12 + 4*c < 30, c, S.Reals))
print([n for n in range(8) if 12 + 4*n < 30])Interval.open(-oo, 9/2)
[0, 1, 2, 3, 4]
Exercise 4: A validator
Write valid_quantity(q) that accepts whole numbers with and rejects everything else. Test it on 0, 1, 99, 100 and 2.5.
Answer
False, True, True, False, False
Solution
def valid_quantity(q):
return isinstance(q, int) and 1 <= q <= 99
print([valid_quantity(q) for q in [0, 1, 99, 100, 2.5]])[False, True, True, False, False]
Test yourself
Solve .
Which interval means ' is at least 2'?
What does clamp(v, 0, 255) return for v = 300?
- 300
- 0
- 255
- 45