Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

0.1+0.2=0.30000000000000004 的问题 #8

Open
qingfengmy opened this issue Mar 12, 2018 · 0 comments
Open

0.1+0.2=0.30000000000000004 的问题 #8

qingfengmy opened this issue Mar 12, 2018 · 0 comments

Comments

@qingfengmy
Copy link
Owner

假设表示小数的位数是4,则2进制只能表示

二进制 换算过程 十进制
0.0001 2^-4=1/16=0.0625 0.0625
0.0010 2^-3=1/8=0.125 0.125
0.0011 2^-3+2^-4=1/8+1/16=0.1875 0.1875
0.0100 2^-2=1/4=0.25 0.25
0.0101 2^-2+2^-4=0.25+0.0625=0.3125 0.3125
0.0110 2^-2+2^-3=0.25+0.125=0.375 0.375
0.0111 2^-2+2^-3+2^-4=0.25+0.125+0.0625=0.4375 0.4375
0.1000 2^-1=0.5 0.5
0.1001 2^-1+2^-4=0.5+0.0625=0.5625 0.5625
0.1010 2^-1+2^-3=0.5+0.125=0.625 0.625
0.1011 2^-1+2^-3+2^-4=0.5+0.125+0.0625=0.6875
0.1100 2^-1+2^-2=0.5+0.25=0.75 0.75
0.1101 2^-1+2^-2+2^-4=0.5+0.25+0.0625=0.8125 0.8125
0.1110 2^-1+2^-2+2^-3=0.5+0.25+0.125=0.875 0.875
0.1111 2^-1+2^-2+2^-3+2^-4=0.9375 0.9375

通过上述表格内容,我们发现 小数的二进制形式无法表示全部的十进制小数,换句话说 并不是所有的十进制小数能被二进制表示

即使增加二进制的小数表示位,十进制的数也在增加。所以一些十进制的小数无法精确表示,只能是约等于。

参考文章:
JavaScript 为什么不能处理小数运算
2进制的0.0001为什么对应10进制的0.0625

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant