aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/global_objects/math/fround
diff options
context:
space:
mode:
authorPeter Bengtsson <mail@peterbe.com>2020-12-08 14:40:17 -0500
committerPeter Bengtsson <mail@peterbe.com>2020-12-08 14:40:17 -0500
commit33058f2b292b3a581333bdfb21b8f671898c5060 (patch)
tree51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/web/javascript/reference/global_objects/math/fround
parent8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff)
downloadtranslated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz
translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2
translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip
initial commit
Diffstat (limited to 'files/zh-cn/web/javascript/reference/global_objects/math/fround')
-rw-r--r--files/zh-cn/web/javascript/reference/global_objects/math/fround/index.html154
1 files changed, 154 insertions, 0 deletions
diff --git a/files/zh-cn/web/javascript/reference/global_objects/math/fround/index.html b/files/zh-cn/web/javascript/reference/global_objects/math/fround/index.html
new file mode 100644
index 0000000000..022e84dbb6
--- /dev/null
+++ b/files/zh-cn/web/javascript/reference/global_objects/math/fround/index.html
@@ -0,0 +1,154 @@
+---
+title: Math.fround()
+slug: Web/JavaScript/Reference/Global_Objects/Math/fround
+translation_of: Web/JavaScript/Reference/Global_Objects/Math/fround
+---
+<div>{{JSRef("Global_Objects", "Math")}}</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" title="link to the wikipedia page on single-precision floating-point format">单精度浮点数</a>形式的数字。</p>
+
+<h2 id="Syntax" name="Syntax">语法</h2>
+
+<pre class="brush: js"><code>Math.fround(<var>doubleFloat</var>)</code></pre>
+
+<h3 id="Parameters" name="Parameters">参数</h3>
+
+<dl>
+ <dt><code>doubleFloat</code></dt>
+ <dd>一个 {{jsxref("Number")}}。若参数为非数字类型,则会被转投成数字。无法转换时,设置成{{jsxref("NaN")}}。</dd>
+</dl>
+
+<h3 id="Parameters" name="Parameters">返回值</h3>
+
+<p>指定数字最接近的<a href="https://en.wikipedia.org/wiki/Single-precision_floating-point_format">32位单精度</a>浮点数表示。</p>
+
+<h2 id="Examples" name="Examples">描述</h2>
+
+<p>JavaScript 内部使用64位的双浮点数字,支持很高的精度。但是,有时你需要用32位浮点数字,比如你从一个{{jsxref("Float32Array")}} 读取值时。这时会产生混乱:检查一个64位浮点数和一个32位浮点数是否相等会失败,即使二个数字几乎一模一样。</p>
+
+<p>要解决这个问题,可以使用 <code>Math.fround()</code> 来将64位的浮点数转换为32位浮点数。在内部,JavaScript 继续把这个数字作为64位浮点数看待,仅仅是在尾数部分的第23位执行了“舍入到偶数”的操作,并将后续的尾数位设置为0。如果数字超出32位浮点数的范围,则返回 {{jsxref("Infinity")}} 或 <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 class="brush: js">Math.fround(1.5); // 1.5
+Math.fround(1.5) === 1.5; // true</pre>
+
+<p>但是,数字1.337却无法在二进制数字系统中精确表示,所以32位和64位的值是不同的:</p>
+
+<pre class="brush: js">Math.fround(1.337); // 1.3370000123977661
+Math.fround(1.337) === 1.337; // false
+</pre>
+
+<p>2<sup>150</sup> 超出32位浮点,所以返回<code>Infinity</code>:</p>
+
+<pre class="brush: js">2 ** 150; // 1.42724769270596e+45
+Math.fround(2 ** 150); // Infinity
+</pre>
+
+<p>如果参数无法转换成数字,或者为 {{jsxref("NaN")}} (<code>NaN</code>),<code>Math.fround()</code> 会返回 <code>NaN</code>:</p>
+
+<pre class="brush: js">Math.fround('abc'); // NaN
+Math.fround(NaN); // NaN
+</pre>
+
+<p>在某些精度不高的场合下,可以通过将二个浮点数转换成32位浮点数进行比较,以解决64位浮点数比较结果不正确的问题:</p>
+
+<pre class="brush: js">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
+</pre>
+
+<h2 id="Polyfill" name="Polyfill">Polyfill</h2>
+
+<p>下面的函数可以模拟这个 API,前提是浏览器必须已经支持 {{jsxref("Float32Array")}}:</p>
+
+<pre class="brush: js">Math.fround = Math.fround || (function (array) {
+ return function(x) {
+ return array[0] = x, array[0];
+ };
+})(new Float32Array(1));</pre>
+
+<h2 id="Specifications" name="Specifications">规范</h2>
+
+<table class="standard-table">
+ <tbody>
+ <tr>
+ <th scope="col">规范名称</th>
+ <th scope="col">规范状态</th>
+ </tr>
+ <tr>
+ <td>{{SpecName('ES6', '#sec-math.fround', 'Math.fround')}}</td>
+ <td>{{Spec2('ES6')}}</td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="Browser_compatibility" name="Browser_compatibility">浏览器兼容性</h2>
+
+<div>{{CompatibilityTable}}</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>{{CompatChrome("38")}}</td>
+ <td>{{CompatGeckoDesktop("26")}}</td>
+ <td>{{CompatNo}}</td>
+ <td>{{CompatOpera("25")}}</td>
+ <td>{{CompatSafari("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>{{CompatNo}}</td>
+ <td>{{CompatUnknown}}</td>
+ <td>{{CompatVersionUnknown}}</td>
+ <td>{{CompatNo}}</td>
+ <td>{{CompatNo}}</td>
+ <td>iOS 8</td>
+ </tr>
+ </tbody>
+</table>
+</div>
+
+<h2 id="See_also" name="See_also">相关链接</h2>
+
+<ul>
+ <li>{{jsxref("Math.round()")}}</li>
+</ul>