Signed Numbers
Signed numbers carry a direction: positive numbers point one way, negative numbers the other. Temperatures below zero, a bank account in the red, a step to the left, a velocity downward: all of them need a minus sign. This lesson gives the rules for adding, subtracting, multiplying and dividing them, and shows why those rules make sense.
What the sign means
A signed number has a sign ( or ) and a size. The size is its distance from zero, called the absolute value. On the number line, +3 is three steps right of zero, and −3 is three steps left. Numbers like 3 and −3, at the same distance on opposite sides, are opposites. The plus sign is usually left out: 3 means +3.
The minus sign has two jobs that look identical: subtract () and negative/opposite of (, or ). In fact "" means "the opposite of −3", which is 3.
Adding signed numbers
Think of adding as walking on the number line: a positive number is a walk to the right, a negative one a walk to the left.
| Case | Rule | Example |
|---|---|---|
| same signs | add the sizes, keep the sign | |
| different signs | subtract the smaller size from the larger, take the sign of the larger | , |
| opposites | the sum is zero |
A money picture helps: owing 4 dollars and then owing 6 dollars more means owing 10 dollars (). Owing 9 dollars and earning 5 dollars still leaves you owing 4 dollars ().
Subtracting: add the opposite
Every subtraction can be rewritten as an addition: subtracting a number is the same as adding its opposite.
The surprising one is . Taking away a debt makes you richer: if someone cancels a 6-dollar debt you owed, your balance goes up by 6 dollars. On a thermometer, the difference between 4 °C and −6 °C is 10 degrees.
pairs = [(7, 10), (-2, 5), (4, -6), (-8, -3)]
for a, b in pairs:
print(f"{a} - ({b}) = {a - b} same as {a} + ({-b}) = {a + (-b)}")7 - (10) = -3 same as 7 + (-10) = -3
-2 - (5) = -7 same as -2 + (-5) = -7
4 - (-6) = 10 same as 4 + (6) = 10
-8 - (-3) = -5 same as -8 + (3) = -5
Multiplying and dividing
The rule for both is about the signs only:
| Signs | Result | Examples |
|---|---|---|
| and | , | |
| and | , | |
| and (either order) | , |
Short version: same signs give positive, different signs give negative. With many factors, count the negatives: an even number of them gives a positive product, an odd number gives a negative one. .
Why is negative negative positive? Look at a pattern, where each step down subtracts 1 from the second factor:
for b in [3, 2, 1, 0, -1, -2, -3]:
print(f"-3 x {b:2d} = {-3 * b:3d}")-3 x 3 = -9
-3 x 2 = -6
-3 x 1 = -3
-3 x 0 = 0
-3 x -1 = 3
-3 x -2 = 6
-3 x -3 = 9
Each line is 3 more than the one above. For the pattern to keep going past zero, must be 3. Any other rule would break the distributive property: , which forces to be the opposite of , so it is +3.
Signed numbers in code
Python's integers and floats are signed, and all the rules above are built in. Two details are worth knowing: unary minus binds tighter than most things but not tighter than **, and -0.0 exists for floats.
print(-3 ** 2) # means -(3 ** 2)
print((-3) ** 2)
print((-2) ** 3, (-2) ** 4)
print(-12 / -4, 12 / -4)
print(-0.0, -0.0 == 0.0)-9
9
-8 16
3.0 -3.0
-0.0 True
-3 ** 2 being −9 catches many people out; it matches the mathematical convention that means . Write (-3) ** 2 when you mean the square of −3.
In software
- Coordinates: in maths the axis points up, but on screens (HTML canvas, most 2D game engines) grows downward. Jumping means a negative velocity there.
- Velocities and forces in physics code are signed: is gravity pulling down.
- Money: a refund is a negative charge; balances can go below zero.
- Signed integer types store the sign in the top bit (two's complement, see Integer Overflow and Two's Complement). An 8-bit signed number holds −128 to 127.
Exercises
Exercise 1: Add and subtract
Compute (a) (b) (c) (d)
Answer
(a) 5 (b) −15 (c) −7 (d) 7
Solution
Exercise 2: Multiply and divide
Compute (a) (b) (c) (d)
Answer
(a) −42 (b) 40 (c) −7 (d) −30
Solution
(a) different signs → negative. (b) same signs → positive. (c) different signs → negative. (d) three negatives (odd) → negative, and .
print((-7) * 6, (-8) * (-5), 56 // -8, (-2) * (-3) * (-5))-42 40 -7 -30
Exercise 3: Temperature swing
At 6 a.m. it was −11 °C; by 2 p.m. it was 4 °C. By how much did the temperature rise? If it then fell 9 degrees, what was the new temperature?
Answer
It rose 15 degrees; then it was −5 °C.
Solution
Exercise 4: Sign of a product
Without multiplying, decide the sign of . Then check with math.prod.
Answer
Positive (four negative factors); the product is 5 040.
Solution
Count the negatives: −1, −3, −5, −7 is four, an even number, so the product is positive. Its size is .
import math
print(math.prod([-1, 2, -3, 4, -5, 6, -7]))5040
Test yourself
What is ?
- 3
- −13
- −3
- 13
What does Python print for -4 ** 2?
- 16
- -16
- 8
- -8
The product of five negative numbers is…
- always positive
- always negative
- zero
- positive or negative, depending on the numbers