# simplify the life

## 【0.1 + 0.2 = 0.30000000000000004】该怎样理解？

(s) * (m) * (2^e)

s 是符号位，表示正负。m 是尾数，有 52 bits。e 是指数，有 11 bits，e 的范围是 [-1074, 971]ECMAScript 5 规范），这样其实很容易推出 Javascript 能表示的最大数为：

1 * (Math.pow(2, 53) - 1) * Math.pow(2, 971) = 1.7976931348623157e+308

1 * 1 * Math.pow(2, -1074) = 5e-324

// 0.1 转化为二进制
0.0 0011 0011 0011 0011...(0011循环）

// 0.2 转化为二进制
0.0011 0011 0011 0011 0011...(0011循环）

e = -4; m = 1.1001100110011001100110011001100110011001100110011010 (52位)
e = -3; m = 1.1001100110011001100110011001100110011001100110011010 (52位)

e = -4; m = 1.1001100110011001100110011001100110011001100110011010 (52位)
+ e = -3; m = 1.1001100110011001100110011001100110011001100110011010 (52位)
---------------------------------------------------------------------------
e = -3; m = 0.1100110011001100110011001100110011001100110011001101
+ e = -3; m = 1.1001100110011001100110011001100110011001100110011010
---------------------------------------------------------------------------
e = -3; m = 10.0110011001100110011001100110011001100110011001100111
---------------------------------------------------------------------------
e = -2; m = 1.0011001100110011001100110011001100110011001100110100(52位)
---------------------------------------------------------------------------
= 0.010011001100110011001100110011001100110011001100110100
= 0.30000000000000004(十进制)

9007199254740992 + 1 = 9007199254740992 的推理过程大同小异。

9007199254740992 其实就是 2 ^ 53。

e = 0; m = 100000000000000000000000000000000000000000000000000000 (53个0)
+ e = 0; m = 1
---------------------------------------------------------------------------
e = 0; m = 100000000000000000000000000000000000000000000000000001

Number.MAX_VALUE + 1 == Number.MAX_VALUE;
Number.MAX_VALUE + 2 == Number.MAX_VALUE;
...
Number.MAX_VALUE + x == Number.MAX_VALUE;
Number.MAX_VALUE + x + 1 == Infinity;
...
Number.MAX_VALUE + Number.MAX_VALUE == Infinity;

// 问题：
// 1. x 的值是什么？
// 2. Infinity - Number.MAX_VALUE == x + 1; 是 true 还是 false ?

2016-07-21 补：

(0.1 * 10 + 0.2 * 10) / 10
=> 0.3

2177.74*100
=> 217773.99999999997

posted on 2015-12-10 06:37 子迟 阅读(...) 评论(...) 编辑 收藏

• 随笔 - 165
• 文章 - 0
• 评论 - 581