Week 15 · 15.3 · Wed, Nov 11 · The Bigger Picture: ACT-style reading
The Missing Fraction: From a Stock Index to Java's Math
1 This section explains how a computer can lose part of an answer, from a stock exchange in 1982 to the way Java divides numbers today.
A Number That Drifted
2 In January 1982, the Vancouver Stock Exchange in British Columbia, Canada, started a new index, a single number that follows the prices of many stocks at once, and set its starting value at 1000. A computer worked out a new value after every trade on the exchange, about 3,000 times a day, and each new value was built from the one before it.
3 The problem was in how the computer stored each value: it worked out the index to four decimal places, but it kept only three and chopped off the fourth. It did not round to the nearest thousandth; it simply dropped the last digit. Each time, the index lost a tiny piece, never as much as a thousandth of a point, and the next value started from the smaller number.
4 A piece that small would not matter once, but because it was lost after every trade, the losses added up to about one point a day, or about 20 points a month. On Friday, November 25, 1983, the index stood at 524.811. Consultants from Toronto and California had spent three weeks working out the corrections for 22 months of chopped digits. When the exchange opened on Monday morning, the corrected index read 1098.892, up 574.081 points. Before a single new trade, the index had more than doubled.
Whole Numbers and Leftovers
5 Java also chops off part of an answer, and it does so on purpose. Java stores whole numbers in a type called int. When both sides of the division sign, /, are ints, Java performs integer division: it keeps the whole part of the answer and drops the rest. The Java Language Specification says that integer division rounds toward 0. For that reason, 7 / 2 is 3, not 3.5. The remainder operator gives what is left over, so the remainder of 7 divided by 2 is 1. In Java it is written with the percent sign, as in the second line below. Together, division and remainder work like ÷ and rem from the steps language earlier in this unit.
1System.out.println(7 / 2); 2System.out.println(7 % 2);
3 1
6 To keep the fraction, a program needs a double, Java's type for numbers with a decimal point. A number written with a decimal point, such as 2.0, is a double. If either side of a division is a double, Java does the division as doubles, so 7 / 2.0 is 3.5. The kind of number decides the answer, not the box it goes into. In the line double avg = 7 / 2; both numbers are ints, so Java works out 3 first and then stores 3.0. Figure 1 sets the Vancouver index's chopped digits beside both kinds of division.
7 Java also follows an order. Multiplication, division and remainder are worked before addition and subtraction, and operators of the same rank are worked from left to right. Parentheses are worked first. In 7 / 2 * 2, Java divides first: 7 / 2 is 3, and then 3 times 2 is 6, so the half is gone before the multiplication.
Numbers a Computer Cannot Store
8 Even a double cannot store every fraction exactly. Java stores a double in binary, using only the digits 0 and 1, so one tenth is stored as a value very close to 0.1 but not equal to it. When a Java program adds 0.1 and 0.2, it prints a number just above 0.3.
1System.out.println(0.1 + 0.2);
0.30000000000000004
9 Oracle's Java tutorial warns that the double type should never be used for precise values, such as currency.
10 Today's question asks how a computer could lose the half in 7 / 2 and what would keep it. When both numbers are ints, Java's integer division drops the fraction, in the same way that the Vancouver computer chopped off the last digit of the index. Making one side a double, as in 7 / 2.0, keeps the half. On today's bench, each answer is worked out the way Java works it: whole part, leftover, or decimal.
Summary
11 From 1982 to 1983, a digit chopped off after every trade grew into an error of more than 570 points, and the Vancouver index read less than half of its true value. Java chops fractions on purpose: integer division keeps the whole part, the remainder operator gives what is left over, and a double keeps the decimal. Java works multiplication, division and remainder before addition and subtraction, and even a double stores some fractions only approximately.
Sources: Alan Wexelblat, RISKS Digest, vol. 3, no. 41 (1986), reporting the Toronto Star of November 29, 1983; Wikipedia, Vancouver Stock Exchange (citing The Wall Street Journal, November 8, 1983, and the Toronto Star, November 29, 1983); Saarland University, The Software Horror Picture Show; Oracle, The Java Tutorials: Primitive Data Types, Assignment, Arithmetic, and Unary Operators and Operators; Oracle, The Java Language Specification, Java SE 21 Edition, §5.6 and §15.17.
Read the passage on the left. Answer each question by clicking a choice: the right answer turns green, and a wrong one tells you why. Questions point to paragraph numbers, as on the ACT.
1. Main idea
The main purpose of the passage is to:
2. Detail
According to paragraph 3, why did the index lose a tiny piece each time it was worked out?
3. Words in context
As it is used in paragraph 3 ("kept only three and chopped off the fourth"), the phrase chopped off most nearly means:
4. Cause and effect
According to paragraph 4, the error became large enough to matter mainly because:
5. Inference
Based on paragraph 4, what can most reasonably be inferred about the index on Friday, November 25, 1983?
6. Detail
According to paragraph 5, what does Java do when both sides of / are ints?
7. Compare
According to paragraph 6, how is 7 / 2.0 different from 7 / 2?
8. Cause and effect
According to paragraph 7, why is 7 / 2 * 2 equal to 6 in Java and not 7?
9. Author's claim
Which sentence from the passage best answers how a computer could lose the half in 7 / 2?