This commit is contained in:
krahets 2023-02-22 19:28:44 +08:00
parent 2e87397650
commit 8d6f7336e8
3 changed files with 11 additions and 5 deletions

View File

@ -1720,8 +1720,8 @@
</ol> </ol>
<p>其它整数类型 byte, short, long 取值范围的计算方法与 int 类似,在此不再赘述。</p> <p>其它整数类型 byte, short, long 取值范围的计算方法与 int 类似,在此不再赘述。</p>
<h3 id="_2">浮点数表示方式 *<a class="headerlink" href="#_2" title="Permanent link">&para;</a></h3> <h3 id="_2">浮点数表示方式 *<a class="headerlink" href="#_2" title="Permanent link">&para;</a></h3>
<p>细心的你可能会疑惑: int 和 float 长度相同,都是 4 bytes <strong>但为什么 float 的取值范围远大于 int</strong> 这是因为浮点数 float 采用了不同的表示方式</p> <p>细心的你可能会疑惑: int 和 float 长度相同,都是 4 bytes <strong>但为什么 float 的取值范围远大于 int</strong> 按说 float 需要表示小数,取值范围应该变小才对</p>
<p>IEEE 754 标准规定32-bit 长度的 float 由以下部分构成:</p> <p>其实,这是因为浮点数 float 采用了不同的表示方式。IEEE 754 标准规定32-bit 长度的 float 由以下部分构成:</p>
<ul> <ul>
<li>符号位 <span class="arithmatex">\(\mathrm{S}\)</span> :占 1 bit </li> <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{E}\)</span> :占 8 bits </li>
@ -1735,13 +1735,19 @@
<div class="arithmatex">\[ <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> \]</div>
<p>其中 <span class="arithmatex">\(\mathrm{S} \in \{-1, 1\}\)</span> , <span class="arithmatex">\(\mathrm{E} \in \{ 1, 2, \dots, 254 \}\)</span> , <span class="arithmatex">\((1 + \mathrm{N}) = 1+\sum_{i=1}^{23} b_{23-i} 2^{-i} \subset [1, 2 - 2^{-23}]\)</span></p> <p>其中各项的取值范围为</p>
<div class="arithmatex">\[
\begin{aligned}
\mathrm{S} \in &amp; \{ 0, 1\} , \quad \mathrm{E} \in \{ 1, 2, \dots, 254 \} \newline
(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="../data_and_memory.assets/IEEE-754-float.png" /></p> <p><img alt="IEEE-754-float" src="../data_and_memory.assets/IEEE-754-float.png" /></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">\[ <div class="arithmatex">\[
\text { val } = (-1)^0 \times 2^{124 - 127} \times (1 + 0.375) = 0.171875 \text { val } = (-1)^0 \times 2^{124 - 127} \times (1 + 0.375) = 0.171875
\]</div> \]</div>
<p>现在我们可以回答开始的问题:<strong>float 的表示方式包含指数位,导致其取值范围远大于 int</strong> 。根据以上计算, float 可表示的最大正数为 <span class="arithmatex">\(2^{127} \times (2 - 2^{-23}) \approx 3.4 \times 10^{38}\)</span> ,切换符号位便可得到最小负数。</p> <p>现在我们可以回答开始的问题:<strong>float 的表示方式包含指数位,导致其取值范围远大于 int</strong> 。根据以上计算, float 可表示的最大正数为 <span class="arithmatex">\(2^{254 - 127} \times (2 - 2^{-23}) \approx 3.4 \times 10^{38}\)</span> ,切换符号位便可得到最小负数。</p>
<p><strong>浮点数 float 虽然拓展了取值范围,但副作用是牺牲了精度</strong>。整数类型 int 将全部 32 位用于表示数字,数字是均匀分布的;而由于指数位的存在,浮点数 float 的数值越大,相邻两个数字之间的差值就会趋向越大。</p> <p><strong>浮点数 float 虽然拓展了取值范围,但副作用是牺牲了精度</strong>。整数类型 int 将全部 32 位用于表示数字,数字是均匀分布的;而由于指数位的存在,浮点数 float 的数值越大,相邻两个数字之间的差值就会趋向越大。</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>
<table> <table>

File diff suppressed because one or more lines are too long

Binary file not shown.