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.3 嘛?为什么? #131

Open
GleanCoder1116 opened this issue Mar 1, 2021 · 0 comments
Open

0.1 + 0.2 === 0.3 嘛?为什么? #131

GleanCoder1116 opened this issue Mar 1, 2021 · 0 comments
Labels
高频面试 高频面试

Comments

@GleanCoder1116
Copy link
Member

GleanCoder1116 commented Mar 1, 2021

总结回答

精度丢失可能出现在进制转换和对阶运算中

个人理解

JavaScript使用Number类型表示数字(整数和浮点数),遵循 IEEE 754 标准 通过64位来表示一个数字;只要是 IEEE 754 标准的语言都会有这个问题;

计算机无法对十进制直接进行运算,硬件决定;需要首先需要进制转换, 0.1和0.2 转换成二进制后会无限循环;这是第一个异常点;

接下来,需要进行 对阶运算, 由于指数位数不相同,运算时需要对阶运算 这部分也可能产生精度损失

对阶(exponent matching)是指将两个进行运算的浮点数的阶码对齐的操作。对阶的目的是为使两个浮点数的尾数能够进行加减运算。

@GleanCoder1116 GleanCoder1116 added the 高频面试 高频面试 label Sep 6, 2021
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