-
Notifications
You must be signed in to change notification settings - Fork 1
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
JavaScript Numbers #78
Comments
双精度浮点数存储格式: sign(符号): 用来表示正负号 exponent(指数): 用来表示次方数 mantissa(尾数): 用来表示精确度 符号0代表数值为正,1代表数值为负。 指数双精度浮点数在存储指数位时,会加上一个固定的偏移值。 IEEE 754标准规定该固定值为 尾数尾数一般要求规格化,把尾数处理到 例子例如0.1 转成二进制: 0.1.toString(2)
// "0.0001100110011001100110011001100110011001100110011001101" 科学计数法:
我们自己算出来的是
M 舍去首位的1,得到 100110011001100...,最终就是: references |
安全整数在 2^53二进制表示:
科学计数法:
其实就相当于小数点往左移动了53位。 双精度表示法:
由于双精度浮点数的尾数位数限制,最后一位0丢失了,精度丢失了,所以不认为是一个安全整数 2^53 - 1二进制表示:
科学计数法:
双精度表示法:
2^53 - 2二进制表示:
科学计数法:
双精度表示法:
以此类推,以上范围内的整数都可以由一个唯一的浮点数表示。 如果超出了以上范围,存储就可能不唯一了,例如 2^53 + 1二进制表示:
科学计数法:
双精度表示法:
由于双精度浮点数的尾数位数限制,最后一位1要丢掉。 所以实际上 Math.pow(2, 53) === Math.pow(2, 53) + 1 // true 整数和浮点数不再一一对应,所以就不安全了。 |
JavaScript浮点数相关资料。
The text was updated successfully, but these errors were encountered: