TExcelTAD
Home › Basic Math & Pre-Algebra › Absolute Value

Absolute Value

The absolute value of a number is its distance from zero, ignoring direction. Both 6 and −6 are 6 steps from zero, so both have absolute value 6. It is a small idea with a big job: whenever a program asks "how far?" or "how big is the error?" rather than "which way?", absolute value is involved.

Definition

The absolute value of xx is written ∣x∣\lvert x \rvert (vertical bars on both sides):

∣x∣=xif x≥0∣x∣=−xif x<0\begin{aligned} \lvert x \rvert &= x && \text{if } x \ge 0 \\ \lvert x \rvert &= -x && \text{if } x < 0 \end{aligned}

The second line looks odd at first: how can −x-x be positive? Because when xx is negative, −x-x is its opposite, which is positive. For x=−6x = -6: ∣−6∣=−(−6)=6\lvert -6 \rvert = -(-6) = 6.

xx 9 0 −2.5 −100 34\frac{3}{4}
∣x∣\lvert x \rvert 9 0 2.5 100 34\frac{3}{4}

Absolute value is never negative, and it is 0 only for 0 itself.

In Python it is the built-in function abs, which works for ints, floats and fractions:

python · try it yourself
from fractions import Fraction

for x in [9, 0, -2.5, -100, Fraction(-3, 4)]:
    print(f"abs({x}) = {abs(x)}")

def my_abs(x):            # the definition, written out
    return x if x >= 0 else -x
print(my_abs(-6), my_abs(6))
abs(9) = 9
abs(0) = 0
abs(-2.5) = 2.5
abs(-100) = 100
abs(-3/4) = 3/4
6 6

Distance between two numbers

The distance between aa and bb on the number line is ∣a−b∣\lvert a - b \rvert. It doesn't matter which you subtract from which, because ∣a−b∣=∣b−a∣\lvert a - b \rvert = \lvert b - a \rvert.

distance from −3 to 5:∣−3−5∣=∣−8∣=8or∣5−(−3)∣=∣8∣=8\text{distance from } {-3} \text{ to } 5{:}\quad \lvert -3 - 5 \rvert = \lvert -8 \rvert = 8 \quad\text{or}\quad \lvert 5 - (-3) \rvert = \lvert 8 \rvert = 8

This is the most common use of absolute value in practice. "Is xx within 0.5 of 10?" becomes ∣x−10∣≤0.5\lvert x - 10 \rvert \le 0.5, which is the same as 9.5≤x≤10.59.5 \le x \le 10.5.

Rules worth knowing

Rule Example
∣a×b∣=∣a∣×∣b∣\lvert a \times b \rvert = \lvert a \rvert \times \lvert b \rvert ∣(−3)×4∣=12=3×4\lvert (-3) \times 4 \rvert = 12 = 3 \times 4
∣a÷b∣=∣a∣÷∣b∣\lvert a \div b \rvert = \lvert a \rvert \div \lvert b \rvert ∣−20÷5∣=4\lvert -20 \div 5 \rvert = 4
∣−a∣=∣a∣\lvert -a \rvert = \lvert a \rvert ∣−7∣=∣7∣\lvert -7 \rvert = \lvert 7 \rvert
∣a+b∣≤∣a∣+∣b∣\lvert a + b \rvert \le \lvert a \rvert + \lvert b \rvert (triangle inequality) ∣5+(−3)∣=2≤8\lvert 5 + (-3) \rvert = 2 \le 8

Watch out: absolute value does not split over addition. ∣5+(−3)∣=2\lvert 5 + (-3) \rvert = 2, but ∣5∣+∣−3∣=8\lvert 5 \rvert + \lvert -3 \rvert = 8. The two are equal only when aa and bb have the same sign (or one is zero). The triangle inequality says the sum's size can never be more than the sizes added: going 5 steps right and 3 steps left can't take you further than 8 steps from where you started.

python · try it yourself
import random

rng = random.Random(3)
always_ok = True
for _ in range(1000):
    a, b = rng.randint(-50, 50), rng.randint(-50, 50)
    if abs(a * b) != abs(a) * abs(b) or abs(a + b) > abs(a) + abs(b):
        always_ok = False
print("rules held in 1000 random tests:", always_ok)
print(abs(5 + -3), abs(5) + abs(-3))
rules held in 1000 random tests: True
2 8

Solving ∣x∣=\lvert x \rvert = number

∣x∣=4\lvert x \rvert = 4 has two solutions, x=4x = 4 and x=−4x = -4, the two points at distance 4 from zero. ∣x∣=0\lvert x \rvert = 0 has one solution (0), and ∣x∣=−4\lvert x \rvert = -4 has none, because a distance can't be negative. Similarly ∣x−10∣=3\lvert x - 10 \rvert = 3 means "xx is 3 away from 10": x=7x = 7 or x=13x = 13.

In software

  • Comparing floats: because floats carry tiny rounding errors, code checks abs(a - b) < tolerance instead of a == b. Python's math.isclose does this for you.
  • Errors in machine learning: the mean absolute error averages ∣prediction−truth∣\lvert \text{prediction} - \text{truth} \rvert over many examples; it tells you how far off a model is, typically.
  • Grid distance: on a city-block or tile map, the number of moves from (x1,y1)(x_1, y_1) to (x2,y2)(x_2, y_2) without diagonals is ∣x1−x2∣+∣y1−y2∣\lvert x_1 - x_2 \rvert + \lvert y_1 - y_2 \rvert, the Manhattan distance, used by path-finding in games.
  • Dead zones: a game ignores joystick input when ∣stick∣<0.15\lvert \text{stick} \rvert < 0.15 so a slightly worn stick doesn't make the character drift.
