This commit is contained in:
krahets
2023-08-21 19:32:49 +08:00
parent c0f960b443
commit c359c07fe0
67 changed files with 443 additions and 442 deletions

View File

@ -3418,12 +3418,13 @@
</div>
<h2 id="331">3.3.1 &nbsp; 原码、反码和补码<a class="headerlink" href="#331" title="Permanent link">&para;</a></h2>
<p>在上一节的表格中我们发现,所有整数类型能够表示的负数都比正数多一个,例如 <code>byte</code> 的取值范围是 <span class="arithmatex">\([-128, 127]\)</span> 。这个现象比较反直觉,它的内在原因涉及到原码、反码、补码的相关知识。</p>
<p>实际上<strong>数字是以“补码”的形式存储在计算机中的</strong>。在分析这样做的原因之前,我们首先给出三者的定义:</p>
<p>首先需要指出<strong>数字是以“补码”的形式存储在计算机中的</strong>。在分析这样做的原因之前,我们首先给出三者的定义:</p>
<ul>
<li><strong>原码</strong>:我们将数字的二进制表示的最高位视为符号位,其中 <span class="arithmatex">\(0\)</span> 表示正数,<span class="arithmatex">\(1\)</span> 表示负数,其余位表示数字的值。</li>
<li><strong>反码</strong>:正数的反码与其原码相同,负数的反码是对其原码除符号位外的所有位取反。</li>
<li><strong>补码</strong>:正数的补码与其原码相同,负数的补码是在其反码的基础上加 <span class="arithmatex">\(1\)</span></li>
</ul>
<p>下图展示了原吗、反码和补码之间的转换方法。</p>
<p><img alt="原码、反码与补码之间的相互转换" src="../number_encoding.assets/1s_2s_complement.png" /></p>
<p align="center"> 图:原码、反码与补码之间的相互转换 </p>
@ -3431,9 +3432,9 @@
<div class="arithmatex">\[
\begin{aligned}
&amp; 1 + (-2) \newline
&amp; = 0000 \space 0001 + 1000 \space 0010 \newline
&amp; \rightarrow 0000 \space 0001 + 1000 \space 0010 \newline
&amp; = 1000 \space 0011 \newline
&amp; = -3
&amp; \rightarrow -3
\end{aligned}
\]</div>
<p>为了解决此问题,计算机引入了「反码 1's complement code」。如果我们先将原码转换为反码并在反码下计算 <span class="arithmatex">\(1 + (-2)\)</span> ,最后将结果从反码转化回原码,则可得到正确结果 <span class="arithmatex">\(-1\)</span></p>
@ -3450,14 +3451,14 @@
<p>另一方面,<strong>数字零的原码有 <span class="arithmatex">\(+0\)</span><span class="arithmatex">\(-0\)</span> 两种表示方式</strong>。这意味着数字零对应着两个不同的二进制编码,其可能会带来歧义。比如在条件判断中,如果没有区分正零和负零,则可能会导致判断结果出错。而如果我们想要处理正零和负零歧义,则需要引入额外的判断操作,其可能会降低计算机的运算效率。</p>
<div class="arithmatex">\[
\begin{aligned}
+0 &amp; = 0000 \space 0000 \newline
-0 &amp; = 1000 \space 0000
+0 &amp; \rightarrow 0000 \space 0000 \newline
-0 &amp; \rightarrow 1000 \space 0000
\end{aligned}
\]</div>
<p>与原码一样,反码也存在正负零歧义问题,因此计算机进一步引入了「补码 2's complement code」。我们先来观察一下负零的原码、反码、补码的转换过程</p>
<div class="arithmatex">\[
\begin{aligned}
-0 = \space &amp; 1000 \space 0000 \space \text{(原码)} \newline
-0 \rightarrow \space &amp; 1000 \space 0000 \space \text{(原码)} \newline
= \space &amp; 1111 \space 1111 \space \text{(反码)} \newline
= 1 \space &amp; 0000 \space 0000 \space \text{(补码)} \newline
\end{aligned}
@ -3506,16 +3507,16 @@ b_{31} b_{30} b_{29} \ldots b_2 b_1 b_0
(1 + \mathrm{N}) = &amp; (1 + \sum_{i=1}^{23} b_{23-i} 2^{-i}) \subset [1, 2 - 2^{-23}]
\end{aligned}
\]</div>
<p><img alt="IEEE 754 标准下的 float 表示方式" src="../number_encoding.assets/ieee_754_float.png" /></p>
<p align="center">IEEE 754 标准下的 float 表示方式 </p>
<p><img alt="IEEE 754 标准下的 float 的计算示例" src="../number_encoding.assets/ieee_754_float.png" /></p>
<p align="center">IEEE 754 标准下的 float 的计算示例 </p>
<p>给定一个示例数据 <span class="arithmatex">\(\mathrm{S} = 0\)</span> <span class="arithmatex">\(\mathrm{E} = 124\)</span> <span class="arithmatex">\(\mathrm{N} = 2^{-2} + 2^{-3} = 0.375\)</span> ,则有:</p>
<p>观察上图,给定一个示例数据 <span class="arithmatex">\(\mathrm{S} = 0\)</span> <span class="arithmatex">\(\mathrm{E} = 124\)</span> <span class="arithmatex">\(\mathrm{N} = 2^{-2} + 2^{-3} = 0.375\)</span> ,则有:</p>
<div class="arithmatex">\[
\text { val } = (-1)^0 \times 2^{124 - 127} \times (1 + 0.375) = 0.171875
\]</div>
<p>现在我们可以回答最初的问题:<strong><code>float</code> 的表示方式包含指数位,导致其取值范围远大于 <code>int</code></strong> 。根据以上计算,<code>float</code> 可表示的最大正数为 <span class="arithmatex">\(2^{254 - 127} \times (2 - 2^{-23}) \approx 3.4 \times 10^{38}\)</span> ,切换符号位便可得到最小负数。</p>
<p><strong>尽管浮点数 <code>float</code> 扩展了取值范围,但其副作用是牺牲了精度</strong>。整数类型 <code>int</code> 将全部 32 位用于表示数字,数字是均匀分布的;而由于指数位的存在,浮点数 <code>float</code> 的数值越大,相邻两个数字之间的差值就会趋向越大。</p>
<p>进一步地,指数位 <span class="arithmatex">\(E = 0\)</span><span class="arithmatex">\(E = 255\)</span> 具有特殊含义,<strong>用于表示零、无穷大、<span class="arithmatex">\(\mathrm{NaN}\)</span></strong></p>
<p>如下表所示,指数位 <span class="arithmatex">\(E = 0\)</span><span class="arithmatex">\(E = 255\)</span> 具有特殊含义,<strong>用于表示零、无穷大、<span class="arithmatex">\(\mathrm{NaN}\)</span></strong></p>
<p align="center"> 表:指数位含义 </p>
<div class="center-table">