查看“︁JavaScript/双精度浮点数”︁的源代码
来自康健生活
←
JavaScript/双精度浮点数
因为以下原因,您没有权限编辑该页面:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
== 引言 == JavaScript 的数字是以 64 位(双精度)浮点数形式存储。 === 定义 === <pre> | S | E(n位)| M(m位)| ------------------------------ | 0 | 00...00 | 00...00 | </pre> 下文中将统一指数位的位数定义为 n,尾数位的位数定义为 m。 === 数码 === 表示进制数的元素,二进制的数码为 <code>0,1</code>,十进制的数码 <code>0,1,2,3,4,5,6,7,8,9</code>,十六进制的数码 <code>0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F</code>。 === 基数 === 表示数码的个数,二进制的基数为 2,十进制的基数为 10,十六进制的基数为 16。 === 位权 === <math>(N)_R = a_{n-1}R^{n-1} + \cdots + a_{0}R^{0} + a_{-1}R^{-1} + \cdots + a_{-m}R^{-m}</math>,<math>R</math> 表示基数、<math>a</math> 表示进制的数码。 === 进制 === 十进制 <code>-27.15625</code> 。 <math>(-27.15625)_{10} = ({\color{Green}-1} \times ({\color{Red} 2 \times 10^1 + 7 \times 10^0 + 1 \times 10^{-1} + 5 \times 10^{-2} + 6 \times 10^{-3} + 2 \times 10^{-4} + 5 \times 10^{-5}}))_{10}</math> == [https://standards.ieee.org/ieee/754/6210/ IEEE 754] 标准 == === 数字存储效果图 === 单精度效果图 <pre> 符号 | 指数 | 尾数 sign | exponent(8 bits) | mantissa(23 bits) --------------------------------------------------------------------------------------- 0 | 01111100 | 01000000000000000000000 | = 0.15625 ----------------------------------------------------------------------------------- 31 | 30 ... 23 | 22 ... (bit index) 0 ----------------------------------------------------------------------------------------- S | M | E </pre> 双精度效果图 <pre> sign | exponent(11 bits) | mantissa(52 bits) --------------------------------------------------------------------------------------- 0 | 01111100000 | 0100000000000000000000000000000000000000000000000000 | = 5.820766091346741E-10 ------------------------------------------------------------------------------------------- 63 | 62 ... 52 | 51 ... (bit index) 0 </pre> 除此之外还有半精度浮点数等。 {| class="wikitable" |- ! !! !! 符号位(Sign) !! 指数位(Exponent) !! 尾数位(Fraction) !! 偏移量 !! 精度 |- | 半精度浮点数 || 16 位 || 1 || 5 || 10 || 15 || |- | 单精度浮点数 || 32 位 || 1 || 8 || 23 || 127 || |- | 双精度浮点数 || 64 位 || 1 || 11 || 52 || 1023 || |- | 四精度浮点数 || 128 位 || 1 || 15 || 112 || 16383 || |} 在书写表示浮点数时通常在浮点数前添加 <code>0b</code> 作为标识。比如 <code>0b1100111011001010</code>。 <math>\frac{1}{2^{24}} = \frac{1}{16777216}</math> === 科学计数法 === 使用科学计数法可以方便的表示小数点的位置。科学计数法表达的公式为: <math> \begin{array}{l} value = (-1)^{sign} \times mantissa \times \text{base}^{exponent} \\ \Rightarrow V = (-1)^S \times M \times b^E \\ \begin{cases} 2 \leq base < +\infty, base \in \mathbb{z} \\ 1 \leq mantissa < base \\ exponent \in \mathbb{z} \end{cases} \end{array} </math> 其中 <math>base</math> 为基数(进制),以十进制数字 <code>-27.15625</code> 为例,推导 <math>-27.15625 = (-1)^{1} \times 2.715625 \times 10^{1} = -2.715625E+1</math>。 === 二进制科学计数法 === ==== 公式 ==== 二进制浮点数的标准IEEE中的存储格式进行存储,它的真实值需要通过公式转换获得(二进制科学计数法),<math>V = (-1)^S \times M \times 2^E = (-1)^S \times M \times 2^{e - \text{偏移值}}</math>。其中. # <math>S</math>(sign) 表示符号位,取值范围为 <math>S = 0</math> 或 <math>S = 1</math>。当 <math>S = 0</math> 则 <math>V</math> 为正数,当 <math>S = 1</math> 则 <math>V</math> 为负数。 # <math>M</math>(mantissa) 表示尾数位,也被称之为<code>分数值</code>(fraction)。取值范围是 <math>[1, 2)</math>。 # <math>E</math>(exponent) 表示指数位真值。 # e 为指数位整数,其取值范围是 <math>(0, 2^n - 1)</math>。 # 指数偏移值为 <math>2^{n - 1} - 1</math>。 其中通过拆分 M 可以推导出: <math> \begin{array}{l} V = (-1)^S \times M \times 2^E \\ = (-1)^S \times (1.d_{n-1}d_{n-2} \cdots d_0 )_2 \times 2^E \\ = (-1)^S \times \bigg( 2^0 + \sum_{i = -m}^m d_i \times 2^i \bigg) \times 2^E = (-1)^S \times \bigg( 2^0 + \sum_{i = 1}^m d_{m - i} \times 2^{-i} \bigg) \times 2^E \\ = (-1)^S \times \bigg( 1 + \sum_{i = 1}^m d_{m - i} \times 2^{-i} \bigg) \times 2^E \\ \begin{cases} m = \text{尾数位个数} \end{cases} \end{array} </math> 在进一步拆分 E,可推导出: <math> \begin{array}{l} V = (-1)^S \times M \times 2^E \\ = (-1)^S \times \bigg( 2^0 + \sum_{i = 1}^m d_{m - i} \times 2^{-i} \bigg) \times 2^{e - \text{偏移值}} \\ = (-1)^S \times \bigg( 1 + \sum_{i = 1}^m d_{m - i} \times 2^{-i} \bigg) \times 2^{e - (2^{n - 1} - 1)} \\ \begin{cases} n = \text{指数位个数} \\ m = \text{尾数位个数} \\ e = \sum_{j = 0}^{n} p_{j} \times 2^{j} \end{cases} \end{array} </math> 除此之外,公式还有另外一种解释,确定 M 为尾数,使用二进制的移码,将尾数位的整数转为小数,则此时公式为: <math> \begin{array}{l} V = (-1)^S \times ( \frac{2^m}{2^m} + \frac{M}{2^m} ) \times 2^{e - (2^{n - 1} - 1)} \\ = (-1)^S \times \bigg( \frac{2^m}{2^m} + \bigg(\sum_{i = 0}^{m - 1} d_{i} \times 2^{i} \bigg) \times \frac{1}{2^m} \bigg) \times 2^{e - (2^{n - 1} - 1)} \\ = (-1)^S \times \bigg( 1 + \bigg(\sum_{i = 0}^{m - 1} d_{i} \times 2^{i} \bigg) \times \frac{1}{2^m} \bigg) \times 2^{e - (2^{n - 1} - 1)} \end{array} </math> 用十进制数字 <code>-27.15625</code> 的双精度二进制做示例: <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> 也就是说当我们遇到一个数字的时候,如果想知道它的浮点数,就可以通过公式进行逆向,比如数字<code>-27.15625</code>。将数字<code>-27.15625</code>拆成三部分<code>-</code>、<code>27</code>、<code>0.15625</code>,分别进行十进制转二进制: <math>\begin{aligned} & & \text{逆序排序} \\ \text{整数部分} & 27 \div 2 = 13 & \quad\cdots\quad\text{余 1} & \uparrow \\ & 13 \div 2 = 6 & \quad\cdots\quad\text{余 1} & \mid \\ & 6 \div 2 = 3 & \quad\cdots\quad\text{余 0} & \mid \\ & 3 \div 2 = 1 & \quad\cdots\quad\text{余 1} & \mid \\ & 1 \div 2 = 0 & \quad\cdots\quad\text{余 1} & \mid \end{aligned}</math> <math>\begin{aligned} & & \text{顺序排序} \\ \text{小数部分} & 0.15625 \times 2 = 0.3125 & \quad\cdots\quad\text{取 0} & \mid \\ & 0.3125 \times 2 = 0.625 & \quad\cdots\quad\text{取 0} & \mid \\ & 0.625 \times 2 = {\color{Red} 1.25} & \quad\cdots\quad\text{取 1} & \mid \\ & {\color{Red} 0.25} \times 2 = 0.5 & \quad\cdots\quad\text{取 0} & \mid \\ & 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>\begin{align} (-27.15625)_{10} = (-11011.00101)_2 \\ = (1100111011001010)_{\text{半精度浮点数}} \\ = (11000001110110010100000000000000)_{\text{单精度浮点数}} \\ = (1100000000111011001010000000000000000000000000000000000000000000)_{\text{双精度浮点数}} \end{align}</math> ==== 符号位(S) ==== 0 表示正数,1 表示负数或 0 ==== 指数位(E, 阶码位) ==== 指数位中存在三个参数定义,分别为阶码 E(又称偏移指数)、指数域编码值 e 以及指数偏移值。三者的计算公式为 <math>E = e - (2^{n - 1} - 1)</math>。 '''阶码 E''' 为'''有符号整数''',它的取值范围为<math>[-2^{n-1} + 1, 2^{n-1}]</math>。但是在实际存储指数过程中,需要把阶码 E 转换成'''指数域编码值 e''' '''指数域编码值 e'''为'''无符号整数''',也就是说实际上阶码 E 在内存中以无符号(指数域编码值 e)形式进行存储的。其计算公式为 <math>e = E + \text{偏移值}</math>,取值范围为<math>[0, 2^n - 1]</math>。 '''指数偏移值'''(exponent bias)也被称之为给定移位指数,用于区分正负数字,其值为 <math>2^{n-1}-1</math>,<math>n</math> 为指数位数。并且约定它为中间数对应的'''真值'''(normal number)为 <code>0</code>。以单精度为例,单精度偏移量为 <math>2^{8-1}-1 = 127</math>,就是余 127 码(余码表示法)。假设现有单精度浮点数 E 值为 10(<math>2^{10}</math>),当保存 32 位浮点数到内存时,存入值就是 <math>10 + 127 = 137 = (10001001)_2</math>。 <br> 以上就是关于指数位的几个基本的概念定义。下面说一下其中各参数(阶码与指数域编码)的取值范围的来源。从公式 <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>。这里着重注意取值范围的'''开闭区间'''。 下面着重解释区间内数字的转换,以 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> 计算的公式,<math>(-1)^S \times 1.000000000000000000000 \times 2^{-126} \approx 3.4E+38</math> 在 8 字节双精度中 * <math>(1.\overline{0})_{10} \times 2^{-1022} \approx 2.22507E-308</math> * <math>(2^{52})_{10} \times 2^{1023} \approx 1.79769E308</math> * <math>(2^{-52})_{10} \times 2^{-1022} \approx 4.940656E-324</math> 变量范围为 <math>-2^{1024 } \sim +2^{1024 }</math> 转换为 <math>\pm 2.22507 \times 10^{-308} \sim \pm 1.79769 \times 10^{308}</math>,有效数字为 15 位。 在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>。 ==== 尾数位 (M) ==== 尾数 (mantissa) 是一个二进制小数,用来表示精度。尾数的规格化取值范围为 <math>1 \leq M < 2</math>。 调换一下位置可以更直观,在将尾数存入内存中时,舍弃尾数的整数位,尾数的小数位放入尾数位中,按照不同精度进行修正(裁剪或者填充 <code>0</code>)。 <pre> | 隐藏位(舍弃) | mantissa(52 bits) | exponent(11 bits) | sign | ------------------------------------------------------------------------------------------------------ | 1. | 0100000000000000000000000000000000000000000000000000 | 01111100000 | 0 | --------------------------------------------------------------------------------------------------------- | | 51 ... (bit index) 0 | 62 ... 52 | 63 | </pre> 隐含位、隐藏位亦或者称之为隐藏位表示方法。 == 规约 == === 规约形式的浮点数 === 规约形式的浮点数(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>。 == 进制转换 == === 公式 === 二进制的表示公式为,<math>d = \sum_{i = -n}^m b_i \times 2^i</math>。 十进制的表示公式为,<math>d = \sum_{i = -n}^m b_i \times 10^i</math>。 === 二进制转十进制 === 通过<code>按权相加</code>的方式将二进制数转换为十进制数。公式为 <math>(d_{n} \cdots d_0 {\color{Red}.} \cdots d_{m})_2 = d_n \times 2^{n - 1} + \cdots + d_0 \times 2^0 + \cdots + d_m \times 2^{-m}</math>。 例如 <math>(1010101.1011)_2 = (85.6875)_{10}</math>。 <math>\begin{align} (1010101.1011)_2 = {\color{Red}1 \times 2^6} + 0 \times 2^5 + {\color{Red}1 \times 2^4} + 0 \times 2^3 + {\color{Red}1 \times 2^2} + 0 \times 2^1 + {\color{Red}1 \times 2^0} + {\color{Green}1 \times 2^{-1}} + 0 \times 2^{-2} + {\color{Green}1 \times 2^{-3}} + {\color{Green}1 \times 2^{-4}} \\ = 2^6 + 2^4 + 2^2 + 2^0 + 2^{-1} + 2^{-3} + 2^{-4} \\ = 64 + 16 + 4 + 1 + 0.5 + 0.125 + 0.0625 \\ = 85.6875 \\ \therefore (1010101.1011)_2 = (85.6875)_{10} \end{align}</math> === 整型 === 计算机中整数的使用可分为两种方式:一种只能表示非负数,称为无符号整型数;另一种能够表示负数、0以及正数,称为有符号整型数。 ==== 无符号整型数 ==== ==== 有符号整型数 ==== ===== 机器数 ===== 数字在计算机系统中的二进制表示形式被称之为机器数。 ===== 真值 ===== 机器数对应的真正数值称为机器数的真值。 ===== 原码 ===== 纵观互联网诸多文章的人云亦云中,将原码的取值范围写为 <math>(-2^{n-1}, 2^{n-1})</math> 和 <math>[-2^{n-1} + 1, 2^{n-1} - 1]</math> 两种,那么哪个才是准确的呢?以 8 位原码 <code>00000000</code> 结构为例,其中包含了 1 位符号位和 7 位二进制值位,即 <code>s mmmmmmm</code>。通过公式进行值的计算: <math>V = (-1)^S \times (d_{n}d_{n-1} \cdots d_{1}d_0 )_2 = (-1)^S \times \bigg(\sum_{i = 0}^{n -1} d_i \times 2^i \bigg)</math> 当 n = 7 时,<math>V = (-1)^S \times (d_{6}d_{5}d_{4}d_{3}d_{2}d_{1}d_0 )_2 = (-1)^S \times \bigg(\sum_{i = 0}^6 d_i \times 2^i \bigg)</math> 所以其 7 位二进制值位的取值范围为: <math>\begin{array}{l} [min, max] = [(0000000)_2, (1111111)_2] \\ = \bigg[\sum_{i = 0}^6 0 \times 2^i, \sum_{i = 0}^6 1 \times 2^i \bigg] \\ = [(0 \times 2^6 + 0 \times 2^5 + \cdots + 0 \times 2^0), (1 \times 2^6 + 1 \times 2^5 + \cdots + 1 \times 2^0)] \\ = [0, 127] \end{array}</math> 加上前面的 1 位符号位后并由此得出结论 8 位二进制的表示范围为 <math>[-127, 0] \cup [0, +127] = [-127, 127]</math>。 又因为 <math>\sum_{i = 0}^n 2^i = 2^{n +1} - 1 \Rightarrow \sum_{i = 0}^{n-1} 2^i = 2^n - 1</math> 但是这里的 n 是指<code>二进制值位</code>的位数,如果 n 为<code>符号位+二进制值位</code>就可以表示为 <math>[-2^{n-1} + 1, 2^{n-1} - 1]</math>。由此,在整数范围内,理论上原码的取值范围写为 <math>(-2^{n-1}, 2^{n-1})</math> 和 <math>[-2^{n-1} + 1, 2^{n-1} - 1]</math> 两种写法都是正确的结论。 ===== 反码 ===== 二进制反码在设计上与原码基本一致,但二进制值为原码的取反值,如原码中 <code>-1</code> 为 <code>1000 0001</code>,在反码中为 <code>1111 1110</code>。反码的意义? ===== 补码 ===== 二进制补码的规则是正整数保持不变,负整数二进制值位取反后 <code>+1</code> 进位。同样以 8 位表示的正整数 1 为例 <code>0000 0001</code>,其相反值 -1 位 <pre> 原码 | 0 000 0001 | 补码取反 | 1 111 1110 | +1 | 1 | ----------------------- | 1 111 1111 | </pre> 8 位二进制的表示范围为 <math>[-128, +127]</math> ===== 移码 ===== [https://zh.wikipedia.org/wiki/%E7%A7%BB%E7%A0%81 移码] === 十进制转二进制 === ==== 十进制整数位转二进制 ==== 整数位转二进制,除 2 取余,递进再取整数位除 2 取余,直到商为 0。如,<math>106_{(10)} = 01101010_{(2)}</math>。 <math>\begin{aligned} & & \text{逆序排序} \\ 106 \div 2 = 53 & \quad\cdots\quad\text{余 0} & \uparrow \\ 53 \div 2 = 26 & \quad\cdots\quad\text{余 1} & \mid \\ 26 \div 2 = 13 & \quad\cdots\quad\text{余 0} & \mid \\ 13 \div 2 = 6 & \quad\cdots\quad\text{余 1} & \mid \\ 6 \div 2 = 3 & \quad\cdots\quad\text{余 0} & \mid \\ 3 \div 2 = 1 & \quad\cdots\quad\text{余 1} & \mid \\ 1 \div 2 = 0 & \quad\cdots\quad\text{余 1} & \mid \end{aligned}</math> <div class="pageBoxGreen"> 其中,<math>\color{Green} 1 \div 2 = 0</math>,根据公式 <math>\color{Green} \text{被除数} = \text{除数} \times \text{商} + \text{余数}</math>,推导 <math>\color{Green} 1 = 2 \times 0 + \text{余数}</math>,得出结论余数为 1。 </div> ===== 正整数的转换 ===== 除二取余,倒序(逆序)排列,高位补零。 ===== 负整数的转换 ===== 十进制负整数的绝对值后,转换二进制,各位取反后 +1。 ==== 十进制小数位(浮点数)转二进制 ==== 小数位转二进制,乘 2,递进再取小数位乘 2,直到小数部分为 0。如,(0.125)<sub>10</sub> = (0001)<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{1}\overline{0}\overline{0}\overline{1})_2</math>。 <math>\begin{align} (0.3)_{10} = 0.010011001100110011001 \cdots \\ = 0.0\overline{1}\overline{0}\overline{0}\overline{1} \end{align}</math> == 标题文本 == === 有符号整数 === JavaScript 将数字存储为 64 位浮点数,在进行<code>位</code>的运算之前,先将数字转换为 32 位有符号整数(32 位二进制),执行按位运算后,结果将转换为 64 位浮点数数字。 == 指数 == 指数的边界 <nowiki>(2^-1075, 2^1024)</nowiki> <syntaxhighlight lang="javascript"> Math.pow(2, -1076); // 0 负向溢出 Math.pow(2, 1024); // Infinity 正向溢出</syntaxhighlight> == 端序(字节序) == === 大端序 === === 小端序 === == 其他 == [https://www.h-schmidt.net/FloatConverter/IEEE754.html 浮点数测试工具] == 精度损失 == 1. 经典问题 <code>0.1 + 0.2 = 0.30000000000000004</code>。 将十进制转换为二进制,得出: <math forcemathmode="native"> \begin{array}{l} (0.1)_{10} = (0.0001100110011 \cdots)_2 = (0.00011 \overline{0011})_2 \\ (0.2)_{10} = (0.001100110011 \cdots)_2 = (0.0011 \overline{0011})_2 \end{array} </math> 规范化 <math> \begin{array}{l} 0.1 = (-1)^0 \times 1.1\overline{0}\overline{0}\overline{1}\overline{1} \times 2^-4 \\ 0.2 = (-1)^0 \times 1.1\overline{0}\overline{0}\overline{1}\overline{1} \times 2^-3 \end{array} </math> 推导出 <math> \begin{array}{l} 0.1 \Rightarrow S = 0, M= 1.1 \overline{0}\overline{0}\overline{1}\overline{1}, E = -4 \\ 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 \\ e = (-3 + 1023)_{10} = (1020)_{10} = (1111111100)_2 \end{array} </math> 寄存器 <pre> // 0.1 的存储 0 | 011 1111 1011 | 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1010 ----------------------------------------------------------------------------------- S | M (11位) | E (52位) // 0.2 的存储 0 | 011 1111 1100 | 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1010 ------------------------------------------------------------------------------------------ S | M (11位) | E (52位)</pre> 进行对阶(对 E 进行小阶对大阶) <math> \begin{array}{r} 0.1 = 1.1\overline{0}\overline{0}\overline{1}\overline{1} * 2^{-4} \\ = 0.11\overline{0}\overline{0}\overline{1}\overline{1} * 2^{-3} \\ 0.2 = 1.1\overline{0}\overline{0}\overline{1}\overline{1} \times 2^{-3} \end{array} </math> 二进制加法计算 <pre> | S | E | 隐藏位(舍弃位) | M | 0 | 01111111100 | 0 | 1100110011001100110011001100110011001100110011001101 + | 0 | 01111111100 | 1 | 1001100110011001100110011001100110011001100110011010 ---------------------------------------------------------------------------------- = | 0 | 01111111100 | 10 | 0110011001100110011001100110011001100110011001100111 尾数 M(包含隐藏位)⇒(向右移动)一位,同时 E(阶码)+ 1 | 0 | 01111111100 | 1 | 00110011001100110011001100110011001100110011001100111 + | 0 | 1 | ---------------------------------------------------------------------------------- = | 0 | 01111111101 | 1 | 00110011001100110011001100110011001100110011001100111 尾数 M 超出 52 位进行 0 舍 1 入,舍弃值为 0 保持 M 中 52 位不变,舍弃值为 1 则将舍弃值与 M 中 52 位进行二进制加法运算 | 0 | 01111111101 | 1 | 0011001100110011001100110011001100110011001100110011 + 1 ---------------------------------------------------------------------------------- = | 0 | 01111111101 | 1 | 0011001100110011001100110011001100110011001100110100 </pre> 推导出: <math> \begin{array}{l} \because \quad \begin{cases} S = 0 \\ E = -2 \\ M = 1.0011001100110011001100110011001100110011001100110100 \end{cases} \\ \therefore \quad (-1)^0 \times (1.0011001100110011001100110011001100110011001100110100)_2 \times 2^{-2} \\ = (0.010011001100110011001100110011001100110011001100110100)_2 \\ \Rightarrow (0 \times 2^{-1} + {\color{Red}1 \times 2^{-2}} + 0 \times 2^{-3} + 0 \times 2^{-4} + {\color{Red}1 \times 2^{-5}} + \cdots + {\color{Red}1 \times 2^{-50}} + 0 \times 2^{-51} + 0 \times 2^{-52})_{10} \\ = (0.300000000000000044408 \cdots)_{10} \\ = (3.0000000000000004E-1)_{10} \end{array} </math> == 精度相同 == <math>1.4073748835532801 \times 10^{14} \approx 1.40737488355328 \times 10^{14}</math> <pre> 0 10000101110 0000000000000000000000000000000000000000000000000000</pre> == ULP == ULP(Unit in the Last Place) == 精确度与准确度 == == 近似相等性 == == 浮点异常 == === 无效操作 === === 被零除 === === 上溢 === === 下溢 === === 不精确计算 === == 参考文献 == # [https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html What Every Computer Scientist Should Know About Floating-Point Arithmetic] # [https://stackoverflow.com/questions/588004/is-floating-point-math-broken/588014#588014 Is floating-point math broken?] # [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 每位计算机科学家都应该了解的浮点运算]
返回
JavaScript/双精度浮点数
。
导航菜单
康健生活
搜索
user-page
通知
user-interface-preferences
个人工具
登录
五一
多读书,多运动,多到远处走走,再看看不一样的世界。