We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Learn more about funding links in repositories.
Report abuse
There was an error while loading. Please reload this page.
第317天 js如何解决数字精度丢失的问题?
作者:一条有梦想的鱼
#80
我也要出题
对做运算的两个数字做化整处理(同乘10的n次幂),运算完再降级处理(除10的n次幂)
https://forever-z-133.github.io/demo-preview/#/./pages/function/someFunction?id=-数字计算 这个函数也写过,但也存在幂次方或运算时超出安全数的问题。
另外提一嘴,在翻倍到整数时直接乘也是不稳妥的, 最好乘后还要四舍五入,或转为字符串 replace 掉小数点毕竟稳妥。
const verifyFunc = (left, right) => { return Math.abs(left - right) < Number.EPSILON * Math.pow(2, 2); };
console.log(verifyFunc(0.1 + 0.2, 0.3));
Activity
tanxer commentedon Feb 27, 2020
对做运算的两个数字做化整处理(同乘10的n次幂),运算完再降级处理(除10的n次幂)
longhui520 commentedon Feb 27, 2020
forever-z-133 commentedon Feb 28, 2020
https://forever-z-133.github.io/demo-preview/#/./pages/function/someFunction?id=-数字计算
这个函数也写过,但也存在幂次方或运算时超出安全数的问题。
另外提一嘴,在翻倍到整数时直接乘也是不稳妥的,
最好乘后还要四舍五入,或转为字符串 replace 掉小数点毕竟稳妥。
NAZBIN commentedon Jun 22, 2020
const verifyFunc = (left, right) => {
return Math.abs(left - right) < Number.EPSILON * Math.pow(2, 2);
};
console.log(verifyFunc(0.1 + 0.2, 0.3));