{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "389c4528",
   "metadata": {},
   "source": [
    "# The number your computer can't hold\n",
    "### Chapter 2 · finite precision · **[WALKTHROUGH]**\n",
    "\n",
    "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/MeiraDavidson/math_module1/blob/main/notebooks/notebook_2_1_finite_precision.ipynb)\n",
    "\n",
    "**What you'll see:** your own computer insist that `0.1 + 0.2` is *not* `0.3` — and\n",
    "then watch it run out of room while trying to hold π and ⅓. Reading about this is a\n",
    "curiosity. Watching your *own machine* do it is unforgettable.\n",
    "\n",
    "Run the cells in order. No libraries, no setup — just press play."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ca2eef88",
   "metadata": {},
   "source": [
    "## 1. The one-line detonation\n",
    "\n",
    "Here is the whole shock in a single line. Run it."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "039660cf",
   "metadata": {},
   "outputs": [],
   "source": [
    "0.1 + 0.2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0f89eced",
   "metadata": {},
   "source": [
    "That trailing `...0004` should not be there. In real arithmetic, $0.1+0.2$ is\n",
    "*exactly* $0.3$. So let's ask the computer the question directly:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8e327611",
   "metadata": {},
   "outputs": [],
   "source": [
    "0.1 + 0.2 == 0.3"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "00ddcda1",
   "metadata": {},
   "source": [
    "**`False`.** The computer is telling you, to your face, that it does not believe\n",
    "$0.1+0.2$ equals $0.3$.\n",
    "\n",
    "It isn't broken. Remember from the chapter: $0.1$ written in *binary* (the\n",
    "computer's native twos) is a **repeating** decimal — it never ends. So the machine\n",
    "can't store $0.1$ exactly, or $0.2$ exactly. It rounds each one to the nearest dot\n",
    "it can hold, and the two tiny roundings don't quite cancel. A speck of error, left\n",
    "over, made visible."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "95762098",
   "metadata": {},
   "source": [
    "## 2. Watch π run out of room\n",
    "\n",
    "Let's print π two ways and see *exactly* where the computer stops. The first line\n",
    "is the value your machine actually stores. The second is the true value of π,\n",
    "copied from someone who computed it the careful way."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "93204861",
   "metadata": {},
   "outputs": [],
   "source": [
    "import math\n",
    "\n",
    "stored = f\"{math.pi:.48f}\"            # what your computer holds, to 48 decimals\n",
    "true_pi = \"3.141592653589793238462643383279502884197169399375\"\n",
    "\n",
    "print(\"computer stores: \", stored)\n",
    "print(\"true value of π: \", true_pi)\n",
    "print()\n",
    "# find the first place where they disagree\n",
    "for i, (a, b) in enumerate(zip(stored, true_pi)):\n",
    "    if a != b:\n",
    "        print(f\"They agree for {i-2} digits after the dot, then the stored value\")\n",
    "        print(\"turns to noise — that's where the computer's 'boxes' ran out.\")\n",
    "        break"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5376242a",
   "metadata": {},
   "source": [
    "The stored value tracks π for about **15–16 digits**, and then everything past\n",
    "that is garbage the machine invented to fill the decimals you asked for. It only\n",
    "ever had room for a sketch."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d70d9856",
   "metadata": {},
   "source": [
    "## 3. Even ⅓ loses its tail\n",
    "\n",
    "It's not just the irrationals. The humble fraction $\\tfrac13 = 0.3333\\ldots$ never\n",
    "ends either, so the computer can't hold it whole:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "29e52fa9",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(f\"1/3 = {1/3:.30f}\")\n",
    "print(\"...the 3's were supposed to go on forever. They stop because the boxes did.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "677ea933",
   "metadata": {},
   "source": [
    "## What just happened\n",
    "\n",
    "> **Our number line is perfect and infinite. The computer's number line is a finite\n",
    "> grid of dots.** Most real numbers — π, ⅓, even plain $0.1$ — fall *between* the\n",
    "> dots, so the computer quietly snaps each one to the nearest dot it can hold. It\n",
    "> isn't lying to you. It's working with a very good **sketch** of the real numbers,\n",
    "> never the real thing.\n",
    "\n",
    "The careful, infinite reasoning you're learning in the book isn't the clumsy\n",
    "version of what a computer does — it's the **exact** version the machine spends all\n",
    "its effort trying to imitate.\n",
    "\n",
    "**Try this:** change `0.1 + 0.2` to `0.5 + 0.25` and run it. Why does *that* one come\n",
    "out exactly right? (Hint: what are $0.5$ and $0.25$ in binary — do *they* end?)"
   ]
  }
 ],
 "metadata": {
  "colab": {
   "provenance": []
  },
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python",
   "version": "3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
