4 · Numbers: int, float & arithmetic
Python's two numeric workhorses — and the float precision trap every ML engineer falls into once.
`7 / 2` is `3.5` in Python 3 — always. Use `//` when you want an integer quotient.
Without this:
Without understanding `/` vs `//`, a single wrong division silently corrupts an index, a count, or a loss calculation.
Python has two everyday numeric types: int (whole numbers, unlimited size) and float (64-bit IEEE 754 decimals). Most arithmetic works the way you'd expect — but division has a gotcha worth knowing before you write your first training loop.
Python (in browser)
Note that `/` always returns a float, even when the result is whole — `4 / 2` is `2.0`, not `2`.
Python runs entirely in your browser via Pyodide (~6 MB on first Run, cached after).
Floats trade range for precision. They cannot represent every decimal exactly — which leads to the most-Googled Python gotcha of all time:
Python (in browser)
0.1 + 0.2 = 0.30000000000000004. IEEE 754 stores fractions in binary — 0.1 has no exact binary representation.
Python runs entirely in your browser via Pyodide (~6 MB on first Run, cached after).
`int()` truncates toward zero — it does not round. Use `round()` when you need standard rounding.
The MML opener anchors what a 'value' is mathematically. If real vs integer numbers feel blurry, read it before continuing.
What is the value of `7 // 2` in Python 3?
- `/` always returns a float; `//` floors to an integer. Mix them up and an array index becomes `3.5` — a crash.
- Float representation errors are real. Use `math.isclose` for equality checks on decimals.
- `int()` truncates; `round()` rounds; `float()` promotes. Know which you need before you cast.
Loss values are floats — comparing `loss == 0.0` instead of `loss < 1e-6` will silently never trigger early stopping. Learning rates like `0.001` must be declared as floats; `learning_rate = 1/1000` is fine in Python 3 but `1//1000` is `0`. Batch indices (`i // batch_size`) use `//` to stay integers.
If you remove it: Type confusion between `int` and `float` produces silent wrong results — an off-by-one in a batch size or a zeroed learning rate that trains forever without converging.