JavaScript/双精度浮点数:修订间差异
来自康健生活
无编辑摘要 |
|||
| (未显示同一用户的48个中间版本) | |||
| 第133行: | 第133行: | ||
</math> | </math> | ||
==== 实现 ==== | |||
= | |||
也就是说当我们遇到一个数字的时候,如果想知道它的浮点数,就可以通过公式进行逆向,比如数字<code>-27.15625</code>。将数字<code>-27.15625</code>拆成三部分<code>-</code>、<code>27</code>、<code>0.15625</code>,分别进行十进制转二进制: | 也就是说当我们遇到一个数字的时候,如果想知道它的浮点数,就可以通过公式进行逆向,比如数字<code>-27.15625</code>。将数字<code>-27.15625</code>拆成三部分<code>-</code>、<code>27</code>、<code>0.15625</code>,分别进行十进制转二进制: | ||
| 第162行: | 第150行: | ||
& 0.5 \times 2 = {\color{Red} 1} & \quad\cdots\quad\text{取 1} & \downarrow \end{aligned}</math> | & 0.5 \times 2 = {\color{Red} 1} & \quad\cdots\quad\text{取 1} & \downarrow \end{aligned}</math> | ||
最后获得结论 <math>(-1) \times (11011 + 0.00101) = -11011.00101</math> | 最后获得结论 <math>(-1) \times (11011 + 0.00101) = -11011.00101</math>。推导过程: | ||
<math>\begin{align} | <math>\begin{align} | ||
(-27.15625)_{10} = (-11011.00101)_2 \\ | (-27.15625)_{10} = (-11011.00101)_2 \\ | ||
= (1100111011001010)_{\text{半精度浮点数}} \\ | = (-1)^{1} \times (1.101100101)_{2} \times 2^4 \\ | ||
\because S = (1)_2, E = (4)_{10}, M = (101100101)_2 \\ | |||
\therefore e = E + 1023 = (1027)_{10} = (1000000011)_2 \\ | |||
\Rightarrow (1100111011001010)_{\text{半精度浮点数}} \\ | |||
= (11000001110110010100000000000000)_{\text{单精度浮点数}} \\ | = (11000001110110010100000000000000)_{\text{单精度浮点数}} \\ | ||
= (1100000000111011001010000000000000000000000000000000000000000000)_{\text{双精度浮点数}} | = (1100000000111011001010000000000000000000000000000000000000000000)_{\text{双精度浮点数}} | ||
\end{align}</math> | \end{align}</math> | ||
反过来通过二进制科学算法计算: | |||
<math> | |||
\begin{array}{l} | |||
\begin{cases} | |||
S = 1 \\ | |||
e = (1000000011)_2 = (1027)_{10} \\ | |||
E = e - 1023 = 4 \\ | |||
M = 101100101 | |||
\end{cases} \\ | |||
V = (-1)^S \times M \times 2^E \\ | |||
= (-1)^{1} \times ({\color{Red}1}.101100101)_{2} \times 2^4 \\ | |||
= -1 \times (11011.00101)_2 \\ | |||
= -1 \times (2^4 + 2^3 + 2^1 + 2^0 + 2^{-3} + 2^{-5})_{10} \\ | |||
= -1 \times (27.15625)_{10} \\ | |||
= (-27.15625)_{10} | |||
\end{array} | |||
</math> | |||
使用另一种计算方法进行推导: | |||
<math> | |||
\begin{array}{l} | |||
\because m = 52, n = 11 \\ | |||
\therefore V = (-1)^S \times ( \frac{52}{2^{52}} + \frac{M}{2^{52}} ) \times 2^{e - 1023} \\ | |||
= (-1)^S \times \bigg( 1 + \bigg(\sum_{i = 0}^{51} d_{i} \times 2^{i} \bigg) \times \frac{1}{2^{52}} \bigg) \times 2^{e - 1023} \\ | |||
\because e = \sum_{j = 0}^{n} p_{j} \times 2^{j} = \sum_{j = 0}^{10} p_{j} \times 2^{j} = 1027 \\ | |||
\therefore V = (-1)^{1} \times \bigg( 1 + \frac{2^{51} + 2^{49} + 2^{48} + 2^{45} + 2^{43}}{2^{52}} \bigg) \times 2^{4} \\ | |||
= -27.15625 | |||
\end{array} | |||
</math> | |||
==== 符号位(S) ==== | ==== 符号位(S) ==== | ||
| 第186行: | 第209行: | ||
以上就是关于指数位的几个基本的概念定义。下面说一下其中各参数(阶码与指数域编码)的取值范围的来源。从公式 <math>E = e - (2^{n - 1} - 1)</math> 可知阶码的取值范围取决于指数域编码值 e,在这之前还需要了解一个概念,即浮点数的规约形式与非规约形式。在非规约形式中指数位的编码为 <math>(0_{n-1}0_{n-2}\cdots 0_{1}0_{0})_2 = (0)_{10}</math> 或者 <math>(1_{n-1}1_{n-2}\cdots 1_{1}1_{0})_2 = (2^n - 1)_{10}</math>,不计入指数域编码值 e 的取值范围,所以去掉非规约值后的规约值范围为 <math>[1, 2^{n} - 1)</math>。通过公式计算可得出阶码 E 的取值范围为 <math>[1 - (2^{n - 1} - 1), 2^{n} - 1 - (2^{n - 1} - 1) = [2 - 2^{n - 1}, 2^{n - 1}) = (1 - 2^{n - 1}, 2^{n - 1})</math>。 | 以上就是关于指数位的几个基本的概念定义。下面说一下其中各参数(阶码与指数域编码)的取值范围的来源。从公式 <math>E = e - (2^{n - 1} - 1)</math> 可知阶码的取值范围取决于指数域编码值 e,在这之前还需要了解一个概念,即浮点数的规约形式与非规约形式。在非规约形式中指数位的编码为 <math>(0_{n-1}0_{n-2}\cdots 0_{1}0_{0})_2 = (0)_{10}</math> 或者 <math>(1_{n-1}1_{n-2}\cdots 1_{1}1_{0})_2 = (2^n - 1)_{10}</math>,不计入指数域编码值 e 的取值范围,所以去掉非规约值后的规约值范围为 <math>[1, 2^{n} - 1)</math>。通过公式计算可得出阶码 E 的取值范围为 <math>[1 - (2^{n - 1} - 1), 2^{n} - 1 - (2^{n - 1} - 1) = [2 - 2^{n - 1}, 2^{n - 1}) = (1 - 2^{n - 1}, 2^{n - 1})</math>。 | ||
例如单精度结构下 IEEE 754 标准的指数位 E 有 8 位,计算偏移值为 <math>E = e - (2^{{\color{Red}8} - 1} - 1) = e - 127</math>,指数域编码值 e 可表示的值范围为 <math>[0, {\color{Green}2^8 - 1} | 例如单精度结构下 IEEE 754 标准的指数位 E 有 8 位,计算偏移值为 <math>E = e - (2^{{\color{Red}8} - 1} - 1) = e - 127</math>,指数域编码值 e 可表示的值范围为 <math>[0, {\color{Green}2^8 - 1}] = [0, 255]</math>,可计算出阶码 E 的取值范围 <math>[-127, 128]</math>。其中 <math>-127</math> 与 <math>128</math> 保留给非规约数。所以有效规约数的取值范围为 <math>-126 \leq E \leq 127</math>,即 <math>[-126, 127]</math>。这里着重注意取值范围的'''开闭区间'''。 | ||
下面着重解释区间内数字的转换,以 4 字节的单精度为例,阶码的范围 <math>-126 \leq E \leq 127</math> 可推导出浮点数真值的取值范围为 <math>-2^{126} \sim +2^{128}</math>。 | |||
<math> | |||
\begin{align} | |||
V_{\text{+max}} = (-1)^0 \times 2^{254 - 127} \times (1.11111111111111111111111)_2 \\ | |||
= 2^{127} \times \Bigg( 2^0 + \sum_{0}^{23 - 1} 2^{n - 23} \Bigg) \\ | |||
= 2^{127} \times (2^0 + (1 - \frac{1}{2^{23}}) )_{10} \\ | |||
= 2^{127} \times (1 + 0.9999998807907104)_{10} \\ | |||
\approx 2^{127} \times 2 \\ | |||
= 2^{128} \\ | |||
= 3.4028236692093846346337460743177e+38 \\ | |||
\approx 3.40282E+38 \\ | |||
\Rightarrow V_{\text{-min}} = (-1)^{1} \times 2^{254 - 127} \times (1.11111111111111111111111)_2 \\ | |||
\approx -3.40282E+38 | |||
\end{align} | |||
</math> | |||
<math> | |||
\begin{align} | |||
V_{\text{+min}} = (-1)^0 \times 2^{1 - 127} \times (1.00000000000000000000000)_2 \\ | |||
= 2^{-126} \times (2^0 + 0)_{10} \\ | |||
= 2^{-126} \\ | |||
= 1.1754943508222875079687365372222e-38 \\ | |||
\approx 1.17549E-38 \\ | |||
\Rightarrow V_{\text{-max}} = (-1)^{1} \times 2^{1 - 127} \times (1.00000000000000000000000)_2 \\ | |||
\approx -1.17549E-38 | |||
\end{align} | |||
</math> | |||
由此可推导出 <math>\pm 1.17549 \times 10^{-38} \sim \pm 3.40282 \times 10^{38}</math>,也就是 <math>\pm 1.17549E-38 \sim \pm 3.40282E+38</math>,即 <math>[-3.40282E+38, -1.17549E-38] \cup 0 \cup [1.17549E-38, 3.40282E+38]</math>。 | |||
<math> | 同理在 8 字节双精度中数的绝对值范围是 <math>[-1.79E+308, -2.23E-308] \cup 0 \cup [2.23E-308, 1.79E+308]</math>。 | ||
==== 尾数位 ( | ==== 尾数位 (M) ==== | ||
尾数 (mantissa) 是一个二进制小数,用来表示精度。尾数的规格化取值范围为 <math>1 \leq M < 2</math>。 | |||
调换一下位置可以更直观,在将尾数存入内存中时,舍弃尾数的整数位,尾数的小数位放入尾数位中,按照不同精度进行修正(裁剪或者填充 <code>0</code>)。 | 调换一下位置可以更直观,在将尾数存入内存中时,舍弃尾数的整数位,尾数的小数位放入尾数位中,按照不同精度进行修正(裁剪或者填充 <code>0</code>)。 | ||
| 第209行: | 第258行: | ||
隐含位、隐藏位亦或者称之为隐藏位表示方法。 | 隐含位、隐藏位亦或者称之为隐藏位表示方法。 | ||
== 规约 == | |||
=== 规约形式的浮点数 === | |||
规约形式的浮点数(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({\color{Red}0 \times 2^{0} +} \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>。 | |||
== 进制转换 == | == 进制转换 == | ||
| 第300行: | 第409行: | ||
==== 十进制小数位(浮点数)转二进制 ==== | ==== 十进制小数位(浮点数)转二进制 ==== | ||
小数位转二进制,乘 2,递进再取小数位乘 2,直到小数部分为 0。如,(0.125)<sub>10</sub> = ( | 小数位转二进制,乘 2,递进再取小数位乘 2,直到小数部分为 0。如,(0.125)<sub>10</sub> = (0.001)<sub>2</sub>,即 2^-3。 | ||
< | <math>\begin{aligned} & & \text{顺序排序} \\ | ||
0.125 \times 2 = 0.25 & \quad\cdots\quad\text{取整 0} & \mid \\ | |||
0.25 \times 2 = 0.5 & \quad\cdots\quad\text{取整 0} & \mid \\ | |||
0.5 \times 2 = 1 & \quad\cdots\quad\text{取整 1 } & \downarrow \end{aligned}</math> | |||
</ | |||
精度损失,比如 <math>(0.3)_{10} = (0.0\overline{ | 精度损失,比如 <math>(0.3)_{10} = (0.0\overline{1001})_2</math>。 | ||
<math>\begin{align} | <math>\begin{align} | ||
(0.3)_{10} = 0.010011001100110011001 \cdots \\ | (0.3)_{10} = 0.010011001100110011001 \cdots \\ | ||
= 0.0\overline{ | = 0.0\overline{1001} | ||
\end{align}</math> | \end{align}</math> | ||
== 标题文本 == | == 标题文本 == | ||
| 第357行: | 第438行: | ||
=== 大端序 === | === 大端序 === | ||
=== 小端序 === | === 小端序 === | ||
== 其他 == | == 其他 == | ||
[https://www.h-schmidt.net/FloatConverter/IEEE754.html 浮点数测试工具] | [https://www.h-schmidt.net/FloatConverter/IEEE754.html 浮点数测试工具] | ||
| 第432行: | 第457行: | ||
<math> | <math> | ||
\begin{array}{l} | \begin{array}{l} | ||
0.1 = (-1)^0 \times 1.1\overline{ | 0.1 = (-1)^0 \times 1.1\overline{0011} \times 2^-4 \\ | ||
0.2 = (-1)^0 \times 1.1\overline{ | 0.2 = (-1)^0 \times 1.1\overline{0011} \times 2^-3 | ||
\end{array} | \end{array} | ||
</math> | </math> | ||
| 第441行: | 第466行: | ||
<math> | <math> | ||
\begin{array}{l} | \begin{array}{l} | ||
0.1 \Rightarrow S = 0, M= 1.1 \overline{ | 0.1 \Rightarrow S = 0, M= 1.1 \overline{0011}, E = -4 \\ | ||
e = (-4 + 1023)_{10} = (1019)_{10} = (1111111011)_2 \\ | e = (-4 + 1023)_{10} = (1019)_{10} = (1111111011)_2 \\ | ||
0.2 \Rightarrow S = 0, M= 1.1 \overline{ | 0.2 \Rightarrow S = 0, M= 1.1 \overline{0011}, E = -3 \\ | ||
e = (-3 + 1023)_{10} = (1020)_{10} = (1111111100)_2 | e = (-3 + 1023)_{10} = (1020)_{10} = (1111111100)_2 | ||
\end{array} | \end{array} | ||
| 第465行: | 第490行: | ||
<math> | <math> | ||
\begin{array}{r} | \begin{array}{r} | ||
0.1 = 1.1\overline{ | 0.1 = 1.1\overline{0011} * 2^{-4} \\ | ||
= 0.11\overline{ | = 0.11\overline{0011} * 2^{-3} \\ | ||
0.2 = 1.1\overline{ | 0.2 = 1.1\overline{0011} \times 2^{-3} | ||
\end{array} | \end{array} | ||
</math> | </math> | ||
| 第534行: | 第559行: | ||
# [https://docs.oracle.com/cd/E19422-01/819-4817/819-4817.pdf oracle 数值计算指南] | # [https://docs.oracle.com/cd/E19422-01/819-4817/819-4817.pdf oracle 数值计算指南] | ||
# [https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html 每位计算机科学家都应该了解的浮点运算] | # [https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html 每位计算机科学家都应该了解的浮点运算] | ||
# [https://devtool.tech/double-type 双精度浮点数内部表示法] | |||
# [https://github.com/evanw/float-toy Float Toy] | |||
# [https://github.com/bartaz/ieee754-visualization ieee754 visualization] | |||