In: Computer Science
First, Calculate the 1/3 in binary form using 8-digits. Then convert binary form back to decimal. Why and what is the error in binary representation ?
Fractions in binary arithmetic terminate
only if 2 is the only prime factor in the denominator. As a result,
1/10 does not have a finite binary representation
(10has prime factors 2 and
5). This causes 10 × 0.1 not to precisely equal 1
in floating-point arithmetic. As an example, to interpret the
binary expression for 1/3 = .010101..., this means: 1/3 = 0 ×
2−1 + 1 ×
2−2 + 0 ×
2−3 + 1 ×
2−4 + ... = 0.3125 + ... An exact value
cannot be found with a sum of a finite number of inverse powers of
two, the zeros and ones in the binary representation of 1/3
alternate forever.
Let's look at it another way - in base 10 which you're likely to be comfortable with, you can't express 1/3 exactly. It's 0.3333333... (recurring). The reason you can't represent 0.1 as a binary floating point number is for exactly the same reason. You can represent 3, and 9, and 27 exactly - but not 1/3, 1/9 or 1/27.
The problem is that 3 is a prime number which isn't a factor of 10. That's not an issue when you want to multiply a number by 3: you can always multiply by an integer without running into problems. But when you divide by a number which is prime and isn't a factor of your base, you can run into trouble (and will do so if you try to divide 1 by that number).
Although 0.1 is usually used as the simplest example of an exact decimal number which can't be represented exactly in binary floating point, arguably 0.2 is a simpler example as it's 1/5 - and 5 is the prime that causes problems between decimal and binary.