mirror of
https://github.com/fofolee/uTools-Manuals.git
synced 2025-06-09 23:44:06 +08:00
126 lines
7.6 KiB
HTML
126 lines
7.6 KiB
HTML
<article id="wikiArticle">
|
||
<div></div>
|
||
<h2 id="Summary" name="Summary">概述</h2>
|
||
<p><strong><code>Math.fround()</code></strong> 可以将任意的数字转换为离它最近的<a class="external" href="https://en.wikipedia.org/wiki/Single-precision_floating-point_format" rel="noopener" title="link to the wikipedia page on single-precision floating-point format">单精度浮点数</a>形式的数字。</p>
|
||
<h2 id="Syntax" name="Syntax">语法</h2>
|
||
<pre><code class="language-javascript"><code>Math.fround(<var>doubleFloat</var>)</code></code></pre>
|
||
<h3 id="Parameters" name="Parameters">参数</h3>
|
||
<dl>
|
||
<dt><code>doubleFloat</code></dt>
|
||
<dd>一个 <a href="Reference/Global_Objects/Number" title="JavaScript 的 Number 对象是经过封装的能让你处理数字值的对象。Number 对象由 Number() 构造器创建。"><code>Number</code></a>。若参数为非数字类型,则会被转投成数字。无法转换时,设置成<a href="Reference/Global_Objects/NaN" title="全局属性 NaN 的值表示不是一个数字(Not-A-Number)。"><code>NaN</code></a>。</dd>
|
||
</dl>
|
||
<h3 id="Parameters" name="Parameters">返回值</h3>
|
||
<p>指定数字最接近的<a class="external" href="https://en.wikipedia.org/wiki/Single-precision_floating-point_format" rel="noopener">32位单精度</a>浮点数表示。</p>
|
||
<h2 id="Examples" name="Examples">描述</h2>
|
||
<p>JavaScript 内部使用64位的双浮点数字,支持很高的精度。但是,有时你需要用32位浮点数字,比如你从一个<a href="Reference/Global_Objects/Float32Array" title="Float32Array 类型数组代表的是平台字节顺序为32位的浮点数型数组(对应于 C 浮点数据类型) 。 如果需要控制字节顺序, 使用 DataView 替代。其内容初始化为0。一旦建立起来,你可以使用这个对象的方法对其元素进行操作,或者使用标准数组索引语法 (使用方括号)。"><code>Float32Array</code></a> 读取值时。这时会产生混乱:检查一个64位浮点数和一个32位浮点数是否相等会失败,即使二个数字几乎一模一样。</p>
|
||
<p>要解决这个问题,可以使用 <code>Math.fround()</code> 来将64位的浮点数转换为32位浮点数。在内部,JavaScript 继续把这个数字作为64位浮点数看待,仅仅是在尾数部分的第23位执行了“舍入到偶数”的操作,并将后续的尾数位设置为0。如果数字超出32位浮点数的范围,则返回 <a href="Reference/Global_Objects/Infinity" title="全局属性 Infinity 是一个数值,表示无穷大。"><code>Infinity</code></a> 或 <code>-Infinity</code>。</p>
|
||
<p>因为<code>fround()</code> 是<code>Math</code> 的静态方法,你必须通过 <code>Math.fround()</code> 来使用,而不是调用你创建的<code>Math</code> 对象的一个实例方法(<code>Math</code>不是一个构造函数)。</p>
|
||
<h2 id="Examples" name="Examples">示例</h2>
|
||
<h3 id="使用_Math.fround()">使用 Math.fround()</h3>
|
||
<p>数字1.5可以在二进制数字系统中精确表示,32位和64位的值相同:</p>
|
||
<pre><code class="language-javascript">Math.fround(1.5); // 1.5
|
||
Math.fround(1.5) === 1.5; // true</code></pre>
|
||
<p>但是,数字1.337却无法在二进制数字系统中精确表示,所以32位和64位的值是不同的:</p>
|
||
<pre><code class="language-javascript">Math.fround(1.337); // 1.3370000123977661
|
||
Math.fround(1.337) === 1.337; // false
|
||
</code></pre>
|
||
<p>2<sup>150</sup> 超出32位浮点,所以返回<code>Infinity</code>:</p>
|
||
<pre><code class="language-javascript">2 ** 150; // 1.42724769270596e+45
|
||
Math.fround(2 ** 150); // Infinity
|
||
</code></pre>
|
||
<p>如果参数无法转换成数字,或者为 <a href="Reference/Global_Objects/NaN" title="全局属性 NaN 的值表示不是一个数字(Not-A-Number)。"><code>NaN</code></a> (<code>NaN</code>),<code>Math.fround()</code> 会返回 <code>NaN</code>:</p>
|
||
<pre><code class="language-javascript">Math.fround('abc'); // NaN
|
||
Math.fround(NaN); // NaN
|
||
</code></pre>
|
||
<p>在某些精度不高的场合下,可以通过将二个浮点数转换成32位浮点数进行比较,以解决64位浮点数比较结果不正确的问题:</p>
|
||
<pre><code class="language-javascript">0.1 + 0.2 == 0.3; //false
|
||
|
||
function equal(v1, v2) {
|
||
return Math.fround(v1) == Math.fround(v2);
|
||
}
|
||
|
||
equal(0.1 + 0.2, 0.3); //true
|
||
</code></pre>
|
||
<h2 id="Polyfill" name="Polyfill">Polyfill</h2>
|
||
<p>下面的函数可以模拟这个 API,前提是浏览器必须已经支持 <a href="Reference/Global_Objects/Float32Array" title="Float32Array 类型数组代表的是平台字节顺序为32位的浮点数型数组(对应于 C 浮点数据类型) 。 如果需要控制字节顺序, 使用 DataView 替代。其内容初始化为0。一旦建立起来,你可以使用这个对象的方法对其元素进行操作,或者使用标准数组索引语法 (使用方括号)。"><code>Float32Array</code></a>:</p>
|
||
<pre><code class="language-javascript">Math.fround = Math.fround || (function (array) {
|
||
return function(x) {
|
||
return array[0] = x, array[0];
|
||
};
|
||
})(new Float32Array(1));</code></pre>
|
||
<h2 id="Specifications" name="Specifications">规范</h2>
|
||
<table class="standard-table">
|
||
<tbody>
|
||
<tr>
|
||
<th scope="col">规范名称</th>
|
||
<th scope="col">规范状态</th>
|
||
</tr>
|
||
<tr>
|
||
<td><a class="external" href="https://www.ecma-international.org/ecma-262/6.0/#sec-math.fround" hreflang="en" lang="en" rel="noopener">ECMAScript 2015 (6th Edition, ECMA-262)<br/><small lang="zh-CN">Math.fround</small></a></td>
|
||
<td><span class="spec-Standard">Standard</span></td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
<h2 id="Browser_compatibility" name="Browser_compatibility">浏览器兼容性</h2>
|
||
<div><div class="blockIndicator warning"><strong><a class="external" href="https://github.com/mdn/browser-compat-data" rel="noopener">We're converting our compatibility data into a machine-readable JSON format</a></strong>.
|
||
This compatibility table still uses the old format,
|
||
because we haven't yet converted the data it contains.
|
||
<strong><a class="new" href="/zh-CN/docs/MDN/Contribute/Structures/Compatibility_tables" rel="nofollow">Find out how you can help!</a></strong></div>
|
||
<div class="htab">
|
||
<a id="AutoCompatibilityTable" name="AutoCompatibilityTable"></a>
|
||
<ul>
|
||
<li class="selected"><a>Desktop</a></li>
|
||
<li><a>Mobile</a></li>
|
||
</ul>
|
||
</div></div>
|
||
<div id="compat-desktop">
|
||
<table class="compat-table">
|
||
<tbody>
|
||
<tr>
|
||
<th>Feature</th>
|
||
<th>Chrome</th>
|
||
<th>Firefox (Gecko)</th>
|
||
<th>Internet Explorer</th>
|
||
<th>Opera</th>
|
||
<th>Safari</th>
|
||
</tr>
|
||
<tr>
|
||
<td>Basic support</td>
|
||
<td>38</td>
|
||
<td><a href="/en-US/Firefox/Releases/26" title="Released on 2013-12-10.">26</a> (26)</td>
|
||
<td><span style="color: #f00;">未实现</span></td>
|
||
<td>25</td>
|
||
<td>7.1</td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
</div>
|
||
<div id="compat-mobile">
|
||
<table class="compat-table">
|
||
<tbody>
|
||
<tr>
|
||
<th>Feature</th>
|
||
<th>Android</th>
|
||
<th>Chrome for Android</th>
|
||
<th>Firefox Mobile (Gecko)</th>
|
||
<th>IE Mobile</th>
|
||
<th>Opera Mobile</th>
|
||
<th>Safari Mobile</th>
|
||
</tr>
|
||
<tr>
|
||
<td>Basic support</td>
|
||
<td><span style="color: #f00;">未实现</span></td>
|
||
<td><span style="color: rgb(255, 153, 0);" title="Compatibility unknown; please update this.">?</span></td>
|
||
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
|
||
<td><span style="color: #f00;">未实现</span></td>
|
||
<td><span style="color: #f00;">未实现</span></td>
|
||
<td>iOS 8</td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
</div>
|
||
<h2 id="See_also" name="See_also">相关链接</h2>
|
||
<ul>
|
||
<li><a href="Reference/Global_Objects/Math/round" title="Math.round() 函数返回一个数字四舍五入后最接近的整数。"><code>Math.round()</code></a></li>
|
||
</ul>
|
||
</article> |