python · try it yourself
import math

print(0.1 + 0.2 == 0.3, abs((0.1 + 0.2) - 0.3) < 1e-9, math.isclose(0.1 + 0.2, 0.3))

predictions = [12, 15, 9, 20]
truth = [10, 15, 11, 25]
errors = [abs(p - t) for p, t in zip(predictions, truth)]
print("errors:", errors, " mean absolute error:", sum(errors) / len(errors))

player, goal = (2, 7), (6, 4)
print("tile moves:", abs(player[0] - goal[0]) + abs(player[1] - goal[1]))
False True True
errors: [2, 0, 2, 5]  mean absolute error: 2.25
tile moves: 7

Exercises

Exercise 1: Evaluate

Compute (a) ∣−15∣\lvert -15 \rvert (b) ∣8−20∣\lvert 8 - 20 \rvert (c) −∣−3∣-\lvert -3 \rvert (d) ∣−4∣×∣2−7∣\lvert -4 \rvert \times \lvert 2 - 7 \rvert

Answer

(a) 15 (b) 12 (c) −3 (d) 20

Solution
(a)∣−15∣=15(b)∣8−20∣=∣−12∣=12(c)∣−3∣=3, then the minus outside makes it −3(d)∣−4∣=4 and ∣2−7∣=∣−5∣=5, so 4×5=20\begin{aligned} &\text{(a)}\quad \lvert -15 \rvert = 15 \\ &\text{(b)}\quad \lvert 8 - 20 \rvert = \lvert -12 \rvert = 12 \\ &\text{(c)}\quad \lvert -3 \rvert = 3,\ \text{then the minus outside makes it } {-3} \\ &\text{(d)}\quad \lvert -4 \rvert = 4 \text{ and } \lvert 2 - 7 \rvert = \lvert -5 \rvert = 5,\ \text{so } 4 \times 5 = 20 \end{aligned}

Exercise 2: Solve

Find every xx with (a) ∣x∣=9\lvert x \rvert = 9 (b) ∣x−2∣=5\lvert x - 2 \rvert = 5 (c) ∣x+1∣=−3\lvert x + 1 \rvert = -3

Answer

(a) 9 or −9 (b) 7 or −3 (c) no solution

Solution

(a) the two points 9 steps from 0. (b) "xx is 5 away from 2": 2+5=72 + 5 = 7 or 2−5=−32 - 5 = -3. (c) an absolute value can never be negative.

python · try it yourself
print([x for x in range(-20, 21) if abs(x - 2) == 5])
[-3, 7]

Exercise 3: Within tolerance

A bolt should be 40 mm long, with a tolerance of 0.2 mm. Which of these lengths pass: 39.85, 40.25, 40.2, 39.7? Write the test as one absolute-value inequality.

Answer

∣length−40∣≤0.2\lvert \text{length} - 40 \rvert \le 0.2. 39.85 and 40.2 pass; 40.25 and 39.7 fail.

Solution

∣39.85−40∣=0.15\lvert 39.85 - 40 \rvert = 0.15 ✓, ∣40.25−40∣=0.25\lvert 40.25 - 40 \rvert = 0.25 ✗, ∣40.2−40∣=0.2\lvert 40.2 - 40 \rvert = 0.2 ✓ (equal is allowed), ∣39.7−40∣=0.3\lvert 39.7 - 40 \rvert = 0.3 ✗. In code, add a tiny allowance so the float rounding of 40.2−4040.2 - 40 doesn't wrongly fail the edge case:

python · try it yourself
for length in [39.85, 40.25, 40.2, 39.7]:
    print(length, abs(length - 40) <= 0.2 + 1e-9)
39.85 True
40.25 False
40.2 True
39.7 False

Exercise 4: Closest to target

Write code that finds which number in [17, -4, 31, 22, 9] is closest to 20.

Answer

22 (distance 2)

Solution

"Closest" means smallest ∣x−20∣\lvert x - 20 \rvert. Python's min accepts a key that says what to compare:

python · try it yourself
nums = [17, -4, 31, 22, 9]
print([abs(x - 20) for x in nums])
print(min(nums, key=lambda x: abs(x - 20)))
[3, 24, 11, 2, 11]
22

Test yourself

What is ∣3−11∣\lvert 3 - 11 \rvert?

  1. −8
  2. 8
  3. 14
  4. −14

How many solutions does ∣x∣=7\lvert x \rvert = 7 have?

  1. None
  2. One
  3. Two
  4. Infinitely many

Which is always true for any numbers aa and bb?

  1. ∣a+b∣=∣a∣+∣b∣\lvert a + b \rvert = \lvert a \rvert + \lvert b \rvert
  2. ∣a+b∣≤∣a∣+∣b∣\lvert a + b \rvert \le \lvert a \rvert + \lvert b \rvert
  3. ∣a−b∣=∣a∣−∣b∣\lvert a - b \rvert = \lvert a \rvert - \lvert b \rvert
  4. ∣a∣=a\lvert a \rvert = a