We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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采用的是IEEE 754规范的二进制浮点数计算方法,由于精度问题。导致0.1+0.2不等于0.3。
解决办法:设置一个误差范围值,通常称为"机器精度",对JavaScript的数字来说,这个值通常是2^-52
在ES6中,该精度值定义在Number.EPSILON中。
if (!Number.EPSILON) { Number.EPSILON = Math.pow(2, -52); } function numbersCloseEnoughToEqual(n1, n2) { return Math.abs(n1 - n2) < Number.EPSILON; } var a = 0.1 + 0.2; var b = 0.3; numbersCloseEnoughToEqual(a, b); //true numbersCloseEnoughToEqual(0.0000001, 0.0000002); //false
The text was updated successfully, but these errors were encountered:
No branches or pull requests
JavaScript采用的是IEEE 754规范的二进制浮点数计算方法,由于精度问题。导致0.1+0.2不等于0.3。
解决办法:设置一个误差范围值,通常称为"机器精度",对JavaScript的数字来说,这个值通常是2^-52
在ES6中,该精度值定义在Number.EPSILON中。
The text was updated successfully, but these errors were encountered: