From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- .../global_objects/math/fround/index.html | 127 +++++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 files/ja/web/javascript/reference/global_objects/math/fround/index.html (limited to 'files/ja/web/javascript/reference/global_objects/math/fround') diff --git a/files/ja/web/javascript/reference/global_objects/math/fround/index.html b/files/ja/web/javascript/reference/global_objects/math/fround/index.html new file mode 100644 index 0000000000..8af6e99331 --- /dev/null +++ b/files/ja/web/javascript/reference/global_objects/math/fround/index.html @@ -0,0 +1,127 @@ +--- +title: Math.fround() +slug: Web/JavaScript/Reference/Global_Objects/Math/fround +tags: + - ES6 + - JavaScript + - Math + - Method + - Reference + - fround +translation_of: Web/JavaScript/Reference/Global_Objects/Math/fround +--- +
{{JSRef}}
+ +

Math.fround() 関数は、ある {{jsxref("Number")}} を表す最も近い{{interwiki("wikipedia", "単精度浮動小数点数", "32ビット単精度")}}浮動小数点数を返します。

+ +
{{EmbedInteractiveExample("pages/js/math-fround.html")}}
+ + + +

構文

+ +
var singleFloat = Math.fround(doubleFloat);
+ +

引数

+ +
+
doubleFloat
+
{{jsxref("Number")}}。この引数が異なる方であった場合は、数値に変換されるか、変換できなければ {{jsxref("NaN")}} になります。
+
+ +

返値

+ +

与えられた値を表す最も近い {{interwiki("wikipedia", "単精度浮動小数点数", "32 ビット単精度")}}浮動小数点数です。

+ +

解説

+ +

JavaScriptは内部的に 64 ビットの倍精度浮動小数点数を使用しており、非常に高い精度を提供しています。しかし、例えば {{jsxref("Float32Array")}} から値を読み込む場合など 32 ビットの浮動小数点数を扱うことがあるかもしれません。これは混乱を招く可能性があります。 64 ビットの浮動小数点数と 32 ビットの浮動小数点数が等しいかどうかをチェックすると、一見同じように見える数値であっても失敗することがあります。

+ +

これを解決するには、 Math.fround() を使用して、 64 ビット浮動小数点数を 32 ビットの浮動小数点数にキャストすることができます。内部的には、 JavaScript は数値を 64 ビットの浮動小数点数として扱い続けますが、仮数の23番目のビットに「偶数への丸め」を実行し、それに続く仮数ビットをすべて 0 に設定します。数値が 32 ビットの浮動小数点数の範囲外の場合は、 {{jsxref("Infinity")}} または -Infinity が返されます。

+ +

fround()Math の静的メソッドであるため、生成した Math オブジェクトのメソッドとしてではなく、常に Math.fround() として使用するようにしてください (Math はコンストラクターではありません)。

+ +

+ +

Math.fround() の使用

+ +

1.5 という数値は二進数で正確に表すことができ、32ビットと64ビットとで同じになります。

+ +
Math.fround(1.5); // 1.5
+Math.fround(1.5) === 1.5; // true
+
+ +

しかし、 1.337 という数値は二進数では正確に表すことができず、32ビットと64ビットとで異なります。

+ +
Math.fround(1.337); // 1.3370000123977661
+Math.fround(1.337) === 1.337; // false
+
+ +

21502^150 は32ビット浮動小数点では大きすぎるため、 Infinity が返されます。

+ +
2 ** 150; // 1.42724769270596e+45
+Math.fround(2 ** 150); // Infinity
+
+ +

引数が数値に変換できない場合、または{{interwiki("wikipedia", "NaN", "非数")}} (NaN) であった場合、 Math.fround()NaN を返します。

+ +
Math.fround('abc'); // NaN
+Math.fround(NaN); // NaN
+
+ +

ポリフィル

+ +

これは {{jsxref("Float32Array")}} に対応している場合、次の関数でエミュレートできます。

+ +
Math.fround = Math.fround || (function (array) {
+  return function(x) {
+    return array[0] = x, array[0];
+  };
+})(new Float32Array(1));
+
+ +

古いブラウザーに対応するには、より遅いですが、次のものも利用できます。

+ +
if (!Math.fround) Math.fround = function(arg) {
+  arg = Number(arg);
+  // Return early for ±0 and NaN.
+  if (!arg) return arg;
+  var sign = arg < 0 ? -1 : 1;
+  if (sign < 0) arg = -arg;
+  // Compute the exponent (8 bits, signed).
+  var exp = Math.floor(Math.log(arg) / Math.LN2);
+  var powexp = Math.pow(2, Math.max(-126, Math.min(exp, 127)));
+  // Handle subnormals: leading digit is zero if exponent bits are all zero.
+  var leading = exp < -127 ? 0 : 1;
+  // Compute 23 bits of mantissa, inverted to round toward zero.
+  var mantissa = Math.round((leading - arg / powexp) * 0x800000);
+  if (mantissa <= -0x800000) return sign * Infinity;
+  return sign * powexp * (leading - mantissa / 0x800000);
+};
+ +

仕様書

+ + + + + + + + + + + + +
仕様書
{{SpecName('ESDraft', '#sec-math.fround', 'Math.fround')}}
+ +

ブラウザーの互換性

+ + + +

{{Compat("javascript.builtins.Math.fround")}}

+ +

関連情報

+ + -- cgit v1.2.3-54-g00ecf