Properties of Operations
A handful of rules describe how addition and multiplication behave: you can swap the order, regroup, and split a product over a sum. These properties are why mental-math tricks work, why algebra is allowed to rearrange equations, and, surprisingly, why some computer code gives slightly different answers when it adds the same numbers in a different order.
The properties at a glance
| Property | Addition | Multiplication |
|---|---|---|
| Commutative (order doesn't matter) | ||
| Associative (grouping doesn't matter) | ||
| Identity (a number that changes nothing) | ||
| Inverse (a number that gets you back to the identity) | , for | |
| Distributive (connects the two) | ||
| Zero property |
The letters , , stand for any numbers. That is the whole point: a property is a promise that holds for every number you could put in.
Commutative and associative: reorder and regroup
Because of these two properties, a long sum or product can be computed in whatever order is easiest:
Subtraction and division have neither property: and . That is one reason mathematicians like to rewrite subtraction as adding a negative () and division as multiplying by a reciprocal (): then the friendly properties apply again.
a, b, c = 6, 15, 4
print("commutative +:", a + b == b + a, " x:", a * b == b * a)
print("associative +:", (a + b) + c == a + (b + c), " x:", (a * b) * c == a * (b * c))
print("subtraction commutative?", a - b == b - a, (a - b, b - a))
print("division associative?", (48 / 4) / 2 == 48 / (4 / 2), ((48 / 4) / 2, 48 / (4 / 2)))commutative +: True x: True
associative +: True x: True
subtraction commutative? False (-9, 9)
division associative? False (6.0, 24.0)
One example that works does not prove a property, but one example that fails is enough to disprove it. The last two lines are such counterexamples.
The distributive property
Multiplying a sum is the same as multiplying each part and adding:
You already used it in Multiplication to split into . It also works backwards, which is called factoring out:
And it works with subtraction: .
Here is a check over many random numbers. It cannot prove the property (nothing finite can), but it builds confidence and would catch a mistaken rule fast:
import random
rng = random.Random(1)
ok = True
for _ in range(1000):
a, b, c = (rng.randint(-100, 100) for _ in range(3))
if a * (b + c) != a * b + a * c:
ok = False
print("distributive holds in 1000 random tests:", ok)
# a rule that is FALSE: does a + (b * c) equal (a + b) * (a + c)?
a, b, c = 2, 3, 4
print(a + b * c, (a + b) * (a + c))distributive holds in 1000 random tests: True
14 30
Addition does not distribute over multiplication, and one counterexample shows it.
Identities and inverses
- Adding 0 or multiplying by 1 changes nothing. That is why a running total starts at
0and a running product starts at1in code. - The additive inverse of 7 is −7 (they add to 0). The multiplicative inverse or reciprocal of 7 is (they multiply to 1). Zero has no reciprocal, which is another way of saying you can't divide by zero.
In software: when the properties break
Python's integers and fractions obey every property exactly. Floats do not quite, because each operation rounds its result to about 16 significant digits:
x = (0.1 + 0.2) + 0.3
y = 0.1 + (0.2 + 0.3)
print(x, y, x == y)
big = 1e16
print((big + 1) - big, (big - big) + 1)0.6000000000000001 0.6 False
0.0 1.0
Float addition is commutative but not associative. This matters in real programs:
- Parallel and GPU code adds numbers in a different order on each run (whichever thread finishes first), so a sum over millions of values can differ in its last digits between runs. Machine learning frameworks document this as "non-determinism".
- Compilers are not allowed to regroup float arithmetic unless you allow it with flags like
-ffast-math, precisely because it changes results. - Strings show that "+" need not be commutative at all:
"ab" + "cd"is"abcd"but"cd" + "ab"is"cdab".
The Floating Point lesson explains the rounding behind this.
Exercises
Exercise 1: Name the property
Which property justifies each step? (a) (b) (c) (d)
Answer
(a) commutative (addition) (b) associative (multiplication) (c) distributive (d) identity (multiplication)
Solution
(a) only the order changed; (b) only the brackets moved; (c) the 8 was multiplied into each part of the sum: and ; (d) 1 is the multiplicative identity.
Exercise 2: Mental math with properties
Compute and in your head, naming the properties you use.
Answer
3 700 and 1 188
Solution
Exercise 3: Factor out
Compute by factoring out.
Answer
2 300
Solution
print(23 * 46 + 23 * 54, 23 * (46 + 54))2300 2300
Exercise 4: Hunt for a counterexample
Is division distributive over addition from the left, that is, does ? Find a counterexample with small whole numbers, then check whether holds.
Answer
No: , but . The other direction does hold: .
Solution
One failing example disproves the first rule. The second rule is the distributive property in disguise, because .
from fractions import Fraction as F
a, b, c = 12, 2, 4
print(F(a, b + c), F(a, b) + F(a, c))
print(F(b + c, a), F(b, a) + F(c, a))2 9
1/2 1/2
Test yourself
Which operation is commutative?
- Subtraction
- Division
- Multiplication
- None of them
uses which property?
- Associative
- Distributive
- Identity
- Commutative
Why can (a + b) + c and a + (b + c) give different results with Python floats?
- Python has a bug
- Each float addition rounds, so the grouping changes which roundings happen
- Floats are not commutative
- Floats cannot be added