Absolute Value Equations and Inequalities
The absolute value is the distance between and on the number line, and reading it as a distance turns absolute value problems into plain geometry: asks for the points 5 away from 3, and asks for the points closer than that. This lesson solves both kinds by splitting them into two cases, and ends with the tolerance checks that numeric code relies on. The arithmetic of is in Absolute Value.
Equations: two cases
Since (with ) means is units from 0, must be or :
Three possibilities depending on the right side:
| Equation | Solutions |
|---|---|
| with | two: or |
| one: | |
| with | none, a distance is never negative |
Always isolate the absolute value first: becomes , so or .
When the unknown is on both sides
If the right side contains , it might turn out negative for a candidate, so check each one, just as with radical equations:
from sympy import symbols, Eq, solve, Abs
x = symbols("x", real=True)
print(solve(Eq(Abs(2*x - 3), 7), x))
print(solve(Eq(3*Abs(x + 1) - 4, 8), x))
print(solve(Eq(Abs(x - 1), 2*x + 4), x))
print(solve(Eq(Abs(x + 2), -1), x))[-2, 5]
[-5, 3]
[-1]
[]
The symbol is declared real=True; without it SymPy would also consider complex numbers, where Abs behaves differently.
Inequalities: inside or outside
Think distance again:
| Inequality () | Meaning | Becomes | Shape |
|---|---|---|---|
| closer than to 0 | ("and") | one interval | |
| at most from 0 | one closed interval | ||
| farther than | or | two rays | |
| at least away | or | two rays |
Tip: "Less than" gives a "less" (bounded) answer, a single interval, solved as a compound "and" inequality. "Greater than" gives two pieces, an "or".
from sympy import symbols, solveset, Abs, S
x = symbols("x", real=True)
print(solveset(Abs(x - 4) <= 3, x, S.Reals))
print(solveset(Abs(2*x + 1) > 5, x, S.Reals))
print(solveset(Abs(x) < -2, x, S.Reals)) # never true
print(solveset(Abs(x) >= -2, x, S.Reals)) # always trueInterval(1, 7)
Union(Interval.open(-oo, -3), Interval.open(2, oo))
EmptySet
Reals
Every interval can be written as one absolute value: centre , radius , and then . For : , .
In software: tolerance checks
Floating-point numbers are rarely exactly equal, so numeric code asks whether two values are close: , " lies within of ". This is an absolute value inequality, and it equals the interval .
import math
x = 0.1 + 0.2
print(x == 0.3)
print(abs(x - 0.3) < 1e-9) # |x - a| < eps
print(math.isclose(x, 0.3)) # relative tolerance
target, eps = 100.0, 0.5
for reading in [99.4, 99.6, 100.5, 100.49]:
print(reading, abs(reading - target) < eps)False
True
True
99.4 False
99.6 True
100.5 False
100.49 True
The same inequality appears as a stopping rule in iterative algorithms (stop when ), as a "snap to grid" test in editors (snap when the cursor is within 8 px of a gridline), as a hit test for a 1D slider, and in unit tests (pytest.approx). math.isclose uses a relative tolerance , which works for very large and very small numbers alike.
Exercises
Exercise 1: An equation
Solve .
Answer
or
Solution
Exercise 2: Isolate first
Solve .
Answer
No solution.
Solution
Exercise 3: Inside and outside
Solve (a) and (b) . Give intervals.
Answer
(a) ; (b)
Solution
from sympy import symbols, solveset, Abs, S
x = symbols("x", real=True)
print(solveset(Abs(x + 3) < 4, x, S.Reals))
print(solveset(Abs(5 - x) >= 2, x, S.Reals))Interval.open(-7, 1)
Union(Interval(-oo, 3), Interval(7, oo))
Exercise 4: Interval to absolute value
A valid temperature is . Write this as one absolute value inequality and code it as ok(t).
Answer
Solution
Centre , radius .
def ok(t):
return abs(t - 22) <= 4
print([ok(t) for t in [17.9, 18, 22, 26, 26.1]])[False, True, True, True, False]
Exercise 5: Snap to grid
An editor snaps the cursor to the nearest vertical gridline (lines every 50 px) when . Write snap(x) that returns the gridline if close enough and otherwise. Test 103, 108, 147 and 125.
Answer
100, 108, 150, 125
Solution
The nearest gridline is . For 103 it is 100 (distance 3, snap); 108 is 8 away, no snap; 147 is 3 from 150, snap; 125 is 25 from both, no snap. (Python's round sends 2.5 to the even number 2, so 125 picks 100, but it is too far away either way.)
def snap(x, step=50, radius=6):
nearest = step * round(x / step)
return nearest if abs(x - nearest) <= radius else x
print([snap(x) for x in [103, 108, 147, 125]])[100, 108, 150, 125]
Test yourself
How many solutions does have?
- None
- One
- Two
- Infinitely many
Which is the solution of ?
- or
The check abs(x - a) < 1e-9 is equivalent to:
- or
- exactly