0

In week-1 of CS50 course to introduction to computer programming, we are explained about the limitations of data types that are used in computers nowadays and how the computation 1/10 to 50 decimal places is not accurate. The reason I am posting this question is that I did not the get the reason as to why after certain zeroes(after the decimal), the computer starts to show us digits like 5,7,2 and all other random digits. I believe(as told to us by CS50) it is easier for the computer to store the number- 0 than the number- 5 since it takes less space(it requires only 1 bit). Since that way it can store more number of digits, shouldn't the computer show us zeroes instead of all the other random digits till as many digits as we want. What is the news for the computer to approximate the value of 1/10. I would be very grateful anybody who could solve this doubt for me.

1

The problem is how numbers are stored. A computer operates with a binary or base-2 number system. The real world operates in a base-10 system. The only fractional numbers that can be stored with perfect accuracy are those where the denominator is a power of 2, like 1/2, 1/4, 3/4, 5/8, etc.

Any other fractional part of a floating point number, i.e., the part of a float to the right of a decimal point, will only be stored in a float with a fixed number of binary digits as a close approximation. The more bits, the more precise the approximation, but still not perfect. This is because binary representations of numbers cannot represent base 10 numbers perfectly, except for powers of 2, which match up in both bases. Anything else will result in a conversion between bases that is always just a little off as you use more and more digits to convert between bases because the conversion will never end.

Think of it this way. We all know what 2/3 is. Now, convert it to a decimal in base 10. It's 0.66666666.... a number that never ends, repeating 3's forever. Now let's make believe that you have a computer of the future that works and stores numbers in base 10, but it has a limit of 8 digits. Then, 1/3 would be 0.66666667 (give or take a digit). It's a close approximation, but it isn't exactly 1/3 because it stops at 8 digits. That's the limit of its capability to store a number.

Think about this too. In base 10, the only fractions that can be stored as a non-repeating decimal are those where the denominator can be factored to powers of 2, powers of 5, and multiples of 2 and 5. Similarly, the only fractions that can be stored accurately in base 2 are those where the denominator is a power of 2. Finally, the only base 10 fractions that can be accurately stored in a binary system are those that have a denominator that is a power of 2 - the only prime factor of both 2 and 10!

Are you getting the idea yet?

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

1
  • Thanks a lot for the explanation, it helped a lot. – Arjun599 Apr 8 '17 at 19:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .