mirror of
https://github.com/krahets/hello-algo.git
synced 2025-07-28 12:52:57 +08:00
deploy
This commit is contained in:
@ -3448,7 +3448,7 @@
|
||||
</div>
|
||||
<h2 id="331">3.3.1 原码、反码和补码<a class="headerlink" href="#331" title="Permanent link">¶</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>
|
||||
@ -3516,7 +3516,7 @@
|
||||
<div class="arithmatex">\[
|
||||
b_{31} b_{30} b_{29} \ldots b_2 b_1 b_0
|
||||
\]</div>
|
||||
<p>根据 IEEE 754 标准,32-bit 长度的 <code>float</code> 由以下部分构成:</p>
|
||||
<p>根据 IEEE 754 标准,32-bit 长度的 <code>float</code> 由以下三个部分构成。</p>
|
||||
<ul>
|
||||
<li>符号位 <span class="arithmatex">\(\mathrm{S}\)</span> :占 1 bit ,对应 <span class="arithmatex">\(b_{31}\)</span> 。</li>
|
||||
<li>指数位 <span class="arithmatex">\(\mathrm{E}\)</span> :占 8 bits ,对应 <span class="arithmatex">\(b_{30} b_{29} \ldots b_{23}\)</span> 。</li>
|
||||
@ -3533,7 +3533,7 @@ b_{31} b_{30} b_{29} \ldots b_2 b_1 b_0
|
||||
<p>其中各项的取值范围:</p>
|
||||
<div class="arithmatex">\[
|
||||
\begin{aligned}
|
||||
\mathrm{S} \in & \{ 0, 1\} , \quad \mathrm{E} \in \{ 1, 2, \dots, 254 \} \newline
|
||||
\mathrm{S} \in & \{ 0, 1\}, \quad \mathrm{E} \in \{ 1, 2, \dots, 254 \} \newline
|
||||
(1 + \mathrm{N}) = & (1 + \sum_{i=1}^{23} b_{23-i} 2^{-i}) \subset [1, 2 - 2^{-23}]
|
||||
\end{aligned}
|
||||
\]</div>
|
||||
@ -3581,12 +3581,8 @@ b_{31} b_{30} b_{29} \ldots b_2 b_1 b_0
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
<p>特别地,次正规数显著提升了浮点数的精度,这是因为:</p>
|
||||
<ul>
|
||||
<li>最小正正规数为 <span class="arithmatex">\(2^{-126} \approx 1.18 \times 10^{-38}\)</span> 。</li>
|
||||
<li>最小正次正规数为 <span class="arithmatex">\(2^{-126} \times 2^{-23} \approx 1.4 \times 10^{-45}\)</span> 。</li>
|
||||
</ul>
|
||||
<p>双精度 <code>double</code> 也采用类似 <code>float</code> 的表示方法,此处不再详述。</p>
|
||||
<p>值得说明的是,次正规数显著提升了浮点数的精度。最小正正规数为 <span class="arithmatex">\(2^{-126}\)</span> ,最小正次正规数为 <span class="arithmatex">\(2^{-126} \times 2^{-23}\)</span> 。</p>
|
||||
<p>双精度 <code>double</code> 也采用类似 <code>float</code> 的表示方法,在此不做赘述。</p>
|
||||
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user