這可能是一個真正合理的答案的問題..但我真的不明白這一點!javascript循環給出了不同的結果
爲什麼,這是否給出不同的結果..
唯一的區別是一個for循環和while循環。即使當他們循環多次準確???
array = [1.2344, 2.47373, 3.444];
var total = 0,
total2 = 0,
i = array.length,
whileLoops = 0,
forLoops = 0;
while (i--) {
whileLoops++;
total += array[i];
}
for (var i = 0, len = array.length; i < len; i++) {
forLoops++;
total2 += array[i];
}
if (total !== total2) {
console.log("BOE")
}
我試過parseFloat,但這也並沒有幫助:(
這是因爲在某種方式???
的Javascript引擎回合數根據要求:小提琴http://jsfiddle.net/5dsx0ump/
UPDATE
請問解決方案是第一個到* 1000和所有的計算後,1000又分爲,以保持輪numbe RS?
你可以給一個小提琴,或告訴我們你得到的輸出? – 2015-01-20 23:08:42
如果使用整數,則會得到相同的結果。顯然問題是四捨五入。 – 2015-01-20 23:10:03
'1.2344 + 2.47373 + 3.444!= 3.444 + 2.47373 + 1.2344' – Fabricator 2015-01-20 23:14:55