mirror of
https://github.com/krahets/hello-algo.git
synced 2025-07-13 18:56:35 +08:00
deploy
This commit is contained in:
@ -3443,21 +3443,25 @@
|
||||
<p>补码的设计非常精妙,由于篇幅关系我们先介绍到这里。建议有兴趣的读者进一步深度了解。</p>
|
||||
<h2 id="332">3.3.2. 浮点数编码<a class="headerlink" href="#332" title="Permanent link">¶</a></h2>
|
||||
<p>细心的你可能会发现:<code>int</code> 和 <code>float</code> 长度相同,都是 4 bytes,但为什么 <code>float</code> 的取值范围远大于 <code>int</code> ?这非常反直觉,因为按理说 <code>float</code> 需要表示小数,取值范围应该变小才对。</p>
|
||||
<p>实际上,这是因为浮点数 <code>float</code> 采用了不同的表示方式。根据 IEEE 754 标准,32-bit 长度的 <code>float</code> 由以下部分构成:</p>
|
||||
<p>实际上,<strong>这是因为浮点数 <code>float</code> 采用了不同的表示方式</strong>。记一个 32-bit 长度的二进制数为:</p>
|
||||
<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>
|
||||
<ul>
|
||||
<li>符号位 <span class="arithmatex">\(\mathrm{S}\)</span> :占 1 bit 。</li>
|
||||
<li>指数位 <span class="arithmatex">\(\mathrm{E}\)</span> :占 8 bits 。</li>
|
||||
<li>分数位 <span class="arithmatex">\(\mathrm{N}\)</span> :占 24 bits ,其中 23 位显式存储。</li>
|
||||
<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>
|
||||
<li>分数位 <span class="arithmatex">\(\mathrm{N}\)</span> :占 23 bits ,对应 <span class="arithmatex">\(b_{22} b_{21} \ldots b_0\)</span> 。</li>
|
||||
</ul>
|
||||
<p>设 32-bit 二进制数的第 <span class="arithmatex">\(i\)</span> 位为 <span class="arithmatex">\(b_i\)</span> ,则 <code>float</code> 值的计算方法定义为:</p>
|
||||
<p>二进制数 <code>float</code> 对应的值的计算方法:</p>
|
||||
<div class="arithmatex">\[
|
||||
\text { val } = (-1)^{b_{31}} \times 2^{\left(b_{30} b_{29} \ldots b_{23}\right)_2-127} \times\left(1 . b_{22} b_{21} \ldots b_0\right)_2
|
||||
\text {val} = (-1)^{b_{31}} \times 2^{\left(b_{30} b_{29} \ldots b_{23}\right)_2-127} \times\left(1 . b_{22} b_{21} \ldots b_0\right)_2
|
||||
\]</div>
|
||||
<p>转化到十进制下的计算公式为</p>
|
||||
<p>转化到十进制下的计算公式:</p>
|
||||
<div class="arithmatex">\[
|
||||
\text { val }=(-1)^{\mathrm{S}} \times 2^{\mathrm{E} -127} \times (1 + \mathrm{N})
|
||||
\text {val}=(-1)^{\mathrm{S}} \times 2^{\mathrm{E} -127} \times (1 + \mathrm{N})
|
||||
\]</div>
|
||||
<p>其中各项的取值范围为</p>
|
||||
<p>其中各项的取值范围:</p>
|
||||
<div class="arithmatex">\[
|
||||
\begin{aligned}
|
||||
\mathrm{S} \in & \{ 0, 1\} , \quad \mathrm{E} \in \{ 1, 2, \dots, 254 \} \newline
|
||||
|
Reference in New Issue
Block a user