十進制 二進制
0.1 0.0001 1001 1001 1001 ...
0.2 0.0011 0011 0011 0011 ...
0.3 0.0100 1100 1100 1100 ...
0.4 0.0110 0110 0110 0110 ...
0.5 0.1
0.6 0.1001 1001 1001 1001 ...
所以比如 1.1 ,其程序?qū)嶋H上無法真正的表示 ‘1.1',而只能做到一定程度上的準(zhǔn)確,這是無法避免的精度丟失:
1.09999999999999999
在JavaScript中問題還要復(fù)雜些,這里只給一些在Chrome中測試數(shù)據(jù):
輸入 輸出
1.0-0.9 == 0.1 False
1.0-0.8 == 0.2 False
1.0-0.7 == 0.3 False
1.0-0.6 == 0.4 True
1.0-0.5 == 0.5 True
1.0-0.4 == 0.6 True
1.0-0.3 == 0.7 True
1.0-0.2 == 0.8 True
1.0-0.1 == 0.9 True
解決
那如何來避免這類 1.0-0.9 != 0.1 的非bug型問題發(fā)生呢?下面給出一種目前用的比較多的解決方案, 在判斷浮點運算結(jié)果前對計算結(jié)果進行精度縮小,因為在精度縮小的過程總會自動四舍五入:
復(fù)制代碼 代碼如下:
(1.0-0.9).toFixed(digits) // toFixed() 精度參數(shù)須在 0 與20 之間
parseFloat((1.0-0.9).toFixed(10)) === 0.1 // 結(jié)果為True
parseFloat((1.0-0.8).toFixed(10)) === 0.2 // 結(jié)果為True
parseFloat((1.0-0.7).toFixed(10)) === 0.3 // 結(jié)果為True
parseFloat((11.0-11.8).toFixed(10)) === -0.8 // 結(jié)果為True
復(fù)制代碼 代碼如下:
// 通過isEqual工具方法判斷數(shù)值是否相等
function isEqual(number1, number2, digits){
digits = digits == undefined? 10: digits; // 默認(rèn)精度為10
return number1.toFixed(digits) === number2.toFixed(digits);
}
isEqual(1.0-0.7, 0.3); // return true
// 原生擴展方式,更喜歡面向?qū)ο蟮娘L(fēng)格
Number.prototype.isEqual = function(number, digits){
digits = digits == undefined? 10: digits; // 默認(rèn)精度為10
return this.toFixed(digits) === number.toFixed(digits);
}
(1.0-0.7).isEqual(0.3); // return true
新聞熱點
疑難解答
圖片精選