Reputation: 604
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?
Upvotes: 0
Views: 103
Reputation: 1570
@Eric explained the basics already. There are however a lot of other issues you need to be aware of when working with floating point numbers.
I recently saw this blog post on the topic, it has an explanation of the binary fractions, examples of how things go wrong with possible solutions and external links with more information on the topic.
https://www.microforum.cc/blogs/entry/1-floating-point-numbers/
Upvotes: 0
Reputation: 224576
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.
Upvotes: 2