Kayavak / Arithmetic and Numbers / AN·09 Negative Numbers / Deeper
Deeper · Arithmetic and Numbers
Negative Numbers
The questions the chapter doesn't stop for: why this exists, the model to keep in your head, what changes when an assumption does, how sure we are of each claim, and what's still open. Read it after the chapter; back to the chapter.
Why does this exist?
Negative numbers were used long before they were accepted. Chinese calculators of the Han dynasty set out equations on a counting board with red and black rods for gains and losses, and the procedure in The Nine Chapters for eliminating unknowns needs them. Brahmagupta's Brāhmasphuṭasiddhānta (628) gives complete rules for adding, subtracting, multiplying and dividing "fortunes", "debts" and zero, the first written treatment of negatives as numbers with an arithmetic of their own. Arabic algebra, which reached Europe, mostly avoided them by rearranging equations so every term was positive.
European mathematicians met negatives as solutions of equations and distrusted them for three centuries: Cardano (1545) used them while calling them "fictitious", Descartes called negative roots "false", and as late as 1831 the English mathematician Augustus De Morgan argued that a negative answer showed the question had been badly posed. What settled the matter was a change of view, from numbers as quantities to numbers as things that obey rules: George Peacock's Treatise on Algebra (1830) and later nineteenth-century work defined the integers by their laws, after which "why is a negative times a negative positive?" had the answer this chapter gives. Computers then reopened the question of how to write negatives, and settled on two's complement by the 1960s.
The mental model
Try it yourself first: write your own model in the Mental Model Explorer, then compare.
A negative number is the opposite of a positive one: the number that adds to it to make zero. Every sign rule follows from keeping chapter 1's laws, especially distributivity. Machine integers are remainders modulo , and two's complement is the choice of reading half of them as negatives.
| Question | Answer |
|---|---|
| What is it? | The integers: whole numbers and their opposites, with order extended to the left of 0 |
| Problem it solves | Subtraction that always has an answer; quantities with a direction (debt, below zero, behind) |
| Parts | Opposite, absolute value, sign rules, order, division conventions, two's complement |
| How it works | ; the laws of arithmetic force |
| Assumptions | Commutative, associative and distributive laws; for machines, a fixed number of bits |
| When they change | Fixed width wraps; unsigned types have no negatives; floats add a negative zero |
| Where it fails | Overflow; has no opposite; languages disagree on negative remainders |
| Related ideas | Subtraction (ch03), the division algorithm (ch04), remainders modulo (ch05), binary (ch02) |
What if?
What if we had kept separate columns instead of negative numbers?
Reason it through, then open
Accounting did exactly that for centuries, and double-entry bookkeeping still does: debits and credits are both recorded as positive numbers in different columns. It works well for ledgers, and it builds in a check (the columns must balance). But algebra needs one kind of number: solving has no answer without , and every formula would split into cases according to which quantities happen to be "losses". Negative numbers let one rule cover every case, at the cost of having to keep track of signs.
What if were ?
Reason it through, then open
Then would be by distributivity, but also . So , and from that every number equals every other. You could keep the rule only by giving up distributivity, and with it the methods of chapters 3 and 4 (long multiplication is distributivity used over and over). There are number systems where some familiar laws fail (matrices aren't commutative, as chapter 4's Deeper page notes), but none keeps distributivity and this sign rule together.
What if computers stored negatives in sign and magnitude?
Reason it through, then open
Some early machines did (and floating-point numbers still do, with a separate sign bit). Adding and would need the hardware to compare sizes, subtract the smaller from the larger and pick the sign, a different circuit from adding two positives. There would be two zeros, and , which must compare equal. Two's complement avoids all of that: one adder, one zero, subtraction as adding the negation. The price is the lopsided range and the one value with no opposite; designers judged that a good trade, and almost every processor since the 1960s agrees.
Where the boundary is
| Aspect | Integers (mathematics) | -bit signed integers (computers) |
|---|---|---|
| Range | Unbounded in both directions | to |
| Every number has an opposite | Yes | All but |
| Always | Fails at , which wraps to | |
| Laws of arithmetic | All hold | , , hold modulo ; order doesn't survive the wrap |
| Division of negatives | One convention chosen by the writer | Fixed by the language (usually truncated) |
| What overflow does | Nothing; there is none | Wraps, saturates, traps or is undefined, by language |
Derive it
Why two's complement addition gives the right signed answer whenever that answer fits.
- With bits, store an integer in range () as , the floored remainder, which is between 0 and .
- The hardware adds two codes as unsigned numbers and keeps bits: it computes .
- Remainders add (chapter 5): , provided is itself in range, so that its code is defined by step 1.
- So the stored result is exactly the code of the true sum, and reading it as signed gives . No step looked at signs.
- If is out of range, the hardware still produces , which reads as : that's overflow, and it happens exactly when two numbers of the same sign give a result of the other sign, which is how processors detect it.
How sure are we?
| Claim | How it's known | Where from |
|---|---|---|
| for all integers | Theorem | From distributivity; the chapter's proof |
| Opposites are unique | Theorem | The chapter's one-line argument |
| Multiplying an inequality by a negative reverses it | Theorem | From the definition of order and the sign rules |
| Two's complement addition is correct whenever the result fits | Theorem | The derivation above |
| Python floors, C, Java and JavaScript truncate | Convention | Each language's specification; measured in the lab |
| Nearly all current processors use two's complement | Historical | Since the 1960s; C++20 and C23 now require it for signed integers |
| Brahmagupta wrote rules for negative numbers in 628 | Historical | The Brāhmasphuṭasiddhānta |
| Integer overflow is a common source of security bugs | Measured | Integer overflow (CWE-190) regularly appears in MITRE's yearly list of the most dangerous software weaknesses |
Still open
Nothing about the integers themselves is in doubt. What remains argued about is how programming languages should treat overflow: wrap silently (fast, and what hardware does), trap (safe, but costs checks), saturate, or make it undefined (as C does for signed integers, which lets compilers optimise but turns overflow into unpredictable behaviour). Newer languages split the difference, Rust for example trapping in debug builds and wrapping in release builds unless told otherwise, and the trade-off is still debated.
What would change your mind? The chapter says the sign rules are forced, not chosen. That holds as long as distributivity is required; it would stop holding only in a system where you deliberately give distributivity up, and the question would then be what such a system is for, not whether the integers are wrong.
Recall
What is the opposite of a number?
The number that adds to it to make zero: a + (−a) = 0.
Why is a negative times a negative positive?
Distributivity forces it: (−a)(b + (−b)) = 0 makes (−a)(−b) the opposite of (−a)b = −ab.
What happens to an inequality when both sides are multiplied by a negative number?
It reverses: 2 < 5 but −2 > −5.
What is −17 mod 5 in Python, and in JavaScript?
3 in Python (floored), −2 in JavaScript (truncated).
How is −x stored in n-bit two's complement?
As 2ⁿ − x; flip every bit of x and add 1.
Which n-bit signed integer has no opposite?
−2ⁿ⁻¹, such as −128 in 8 bits.
Reflect
Be honest with yourself; this stays in your browser. Anything you can't tick yet is what to revisit.