Data Representation: Bits, Bytes & Numbers

IEEE-754 floating point

/ I-triple-E seven-fifty-four /

Integers are fine for whole things, but the world is full of fractions and very large and very small quantities — 0.5, 3.14159, the mass of an electron, the distance to a star. Floating point is how computers store such real numbers in a fixed number of bits. The name is the clue: the decimal (really, binary) point 'floats', so the same format can represent both tiny and enormous values by trading off precision. IEEE-754 is the near-universal standard that pins down exactly how this is done.

The trick is scientific notation in binary. Just as we write a number as a sign, a fraction, and a power of ten (for example -1.5 times 10 to the 3), a float stores three fields: a sign bit (0 for positive, 1 for negative), an exponent (which scales the value up or down by a power of two), and a mantissa, also called the significand or fraction (the precise digits). A 32-bit 'float' splits its bits 1 + 8 + 23; a 64-bit 'double' splits them 1 + 11 + 52, buying more precision and range. For normalized values there is an implicit leading 1 bit before the mantissa, so the significand effectively has one extra bit of precision for free, and the exponent is stored with a bias so it can represent both negative and positive powers.

Floating point is astonishingly useful and astonishingly subtle. Because only a finite number of bits encode an infinite continuum of reals, most values are stored as the NEAREST representable number, not exactly — which is the root of 'why is 0.1 + 0.2 not exactly 0.3?'. It trades exactness for range: a double can represent numbers from about 10 to the -308 up to 10 to the +308, but only about 15-17 significant decimal digits at a time. Use it for measured, scientific, and graphical quantities; reach for integers (or fixed-point) when you need exactness, as with money.

A 32-bit float lays out its bits as 1 sign + 8 exponent + 23 mantissa. The number 1.0 is sign 0, a biased exponent, and a zero fraction (the leading 1 is implicit), stored as 0x3F800000.

Sign + exponent + mantissa = binary scientific notation.

Never compare floats for exact equality (avoid 'if (a == b)'); accumulated rounding means computed values that 'should' be equal often differ by a tiny amount — compare within a small tolerance instead.

Also called
floating pointfloat / double浮點數標準