{"course":{"slug":"maths-you-need","title":"The Maths You Actually Need","url":"https://addaly.com/learn/maths-you-need","level":"intermediate","track":"deep"},"lesson":{"slug":"floating-point-anatomy","title":"What a float32 actually stores, and where its seven digits run out","url":"https://addaly.com/learn/maths-you-need/floating-point-anatomy","position":67,"of":76,"minutes":10,"answer":"A float32 is a sign, an 8-bit exponent and a 24-bit mantissa, giving about seven decimal digits and a range near 10^±38, so its spacing grows with the number, an integer counter stalls at 2^24.","keyIdea":"A float32 is a sign, an 8-bit exponent and a 24-bit mantissa, giving about seven decimal digits and a range near 10^±38, so its spacing grows with the number, an integer counter stalls at 2^24, and equality between computed floats is an accident of rounding rather than a fact."},"body":{"format":"markdown","text":"## Thirty-two bits, three fields\n\nA float32 is scientific notation in binary. Its 32 bits are split into a sign (1 bit), an exponent (8 bits) and a fraction called the mantissa (23 bits):\n\n```\nvalue = (−1)^sign × 1.mantissa × 2^(exponent − 127)\n```\n\nThe leading 1 is not stored, because in binary the first significant digit is always 1, so the mantissa carries 24 bits of precision for the price of 23. Everything about how floats behave follows from those three field widths.\n\n## Precision: about seven digits\n\nTwenty-four binary digits is `24 × log10(2) = 7.2` decimal digits. The gap between 1 and the next representable number is `2^-23 = 1.19 × 10^-7`, called machine epsilon. Any number is stored to a relative accuracy of about half that, six parts in a hundred million. The absolute gap between neighbouring floats grows with the number: near 1 it is `10^-7`, near 1,000 it is `10^-4`, near a million it is `0.06`, and past `2^24 = 16,777,216` it is 2.\n\n> Figure: The 32 bits of a float32, and what each field costs you. Layers, top to bottom: Sign, 1 bit (positive or negative); Exponent, 8 bits (range about ten to the plus or minus 38; exp overflows at 88.7); Mantissa, 23 bits stored and 24 used (7.2 decimal digits: the gap at 1 is 0.00000012, at 16.8 million it is 2). Every surprise in this module comes from those three widths. Seven digits is why a variance computed by subtraction fails; the growing gap is why a float32 counter stalls at 16,777,216 and why adding 1 to a hundred million does nothing at all.\n\nThat last fact bites. A float32 counter incremented by 1 stops at 16,777,216, because `16,777,217` is not representable and the addition rounds back down:\n\n```python\nimport numpy as np\nx = np.float32(16_777_216)\nprint(x + np.float32(1))       # 16777216.0\n```\n\nA running total of tokens, steps or samples kept in float32 silently stalls at seventeen million. Keep counters as integers.\n\n## Range: about 10^±38\n\nEight exponent bits give exponents from −126 to +127, so the largest float32 is about `3.4 × 10^38` and the smallest normal one about `1.2 × 10^-38`. Below that, the format gives up precision to reach a little further, down to `1.4 × 10^-45`, after which the number is zero. Module 5 used the top of that range: `exp(x)` overflows to infinity at `x = 88.7`. The bottom matters too. A probability of `10^-40` is representable only as a denormal with a few bits of precision, and the product of two probabilities of `10^-25` each is exactly zero. This is the concrete reason everything probabilistic is done in log space.\n\n## Why 0.1 + 0.2 is famous, and why the format matters\n\nOne tenth is not representable in binary, any more than one third is in decimal. Stored as float64, `0.1` is `0.1000000000000000055`, and\n\n```python\n0.1 + 0.2 == 0.3          # False in float64: 0.30000000000000004\n```\n\nIn float32 both sides happen to round to the same number, `0.300000012`, and the comparison is True. The lesson is not about 0.3. It is that equality between floats is an accident of rounding, and a test like `if loss == 0.0` or `assert a == b` on computed values will pass or fail depending on the format, the order of operations, and the hardware. Compare with a tolerance:\n\n```python\nnp.isclose(a, b, rtol=1e-5, atol=1e-8)\n```\n\nand choose the tolerances from the format: `rtol` near `10^-5` for float32, `10^-12` for float64.\n\n## The gap you should picture\n\nRepresentable floats are not evenly spaced. They are dense near zero and sparse far from it, with the same *number* of floats between 1 and 2 as between 1,024 and 2,048. This has one consequence you will meet constantly: adding a small number to a large one loses the small one. `10^8 + 1` in float32 is `10^8`, because the gap between floats near `10^8` is 8. The next lesson but one is about the damage that does.\n\n## Doubles, and when to use them\n\nFloat64 has 11 exponent bits and 52 mantissa bits: about 16 digits and a range of `10^±308`. NumPy and Python use it by default; deep-learning frameworks use float32 by default because it halves memory and the arithmetic units are faster. The right rule: float32 for the model, float64 for anything that accumulates, checks, or is compared. A loss averaged over a million batches, a gradient check, a statistic computed by subtraction: float64. A weight matrix: float32 or smaller, and the next lesson says how much smaller.\n\n## Reading the bits yourself\n\n```python\nimport struct\nbits = struct.unpack(\">I\", struct.pack(\">f\", 0.1))[0]\nprint(f\"{bits:032b}\")     # 0 01111011 10011001100110011001101\n```\n\nSign 0, exponent `01111011 = 123`, so `2^(123 − 127) = 2^-4 = 1/16`, mantissa `1.6000000238...`, and `1.6 / 16 = 0.1000000015`. The repeating `1001` in the mantissa is the binary expansion of one tenth being cut off at 23 bits, and that cut is where the `0.0000000015` came from. Once you have seen it, every rounding surprise in the rest of this module has an address."},"check":{"question":"A training script keeps a running count of processed tokens in a float32 tensor. After a long run the count is stuck at 16,777,216 though tokens are still arriving. What is happening?","options":[{"text":"The tensor has overflowed its maximum value and wrapped around, like an integer would.","correct":false,"tempting":"Float32 reaches 3.4 × 10^38 before overflow, and overflow gives infinity rather than a wrap; the stall is a precision limit, not a range limit."},{"text":"Float32 cannot represent integers above about seven digits at all.","correct":false,"tempting":"It represents every integer up to 2²⁴ exactly and many beyond; what it loses above that point is the odd ones, not integers as such."},{"text":"The data loader has stopped delivering tokens and the count is correct.","correct":false,"tempting":"The suspicious value is exactly 2²⁴, which is the number at which float32 loses the ability to represent odd integers; that coincidence is the diagnosis."},{"text":"Above 2²⁴ the float spacing is 2, so adding 1 rounds away.","correct":true}]},"prev":{"slug":"fermi-estimates-for-ml","title":"Estimating anything to within a factor of three","url":"https://addaly.com/learn/maths-you-need/fermi-estimates-for-ml"},"next":{"slug":"half-precision-and-mixed-precision","title":"bf16, fp16, and why training keeps a float32 copy","url":"https://addaly.com/learn/maths-you-need/half-precision-and-mixed-precision"},"authorship":"Founder and series editor: Dr Mustafa Mun. Written by AI models to a written standard. Source and assessment review continues. Academic review invited.","review":{"status":"Draft, open for academic review"},"cite":"Addaly (2026) 'What a float32 actually stores, and where its seven digits run out', in The Maths You Actually Need. Edited by M. Mun. Addaly. Available at: https://addaly.com/learn/maths-you-need/floating-point-anatomy (Accessed: 29 November 2026).","licence":{"name":"CC BY-NC-SA 4.0","url":"https://creativecommons.org/licenses/by-nc-sa/4.0/"},"reader":{"schema":1,"title":"What a float32 actually stores, and where its seven digits run out","courseTitle":"The Maths You Actually Need","module":{"index":8,"slug":"numbers-in-a-machine","title":"Numbers inside a machine"},"answer":null,"keyIdea":"A float32 is a sign, an 8-bit exponent and a 24-bit mantissa, giving about seven decimal digits and a range near 10^±38, so its spacing grows with the number, an integer counter stalls at 2^24, and equality between computed floats is an accident of rounding rather than a fact.","hero":{"kind":"figure","figure":{"t":"figure","label":"Figure 8.1","figure":{"caption":"Every surprise in this module comes from those three widths. Seven digits is why a variance computed by subtraction fails; the growing gap is why a float32 counter stalls at 16,777,216 and why adding 1 to a hundred million does nothing at all.","layers":[{"label":"Sign, 1 bit","detail":"positive or negative"},{"label":"Exponent, 8 bits","detail":"range about ten to the plus or minus 38; exp overflows at 88.7"},{"label":"Mantissa, 23 bits stored and 24 used","detail":"7.2 decimal digits: the gap at 1 is 0.00000012, at 16.8 million it is 2"}],"title":"The 32 bits of a float32, and what each field costs you","type":"stack"},"alt":"Figure: The 32 bits of a float32, and what each field costs you. Layers, top to bottom: Sign, 1 bit (positive or negative); Exponent, 8 bits (range about ten to the plus or minus 38; exp overflows at 88.7); Mantissa, 23 bits stored and 24 used (7.2 decimal digits: the gap at 1 is 0.00000012, at 16.8 million it is 2). Every surprise in this module comes from those three widths. Seven digits is why a variance computed by subtraction fails; the growing gap is why a float32 counter stalls at 16,777,216 and why adding 1 to a hundred million does nothing at all."}},"blocks":[{"t":"h2","id":"thirty-two-bits-three-fields","runs":[{"text":"Thirty-two bits, three fields"}]},{"t":"p","runs":[{"text":"A float32 is scientific notation in binary. Its 32 bits are split into a sign (1 bit), an exponent (8 bits) and a fraction called the mantissa (23 bits):"}]},{"t":"code","lang":"","label":"Text","text":"value = (−1)^sign × 1.mantissa × 2^(exponent − 127)"},{"t":"p","runs":[{"text":"The leading 1 is not stored, because in binary the first significant digit is always 1, so the mantissa carries 24 bits of precision for the price of 23. Everything about how floats behave follows from those three field widths."}]},{"t":"h2","id":"precision-about-seven-digits","runs":[{"text":"Precision: about seven digits"}]},{"t":"p","runs":[{"text":"Twenty-four binary digits is "},{"text":"24 × log10(2) = 7.2","code":true},{"text":" decimal digits. The gap between 1 and the next representable number is "},{"text":"2^-23 = 1.19 × 10^-7","code":true},{"text":", called machine epsilon. Any number is stored to a relative accuracy of about half that, six parts in a hundred million. The absolute gap between neighbouring floats grows with the number: near 1 it is "},{"text":"10^-7","code":true},{"text":", near 1,000 it is "},{"text":"10^-4","code":true},{"text":", near a million it is "},{"text":"0.06","code":true},{"text":", and past "},{"text":"2^24 = 16,777,216","code":true},{"text":" it is 2."}]},{"t":"p","runs":[{"text":"That last fact bites. A float32 counter incremented by 1 stops at 16,777,216, because "},{"text":"16,777,217","code":true},{"text":" is not representable and the addition rounds back down:"}]},{"t":"code","lang":"python","label":"Python","text":"import numpy as np\nx = np.float32(16_777_216)\nprint(x + np.float32(1))       # 16777216.0"},{"t":"p","runs":[{"text":"A running total of tokens, steps or samples kept in float32 silently stalls at seventeen million. Keep counters as integers."}]},{"t":"h2","id":"range-about-10-38","runs":[{"text":"Range: about 10^±38"}]},{"t":"p","runs":[{"text":"Eight exponent bits give exponents from −126 to +127, so the largest float32 is about "},{"text":"3.4 × 10^38","code":true},{"text":" and the smallest normal one about "},{"text":"1.2 × 10^-38","code":true},{"text":". Below that, the format gives up precision to reach a little further, down to "},{"text":"1.4 × 10^-45","code":true},{"text":", after which the number is zero. Module 5 used the top of that range: "},{"text":"exp(x)","code":true},{"text":" overflows to infinity at "},{"text":"x = 88.7","code":true},{"text":". The bottom matters too. A probability of "},{"text":"10^-40","code":true},{"text":" is representable only as a denormal with a few bits of precision, and the product of two probabilities of "},{"text":"10^-25","code":true},{"text":" each is exactly zero. This is the concrete reason everything probabilistic is done in log space."}]},{"t":"h2","id":"why-0-1-0-2-is-famous-and-why-the-format-matters","runs":[{"text":"Why 0.1 + 0.2 is famous, and why the format matters"}]},{"t":"p","runs":[{"text":"One tenth is not representable in binary, any more than one third is in decimal. Stored as float64, "},{"text":"0.1","code":true},{"text":" is "},{"text":"0.1000000000000000055","code":true},{"text":", and"}]},{"t":"code","lang":"python","label":"Python","text":"0.1 + 0.2 == 0.3          # False in float64: 0.30000000000000004"},{"t":"p","runs":[{"text":"In float32 both sides happen to round to the same number, "},{"text":"0.300000012","code":true},{"text":", and the comparison is True. The lesson is not about 0.3. It is that equality between floats is an accident of rounding, and a test like "},{"text":"if loss == 0.0","code":true},{"text":" or "},{"text":"assert a == b","code":true},{"text":" on computed values will pass or fail depending on the format, the order of operations, and the hardware. Compare with a tolerance:"}]},{"t":"code","lang":"python","label":"Python","text":"np.isclose(a, b, rtol=1e-5, atol=1e-8)"},{"t":"p","runs":[{"text":"and choose the tolerances from the format: "},{"text":"rtol","code":true},{"text":" near "},{"text":"10^-5","code":true},{"text":" for float32, "},{"text":"10^-12","code":true},{"text":" for float64."}]},{"t":"h2","id":"the-gap-you-should-picture","runs":[{"text":"The gap you should picture"}]},{"t":"p","runs":[{"text":"Representable floats are not evenly spaced. They are dense near zero and sparse far from it, with the same "},{"text":"number","i":true},{"text":" of floats between 1 and 2 as between 1,024 and 2,048. This has one consequence you will meet constantly: adding a small number to a large one loses the small one. "},{"text":"10^8 + 1","code":true},{"text":" in float32 is "},{"text":"10^8","code":true},{"text":", because the gap between floats near "},{"text":"10^8","code":true},{"text":" is 8. The next lesson but one is about the damage that does."}]},{"t":"h2","id":"doubles-and-when-to-use-them","runs":[{"text":"Doubles, and when to use them"}]},{"t":"p","runs":[{"text":"Float64 has 11 exponent bits and 52 mantissa bits: about 16 digits and a range of "},{"text":"10^±308","code":true},{"text":". NumPy and Python use it by default; deep-learning frameworks use float32 by default because it halves memory and the arithmetic units are faster. The right rule: float32 for the model, float64 for anything that accumulates, checks, or is compared. A loss averaged over a million batches, a gradient check, a statistic computed by subtraction: float64. A weight matrix: float32 or smaller, and the next lesson says how much smaller."}]},{"t":"h2","id":"reading-the-bits-yourself","runs":[{"text":"Reading the bits yourself"}]},{"t":"code","lang":"python","label":"Python","text":"import struct\nbits = struct.unpack(\">I\", struct.pack(\">f\", 0.1))[0]\nprint(f\"{bits:032b}\")     # 0 01111011 10011001100110011001101"},{"t":"p","runs":[{"text":"Sign 0, exponent "},{"text":"01111011 = 123","code":true},{"text":", so "},{"text":"2^(123 − 127) = 2^-4 = 1/16","code":true},{"text":", mantissa "},{"text":"1.6000000238...","code":true},{"text":", and "},{"text":"1.6 / 16 = 0.1000000015","code":true},{"text":". The repeating "},{"text":"1001","code":true},{"text":" in the mantissa is the binary expansion of one tenth being cut off at 23 bits, and that cut is where the "},{"text":"0.0000000015","code":true},{"text":" came from. Once you have seen it, every rounding surprise in the rest of this module has an address."}]}],"check":{"question":[{"text":"A training script keeps a running count of processed tokens in a float32 tensor. After a long run the count is stuck at 16,777,216 though tokens are still arriving. What is happening?"}],"options":[{"text":[{"text":"The tensor has overflowed its maximum value and wrapped around, like an integer would."}],"correct":false,"tempting":[{"text":"Float32 reaches 3.4 × 10^38 before overflow, and overflow gives infinity rather than a wrap; the stall is a precision limit, not a range limit."}]},{"text":[{"text":"Float32 cannot represent integers above about seven digits at all."}],"correct":false,"tempting":[{"text":"It represents every integer up to 2²⁴ exactly and many beyond; what it loses above that point is the odd ones, not integers as such."}]},{"text":[{"text":"The data loader has stopped delivering tokens and the count is correct."}],"correct":false,"tempting":[{"text":"The suspicious value is exactly 2²⁴, which is the number at which float32 loses the ability to represent odd integers; that coincidence is the diagnosis."}]},{"text":[{"text":"Above 2²⁴ the float spacing is 2, so adding 1 rounds away."}],"correct":true,"tempting":null}],"order":[2,3,0,1],"why":null},"prev":{"slug":"fermi-estimates-for-ml","title":"Estimating anything to within a factor of three","minutes":9},"next":{"slug":"half-precision-and-mixed-precision","title":"bf16, fp16, and why training keeps a float32 copy","minutes":9}}}