TreeviewCopyright © iHTCboy all right reserved, powered by aleen42

Binary(二进制)

为什么 0.1 + 0.2 = 0.300000004

   // 加法
   0.1 + 0.2 = 0.30000000000000004
   0.1 + 0.7 = 0.7999999999999999
   0.2 + 0.4 = 0.6000000000000001

   // 减法
   0.3 - 0.2 = 0.09999999999999998
   1.5 - 1.2 = 0.30000000000000004

   // 乘法
   0.8 * 3 = 2.4000000000000004
   19.9 * 100 = 1989.9999999999998

   // 除法
   0.3 / 0.1 = 2.9999999999999996
   0.69 / 10 = 0.06899999999999999

   // 比较
   0.1 + 0.2 === 0.3 // false
   (0.3 - 0.2) === (0.2 - 0.1) // false

十进制到二进制的转换导致的精度问题!C/C++,Java,Javascript中,准确的说:“使用了IEEE 754浮点数格式”来存储浮点类型(float 32, double 64)的任何编程语言都有这个问题!

console.log('0.1 的二进制是:' + 0.1.toString(2));
console.log('0.2 的二进制是:' + 0.2.toString(2));

"0.1 的二进制是:0.0001100110011001100110011001100110011001100110011001101" 
"0.2 的二进制是:0.001100110011001100110011001100110011001100110011001101"

两者相加:
0.01001100110011001100110011001100110011001100110011001100
转换成10进制之后得到:
0.30000000000000004

字符编码

Copyright © iHTCboy.com 2020-12-05 11:17:44 更新

results matching ""

    No results matching ""