数値の表現
2進数表記の新しい書き方:接頭辞 0b または 0B。
console.log(0b11 === 3); // true
console.log(0B11 === 3); // true
8進数表記の新しい書き方:接頭辞 0o または 0O。
console.log(0o11 === 9); // true
console.log(0O11 === 9); // true
定数
Number.EPSILON
Number.EPSILON プロパティは、1 と 1 より大きい最小の浮動小数点数との差を表します。
その値は 2.2204460492503130808472633361816E-16、または 2^-52 にほぼ等しい。
数値が誤差範囲内かどうかをテストする:
0.1 + 0.2 === 0.3; // false
// 在误差范围内即视为相等
equal = (Math.abs(0.1 - 0.3 + 0.2) < Number.EPSILON); // true
プロパティ属性
writable:false
enumerable:false
configurable:false
最大/最小安全整数
安全整数
安全整数とは、JavaScript で正確に表現できる整数のことです。安全整数の範囲は 2 の -53 乗から 2 の 53 乗の間(両端を含まない)で、この範囲を超える整数は正確に表現できません。
最大安全整数
安全整数の範囲の上限、すなわち 2 の 53 乗 - 1。
Number.MAX_SAFE_INTEGER + 1 === Number.MAX_SAFE_INTEGER + 2;
Number.MAX_SAFE_INTEGER === Number.MAX_SAFE_INTEGER + 1;
Number.MAX_SAFE_INTEGER - 1 === Number.MAX_SAFE_INTEGER - 2;
最小安全整数
安全整数の範囲の下限、すなわち 2 の 53 乗 - 1 の負の数。
Number.MIN_SAFE_INTEGER + 1 === Number.MIN_SAFE_INTEGER + 2;
Number.MIN_SAFE_INTEGER === Number.MIN_SAFE_INTEGER - 1;
Number.MIN_SAFE_INTEGER - 1 === Number.MIN_SAFE_INTEGER - 2;
プロパティ属性
writable:false
enumerable:false
configurable:false
メソッド
Number オブジェクトの新しいメソッド
Number.isFinite()
数値が有限(finite)かどうか(つまり Infinity でないか)をチェックするために使用します。
console.log( Number.isFinite(1));
console.log( Number.isFinite(0.1));
console.log( Number.isFinite(NaN));
console.log( Number.isFinite(Infinity));
console.log( Number.isFinite(-Infinity));
console.log( Number.isFinite('foo'));
console.log( Number.isFinite('15'));
console.log( Number.isFinite(true));
Number.isNaN()値が ... であるかを確認するために使用します。NaN 。
console.log(Number.isNaN(NaN));
console.log(Number.isNaN('true'/0));
Number.isNaN("NaN");
Number.isNaN(undefined);
Number.isNaN({});
Number.isNaN("true");
グローバルから Number オブジェクトに移植されたメソッド
グローバルメソッドを段階的に減らし、グローバル変数のモジュール化を目的としています。
メソッドの動作は変更されていません。
Number.parseInt()
指定された文字列を指定の基数の整数に変換するために使用します。
Number.parseInt('12.34');
Number.parseInt(12.34);
Number.parseInt('0011',2);
Number.parseInt === parseInt;
Number.parseFloat()文字列を浮動小数点数に解析するために使用します。Number.parseFloat('123.45')
Number.parseFloat('123.45abc')
Number.parseFloat('abc')
Number.parseFloat === parseFloat
Number.isInteger()指定された引数が整数かどうかを判定するために使用します。Number.isInteger(value)
Number.isInteger(0);
Number.isInteger(1);
Number.isInteger(1.0);
Number.isInteger(1.1);
Number.isInteger(Math.PI);
Number.isInteger(NaN);
Number.isInteger(Infinity);
Number.isInteger(-Infinity);
Number.isInteger("10");
Number.isInteger(true);
Number.isInteger(false);
Number.isInteger([1]);
Number.isInteger(1.0000000000000001)
Number.isInteger(5E-324);
Number.isInteger(5E-325);
Number.isSafeInteger()数値が安全な範囲内にあるかどうかを判定するために使用します。Number.isSafeInteger(Number.MIN_SAFE_INTEGER - 1);
Number.isSafeInteger(Number.MAX_SAFE_INTEGER + 1);
Math オブジェクトの拡張
ES6 では Math オブジェクトに数学関連の静的メソッドが 17 個追加されました。これらのメソッドは Math 内でのみ呼び出すことができます。
通常の計算
Math.cbrt
数値の立方根を計算するために使用します。
Math.cbrt(1);
Math.cbrt(0);
Math.cbrt(-1);
Math.cbrt('1');
Math.cbrt('hhh');
Math.imul
2 つの数値を 32 ビット符号付き整数として乗算した結果を返します。返される値も 32 ビットの符号付き整数です。
Math.imul(1, 2);
Math.imul(0x7fffffff, 0x7fffffff);
Math.hypot
すべての引数の平方和の平方根を計算するために使用します。
Math.hypot(3, 4);
Math.hypot(1, 2, '3');
Math.hypot(true);
Math.hypot(false);
Math.hypot();
Math.hypot([]);
Math.hypot(Infinity);
Math.hypot(-Infinity);
Math.hypot(NaN);
Math.hypot(3, 4, 'foo');
Math.hypot({});
Math.clz32
数値の 32 ビット符号なし整数形式における先頭の 0 の個数を返すために使用します。
Math.clz32(0);
Math.clz32(1);
Math.clz32(0b01000000000100000000000000000000);
Math.clz32(0.5);
Math.clz32('1');
Math.clz32();
Math.clz32([]);
Math.clz32({});
Math.clz32(NaN);
Math.clz32(Infinity);
Math.clz32(-Infinity);
Math.clz32(undefined);
Math.clz32('hhh');
数値処理
Math.trunc
数値の整数部分を返すために使用します。
Math.trunc(12.3);
Math.trunc(12);
Math.trunc(-0.5);
Math.trunc(0.5);
Math.trunc("12.3");
Math.trunc();
Math.trunc(NaN);
Math.trunc("hhh");
Math.trunc("123.2hhh");
Math.fround
数値の 32 ビット単精度浮動小数点数形式を取得するために使用します。
Math.fround(-(2**24)+1);
Math.fround(2 ** 24 - 1);
Math.fround(1.234)
Math.fround(0.3);
Math.fround(NaN)
Math.fround(Infinity)
Math.fround('5');
Math.fround(true);
Math.fround(null);
Math.fround([]);
Math.fround({});
判定
Math.sign
数値の符号(正、負、0)を判定します。
Math.sign(1);
Math.sign(-1);
Math.sign(0);
Math.sign(-0);
Math.sign('1');
Math.sign('-1');
Math.sign(NaN);
Math.sign('hhh');
対数メソッド
Math.expm1()
e の x 乗から 1 を引いた結果、すなわち Math.exp(x) - 1 を計算するために使用します。
Math.expm1(1);
Math.expm1(0);
Math.expm1(-1);
Math.expm1('0');
Math.expm1(NaN);
Math.log1p(x)
1 + x の自然対数、すなわち Math.log(1 + x) を計算するために使用します。
Math.log1p(1);
Math.log1p(0);
Math.log1p(-1);
Math.log1p(-2);
Math.log10(x)
10 を底とする x の対数を計算するために使用します。
Math.log10(1);
Math.log10('1');
Math.log10(0);
Math.log10(-1);
Math.log2()
2 を底とする x の対数を計算するために使用します。
Math.log2(1);
Math.log2('1');
Math.log2(0);
Math.log2(-1);
双曲線関数メソッド
- Math.sinh(x): 双曲線正弦を計算するために使用します。
- Math.cosh(x): 双曲線余弦を計算するために使用します。
- Math.tanh(x): 双曲線正接を計算するために使用します。
- Math.asinh(x): 逆双曲線正弦を計算するために使用します。
- Math.acosh(x): 逆双曲線余弦を計算するために使用します。
- Math.atanh(x): 逆双曲線正接を計算するために使用します。
指数演算子
1 ** 2;
2 ** 2 ** 3;
let exam = 2;
exam ** = 2;