This commit is contained in:
krahets
2023-08-22 13:50:24 +08:00
parent 77b90cd19b
commit b70b7c9e75
67 changed files with 580 additions and 580 deletions

View File

@ -3424,9 +3424,9 @@
<li><strong>反码</strong>:正数的反码与其原码相同,负数的反码是对其原码除符号位外的所有位取反。</li>
<li><strong>补码</strong>:正数的补码与其原码相同,负数的补码是在其反码的基础上加 <span class="arithmatex">\(1\)</span></li>
</ul>
<p>图展示了原吗、反码和补码之间的转换方法。</p>
<p> 3-4 展示了原吗、反码和补码之间的转换方法。</p>
<p><img alt="原码、反码与补码之间的相互转换" src="../number_encoding.assets/1s_2s_complement.png" /></p>
<p align="center">原码、反码与补码之间的相互转换 </p>
<p align="center"> 3-4 &nbsp; 原码、反码与补码之间的相互转换 </p>
<p>「原码 true form」虽然最直观但存在一些局限性。一方面<strong>负数的原码不能直接用于运算</strong>。例如在原码下计算 <span class="arithmatex">\(1 + (-2)\)</span> ,得到的结果是 <span class="arithmatex">\(-3\)</span> ,这显然是不对的。</p>
<div class="arithmatex">\[
@ -3508,16 +3508,16 @@ b_{31} b_{30} b_{29} \ldots b_2 b_1 b_0
\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 align="center"> 3-5 &nbsp; 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>观察图 3-5 ,给定一个示例数据 <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 align="center">指数位含义 </p>
<p>如表 3-2 所示,指数位 <span class="arithmatex">\(E = 0\)</span><span class="arithmatex">\(E = 255\)</span> 具有特殊含义,<strong>用于表示零、无穷大、<span class="arithmatex">\(\mathrm{NaN}\)</span></strong></p>
<p align="center"> 3-2 &nbsp; 指数位含义 </p>
<div class="center-table">
<table>