IEEE 754 Floating Point
0.1 plus 0.2 is not 0.3, and a float explains why. Toggle the 32 bits of sign, exponent, and mantissa, type a decimal, and watch the nearest value your hardware can store snap into place.
IEEE 754 Floating Point
A float is one sign bit, a few exponent bits, and a fraction, packed into 32 or 64 bits. The value is (-1)sign times 1.fraction times 2 raised to (exponent minus bias). Flip any bit below and the decoded number changes, or type a decimal and watch the nearest value the hardware can actually hold snap into place.
Bias on the exponent. The 8-bit exponent field stores an unsigned number, so to reach negative powers of two it carries a fixed offset. Subtract the bias (127) from the stored field to get the real exponent. A stored field of 127 means 2 to the power 0.
The implicit leading 1. Normal numbers are written 1.fraction in binary, and that leading 1 is always there, so it is not stored. You get an extra bit of precision for free. Subnormals are the exception: when the exponent field is all zeros the leading bit is 0 instead, which lets values shrink gradually down to zero.
The four special cases. Exponent field all zeros with a zero fraction is signed zero; all zeros with a nonzero fraction is a subnormal. Exponent field all ones with a zero fraction is infinity; all ones with a nonzero fraction is NaN. Flip the bits above into those shapes and the label updates.
Why decimal fractions break. Binary fractions are sums of 1/2, 1/4, 1/8, and so on. A value like 0.1 is 1/10, and 10 has the factor 5, which no power of two divides, so 0.1 has no finite binary expansion. The hardware stores the nearest value it can, and that tiny rounding gap is what you see below.