Premier League Predictor: FastAPI & Redis — Chapter 7, Exercise 2 ================================================================= TASK Compute the composite score by hand for two teams that collide when goal difference reaches +500 (3 points versus 4 points at -500) and when goals scored reaches 1,000. Report the real numbers, then propose a wider packing that removes both collisions and state what its new limits are. SOLUTION Chapter packing: points*1_000_000 + (gd+500)*1_000 + gf A: 3 points, GD +500, GF 0 = 3_000_000 + 1000*1000 + 0 = 4000000 B: 4 points, GD -500, GF 0 = 4_000_000 + 0*1000 + 0 = 4000000 -> identical, so the table treats them as tied A: 3 points, GD 0, GF 1000 = 3_000_000 + 500*1000 + 1000 = 3501000 B: 3 points, GD +1, GF 0 = 3_000_000 + 501*1000 + 0 = 3501000 -> identical In both cases one field's value has reached the size of the field above it and carries into it. Wider packing: composite = points*10**12 + (gd + 10**4)*10**6 + gf A (3 points, GD +500, GF 0) = 3010500000000 B (4 points, GD -500, GF 0) = 4009500000000 different A (3 points, GD 0, GF 1000) = 3010000001000 B (3 points, GD +1, GF 0) = 3010001000000 different New limits: goal difference within +/-9,999; goals scored below 1,000,000; points bounded only by the total below. The largest realistic composite (114 points, GD +100, GF 120) is 114010100000120, about 1.1e14, well below 2**53 = 9007199254740992, so the double that stores the score still holds it exactly. The real ceiling is 2**53: past it, a double cannot represent every integer, and two different composites could round to the same score. Widening the packing moves the collision limits out but must stay under that ceiling. WHY THIS WORKS AS AN ANSWER ---------------------------- It shows the collision with the actual numbers, explains it as carry between packed fields, and checks the fix against both the collisions and the 2**53 exactness limit rather than only widening the fields.