floating-pointbinaryieee-754ieee

zero point one in the IEEE 754 standard


So, I'm learning about IEEE 754, and have a question

Why some numbers, such as the numbers 0.1 and 0.2, for example - are not a value that exactly encoded in the IEEE 754 standard?


Solution

  • IEEE-754 specifies both binary and decimal formats. .1 and .2 are of course representable in decimal.

    In the binary formats, each digit position corresponds to a power of two. For the bits 101.011, the positions correspond to 4, 2, 1, ½, ¼, and ⅛. So 101.011 represents 1•4 + 0•2 + 1•1 + 0•½ + 1•¼ + 1•⅛ = 4 + 1 + ¼ + ⅛ = 5⅜ = 5.375. We can change the powers of two by adjusting the exponent (this is the “floating” part of floating-point; the value of the number is multiple by a power of two to move the effective position of the “decimal point”).

    .1 cannot be represented because there is no finite set of powers of two whose sum is .1. Suppose you had a sum S of powers of two p0, p1, p2,… pn, sorted in descending order. Since pn is the smallest of these powers, all the others are multiples of it. That means the sum S is the sum of multiples of pn, so it is a multiple of pn. That means S = j / 2k for some integers j and k. If S = .1, then .1 = j / 2k, so 2k = 10 j, and then 2k−1 = 5 j. But if 2k−1 = 5 j,then 2k−1 is a multiple of five. This is impossible since it is a power of two. So S cannot be .1.