I'm reading a book which says that to transform a number from decimal representation to binary representation you have to follow these steps:
- Take the integer part (the one before the point) of the number and transform it in binary like you normally do with integer numbers: that is the integer part of the binary representation of the number we are trying to convert.
- Take the decimal part (the one after the point) of the original number and multiply it by 2, the integer part of the obtained number is a digit (the current one) of the decimal part of the binary number.
- Repeat step 2 until:
- You got 0 as the decimal part of the obtained number.
- Obtained numbers start to repeat, in that case the number is periodic.
At the end you should get the binary transformation of the original number.
I tried to apply the algorithm to the number: 0,264 and got:
0,264 (dec part) -> 0,264 * 2 = 0,528
0,528 (dec part) -> 0,528 * 2 = 1,056
1,056 (dec part) -> 0,056 * 2 = 0,112
0,112 (dec part) -> 0,112 * 2 = 0,224
0,224 (dec part) -> 0,224 * 2 = 0,448
0,448 (dec part) -> 0,448 * 2 = 0,896
0,896 (dec part) -> 0,896 * 2 = 1,792
1,792 (dec part) -> 0,792 * 2 = 1,584
1,584 (dec part) -> 0,584 * 2 = 1,168
1,168 (dec part) -> 0,168 * 2 = 0,336
0,336 (dec part) -> 0,336 * 2 = 0,672
0,672 (dec part) -> 0,672 * 2 = 1,344
1,344 (dec part) -> 0,344 * 2 = 0,688
0,688 (dec part) -> 0,688 * 2 = 1,376
1,376 (dec part) -> 0,376 * 2 = 0,752
0,752 (dec part) -> 0,752 * 2 = 1,504
1,504 (dec part) -> 0,504 * 2 = 1,008
1,008 (dec part) -> 0,008 * 2 = 0,016
......
... And so on.
As you can see neither we found just 0 as the decimal part of the numbers we got, neither these numbers started to repeat.
Nevertheless, trying to convert 0,264 using this online converter gets the following: 0,01000011100101011, which stops one step before where I stopped in the algorithm calculation I showed in this question.
So I was wondering:
- Why does it stop there? Are there other criteria for stopping in the algorithm?
- Am I getting the algorithm wrong?
I think it may have something to do with precision you want to get, but then what should be the correct criteria for stopping?