Arithmetic and NumbersDeeper

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 2n, 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 a−b=a+(−b); the laws of arithmetic force (−a)(−b)=ab
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; −2n−1 has no opposite; languages disagree on negative remainders
Related ideas Subtraction (ch03), the division algorithm (ch04), remainders modulo m (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 x+5=3 has no answer without −2, 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 (−1)×(−1) were −1?

Reason it through, then open

Then (−1)(1+(−1)) would be (−1)+(−1)=−2 by distributivity, but also (−1)×0=0. So 0=−2, 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 +5 and −3 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, +0 and −0, 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) n-bit signed integers (computers)
Range Unbounded in both directions −2n−1 to 2n−1−1
Every number has an opposite Yes All but −2n−1
a+1>a Always Fails at 2n−1−1, which wraps to −2n−1
Laws of arithmetic All hold +, −, × hold modulo 2n; 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.

  1. With n bits, store an integer x in range (−2n−1≤x<2n−1) as c(x)=xmod2n, the floored remainder, which is between 0 and 2n−1.
  2. The hardware adds two codes as unsigned numbers and keeps n bits: it computes (c(x)+c(y))mod2n.
  3. Remainders add (chapter 5): (c(x)+c(y))mod2n =(x+y)mod2n =c(x+y), provided x+y is itself in range, so that its code is defined by step 1.
  4. So the stored result is exactly the code of the true sum, and reading it as signed gives x+y. No step looked at signs.
  5. If x+y is out of range, the hardware still produces (x+y)mod2n, which reads as x+y±2n: 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
(−a)(−b)=ab 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.