JavaScript/双精度浮点数:修订间差异
来自康健生活
| 第170行: | 第170行: | ||
= (1100000000111011001010000000000000000000000000000000000000000000)_{\text{双精度浮点数}} | = (1100000000111011001010000000000000000000000000000000000000000000)_{\text{双精度浮点数}} | ||
\end{align}</math> | \end{align}</math> | ||
== 规约 == | |||
=== 规约形式的浮点数 === | |||
规约形式的浮点数(normal number)的表达形式: | |||
{| class="wikitable" | |||
|- | |||
! S !! e !! M | |||
|- | |||
| s || <math>e \neq 0 \cup e \neq 255</math> || <math>mm \cdots mm</math> | |||
|} | |||
=== 非规约形式的浮点数 === | |||
如上文所知,规格化的数字在浮点数的存储格式中遵循 <math>e \neq 0 \cup e \neq 255</math>,除此之外的数字的浮点数表示都属于非规约形式的浮点数(subnormal number),特指为在浮点运算中特殊的值。 | |||
<math> | |||
\begin{array}{l} | |||
V = (-1)^S \times M \times b^E \\ | |||
= (-1)^S \times \bigg(\sum_{i = 1}^m d_{m - i} \times b^{-i} \bigg) \times b^{e - (b^{n - 1} - 1)} \\ | |||
\because b = 2 \\ | |||
\therefore V = (-1)^S \times \bigg(\sum_{i = 1}^m d_{m - i} \times 2^{-i} \bigg) \times 2^{e - (2^{n - 1} - 1) {\color{Red} + 1}} \\ | |||
= (-1)^S \times \bigg(\sum_{i = 1}^m d_{m - i} \times 2^{-i} \bigg) \times 2^{e - 2^{n - 1} {\color{Red} + 2}} \\ | |||
\end{array} | |||
</math> | |||
{| class="wikitable" | |||
|- | |||
! S !! e !! M | |||
|- | |||
| s || <math>00 \cdots 00</math> || <math>mm \cdots mm</math> | |||
|} | |||
其中第一种情况为指数为全部为 0,尾数位为有效数字 0 或 1。 | |||
又细分为几种情况: | |||
* <code>0 00000000 0000000000000000000000</code>,当尾数位同样全部为零时 数字为 0。 | |||
* <code>0 00000000 m</code>,因为非规格浮点数的尾数位并没有隐藏位,或者说隐藏位的二进制数值是 0,所以通过计数法转为十进制 <math>(-1)^S \times (0.M)_2 \times (2^{-127})_{10}</math>。 | |||
* <code>1 00000000 0000000000000000000000</code>,数字为 <code>-0</code>。在数学意义上 <math>-0 = 0</math>,但是在计算机科学中 <code>-0</code> 是浮点数中表达 0 的一种方式,可以理解为从负方向上趋近于零的数,所以理论上 <math>-0 < 0</math>。 | |||
{| class="wikitable" | |||
|- | |||
! S !! e !! M | |||
|- | |||
| s || <math>11 \cdots 11</math> || <math>00 \cdots 00</math> | |||
|} | |||
无穷大、无穷小 | |||
{| class="wikitable" | |||
|- | |||
! S !! e !! M | |||
|- | |||
| s || <math>11 \cdots 11</math> || <math>m \neq 0 </math> | |||
|} | |||
NaN,该数值被解释为不是一个数字。 | |||
特殊值(双精度): | |||
# 指数位为 <code>1111 1111 1111</code> 时,尾数位为 <math>\overline{0}</math>,表示值为 <code>Infinity</code>。 | |||
# 指数位为 <code>1111 1111 1111</code> 时,尾数位为 <math>\neg{0}</math>,表示值为 <code>NaN</code>。 | |||
==== 符号位(S) ==== | ==== 符号位(S) ==== | ||