貌似发现了个double类型变量运算的bug?
对带小数点的变量(目测是double型,当前的代码中并未声明该变量为double型)进行三次 + 运算后得到的结果莫名其妙的不正确了,得到的异常结果可能比正确结果大也可能比正确结果小,异常结果精确到小数点后16位,bug一定复现。
另外,当计算的变量是整形的时候并不会出现这个问题。
data: { menus: [{ name: 'menu1' , price: 1.00, count: 0 }], totalPrice: 0 }, |
data: { menus: [{ name: 'menu1' , price: 1.10, count: 0 }] |
clkPlus: function (event) { var index = event.currentTarget.dataset.index; var menus_ = this .data.menus; if ( null == menus_[index].count) { return ; } menus_[index].count++; var totalPrice_ = this .data.totalPrice; totalPrice_ = totalPrice_ + menus_[index].price; this .setData({ menus: menus_, totalPrice: totalPrice_ }); } |
< text >已选总价: {{totalPrice}} 元 |
运算过程与结果的控制台输出如下
0 + 1.1
= 1.1
1.1 + 1.1
= 2.2
2.2 + 1.1
= 3.3000000000000003
3.3000000000000003 + 1.1
= 4.4
4.4 + 1.1
= 5.5
5.5 + 1.1
= 6.6
6.6 + 1.1
= 7.699999999999999
7.699999999999999 + 1.1
= 8.799999999999999
8.799999999999999 + 1.1
= 9.899999999999999
9.899999999999999 + 1.1
= 10.999999999999998