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

JS:精度丢失0.1 + 0.2 === 0.3 ? #56

Open
HCLacids opened this issue Mar 8, 2022 · 0 comments
Open

JS:精度丢失0.1 + 0.2 === 0.3 ? #56

HCLacids opened this issue Mar 8, 2022 · 0 comments
Labels

Comments

@HCLacids
Copy link
Owner

HCLacids commented Mar 8, 2022

参考链接

JS存放数字是双精度

pic1
pic2
双精度为存放1位为符号,11位表示指数,52位表示小数。
指数偏移量:指数偏移量公式:
formula
27.5 转换为二进制11011.1
11011.1转换为科学记数法
num
符号位0为正数1为负数
指数位e+ 或者e- 1023+/-e 这里e为4+
小数位10111补气52为最后为
0100 0000 0011 1011 1000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000
0.10000000000000000555.toPrecision(16) //0.1000000000000000 去掉末尾的零后正好为0.1

恢复方式

toPrecision()
把浮点数变成整数,然后再除去基数。0.1+0.23 === (10 + 23) / 100

@HCLacids HCLacids added the JS label Mar 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant