我很好奇将小数 0.1 转换为二进制。
如果我有其他小数,我知道一种方法可以做到这一点 - 例如 0.75
1) 0.75 * 2 = 1.5 >= 1 - 然后变成1
2) 0.5 * 2 = 1 >= 1 - 然后变成1
二进制结果:0,11
但如果小数是0.1——如何应用这个方法呢?
1) 0.1 * 2 = 0 < 1则变为0
就变成0了,没有别的了
0.1 * 2 = 0.2 --> 0 with remainder 0.2
0.2 * 2 = 0.4 --> 0 with remainder 0.4
0.4 * 2 = 0.8 --> 0 with remainder 0.8
0.8 * 2 = 1.6 --> 1 with remainder 0.6
0.6 * 2 = 1.2 --> 1 with remainder 0.2
0.2 * 2 = 0.4 --> 0 with remainder 0.4
0.4 * 2 = 0.8 --> 0 with remainder 0.8
0.8 * 2 = 1.6 --> 1 with remainder 0.6
0.6 * 2 = 1.2 --> 1 with remainder 0.2
Run Code Online (Sandbox Code Playgroud)
ETC..
现在我们得到结果,并在它们前面加上小数点:
0.0001100110011.......
Run Code Online (Sandbox Code Playgroud)
如此循环往复。