JavaScript/双精度浮点数:修订间差异

来自康健生活
Kangkk​(留言 | 贡献)
Kangkk​(留言 | 贡献)
无编辑摘要
 
(未显示同一用户的21个中间版本)
第209行: 第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}) = [0, 255)</math>,可计算出阶码 E 的取值范围 <math>(-127, 128)</math> 或者 <math>[-126, 127]</math> 或者 <math>-126 \leq E \leq 127</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}] = [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>\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>
下面着重解释区间内数字的转换,以 4 字节的单精度为例,阶码的范围 <math>-126 \leq E \leq 127</math> 可推导出浮点数真值的取值范围为 <math>-2^{126} \sim +2^{128}</math>。


计算的公式,<math>(-1)^S \times 1.000000000000000000000 \times 2^{-126} \approx 3.4E+38</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>


在 8 字节双精度中
<math>
 
\begin{align}
* <math>(1.\overline{0})_{10} \times 2^{-1022} \approx 2.22507E-308</math>
V_{\text{+min}} = (-1)^0 \times 2^{1 - 127} \times (1.00000000000000000000000)_2 \\
* <math>(2^{52})_{10} \times 2^{1023} \approx 1.79769E308</math>
= 2^{-126} \times (2^0 + 0)_{10} \\
* <math>(2^{-52})_{10} \times 2^{-1022} \approx 4.940656E-324</math>
= 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>-2^{1024 } \sim +2^{1024 }</math> 转换为 <math>\pm 2.22507 \times 10^{-308} \sim \pm 1.79769 \times 10^{308}</math>,有效数字为 15 位。
由此可推导出 <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>。


在JavaScript 中所有的数字的实际存储都是通过 8 字节 double 浮点型进行表示,使用 64 位(8字节)来存储一个浮点数。double 浮点型的正负数的范围是 <nowiki>-2^1024 ~ +2^1024</nowiki> 也就是 <nowiki>[-1.79E+308, +1.79E+308]</nowiki>,数的绝对值范围是 <math>[-1.79E+308, -2.23E-308] \cup 0 \cup [2.23E-308, 1.79E+308]</math>。
同理在 8 字节双精度中数的绝对值范围是 <math>[-1.79E+308, -2.23E-308] \cup 0 \cup [2.23E-308, 1.79E+308]</math>。


==== 尾数位 (M) ====
==== 尾数位 (M) ====
第257行: 第276行:
\begin{array}{l}
\begin{array}{l}
V = (-1)^S \times M \times b^E \\
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)} \\
= (-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 \\
\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}} \\
\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}} \\
第397行: 第416行:
0.5 \times 2 = 1 & \quad\cdots\quad\text{取整 1 } & \downarrow \end{aligned}</math>
0.5 \times 2 = 1 & \quad\cdots\quad\text{取整 1 } & \downarrow \end{aligned}</math>


精度损失,比如 <math>(0.3)_{10} = (0.0\overline{1}\overline{0}\overline{0}\overline{1})_2</math>。
精度损失,比如 <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{1}\overline{0}\overline{0}\overline{1}
= 0.0\overline{1001}
\end{align}</math>
\end{align}</math>


第438行: 第457行:
<math>
<math>
\begin{array}{l}  
\begin{array}{l}  
0.1 = (-1)^0 \times 1.1\overline{0}\overline{0}\overline{1}\overline{1} \times 2^-4 \\
0.1 = (-1)^0 \times 1.1\overline{0011} \times 2^-4 \\
0.2 = (-1)^0 \times 1.1\overline{0}\overline{0}\overline{1}\overline{1} \times 2^-3
0.2 = (-1)^0 \times 1.1\overline{0011} \times 2^-3
\end{array}
\end{array}
</math>
</math>
第447行: 第466行:
<math>
<math>
\begin{array}{l}  
\begin{array}{l}  
0.1 \Rightarrow S = 0, M= 1.1 \overline{0}\overline{0}\overline{1}\overline{1}, E = -4 \\
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}\overline{0}\overline{1}\overline{1}, E = -3 \\
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}
第471行: 第490行:
<math>
<math>
\begin{array}{r}  
\begin{array}{r}  
0.1 = 1.1\overline{0}\overline{0}\overline{1}\overline{1} * 2^{-4} \\
0.1 = 1.1\overline{0011} * 2^{-4} \\
     = 0.11\overline{0}\overline{0}\overline{1}\overline{1} * 2^{-3} \\
     = 0.11\overline{0011} * 2^{-3} \\
0.2 = 1.1\overline{0}\overline{0}\overline{1}\overline{1} \times 2^{-3}
0.2 = 1.1\overline{0011} \times 2^{-3}
\end{array}
\end{array}
</math>
</math>
第540行: 第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]