Math 對象的擴(kuò)展
ES6 在 Math 對象上新增了 17 個與數(shù)學(xué)相關(guān)的方法。所有這些方法都是靜態(tài)方法,只能在 Math 對象上調(diào)用。
Math.trunc()
Math.trunc() 方法用于去除一個數(shù)的小數(shù)部分,返回整數(shù)部分看下面的代碼
console.log(Math.trunc(3.2));
console.log(Math.trunc(3.8));
console.log(Math.trunc(4.0));
console.log(Math.trunc(4.5));
console.log(Math.trunc(-3.2));
console.log(Math.trunc(-3.8));
console.log(Math.trunc(-4.0));
console.log(Math.trunc(-4.5));

當(dāng)然會有非數(shù)值的情況,非數(shù)值的情況下, Math.trunc() 會把非數(shù)值轉(zhuǎn)化為數(shù)值,如果無法轉(zhuǎn)化為數(shù)值就轉(zhuǎn)化為 NaN 。
console.log(Math.trunc('-3.2'));
console.log(Math.trunc('abc'));
console.log(Math.trunc());
console.log(Math.trunc(NaN));

Math.sign()
Math.sign() 方法用來判斷一個數(shù)到底是正數(shù)還是負(fù)數(shù),它會返回五種值。
- 參數(shù)為正數(shù),返回+1;
- 參數(shù)為負(fù)數(shù),返回-1;
- 參數(shù)為0,返回0;
- 參數(shù)為-0,返回-0;
- 其他值,返回NaN。要注意,這個其他值里面如果可以轉(zhuǎn)化為數(shù)字就會轉(zhuǎn)位數(shù)字再進(jìn)行判斷
看下面的代碼
console.log(Math.sign(30));
console.log(Math.sign(-30));
console.log(Math.sign(0));
console.log(Math.sign(-0));
console.log(Math.sign('abc'));//返回NaN
console.log(Math.sign('20'));//返回1
console.log(Math.sign('-20'));//返回-1
console.log(Math.sign('0')); //返回0
console.log(Math.sign('-0'));//返回-0
console.log(Math.sign(NaN));

Math.cbrt()
Math.cbrt() 方法用于計(jì)算一個數(shù)的立方根。以前是沒有計(jì)算立方根的這個方法的。以前的計(jì)算方法就是 Math.pow(81,1/3) 這樣進(jìn)行計(jì)算
console.log(Math.cbrt(0));
console.log(Math.cbrt(1));
console.log(Math.cbrt(2));
console.log(Math.cbrt(-1));
console.log(Math.cbrt(-0));
console.log(Math.cbrt('9'));
console.log(Math.cbrt('-9'));
console.log(Math.cbrt('0'));
console.log(Math.cbrt('hello'));
console.log(Math.cbrt(NaN));
console.log(Math.pow(27,1/3));

Math.clz32()
JavaScript 的整數(shù)使用32位二進(jìn)制形式表示,Math.clz32 方法返回一個數(shù)的32位無符號整數(shù)形式有多少個前導(dǎo)0。
Math.clz32(0) // 32
Math.clz32(1) // 31
Math.clz32(1000) // 22
Math.clz32(0b01000000000000000000000000000000) // 1
Math.clz32(0b00100000000000000000000000000000) // 2
上面代碼中,0的二進(jìn)制形式全為0,所以有32個前導(dǎo)0;1的二進(jìn)制形式是0b1,只占1位,所以32位之中有31個前導(dǎo)0;1000的二進(jìn)制形式是0b1111101000,一共有10位,所以32位之中有22個前導(dǎo)0。
clz32 這個函數(shù)名就來自 ”count leading zero bits in 32-bit binary representations of a number“ (計(jì)算32位整數(shù)的前導(dǎo)0)的縮寫。
左移運(yùn)算符(<<)與Math.clz32方法直接相關(guān)。
Math.clz32(0) // 32
Math.clz32(1) // 31
Math.clz32(1 << 1) // 30
Math.clz32(1 << 2) // 29
Math.clz32(1 << 29) // 2
對于小數(shù),Math.clz32方法只考慮整數(shù)部分。
Math.clz32(3.2) // 30
Math.clz32(3.9) // 30
對于空值或其他類型的值,Math.clz32方法會將它們先轉(zhuǎn)為數(shù)值,然后再計(jì)算。
Math.clz32() // 32
Math.clz32(NaN) // 32
Math.clz32(Infinity) // 32
Math.clz32(null) // 32
Math.clz32('foo') // 32
Math.clz32([]) // 32
Math.clz32({}) // 32
Math.clz32(true) // 31
Math.imul()
Math.imul方法返回兩個數(shù)以32位帶符號整數(shù)形式相乘的結(jié)果,返回的也是一個32位的帶符號整數(shù)。
Math.imul(2, 4) // 8
Math.imul(-1, 8) // -8
Math.imul(-2, -2) // 4
如果只考慮最后32位,大多數(shù)情況下,Math.imul(a, b)與a * b的結(jié)果是相同的,即該方法等同于(a * b)|0的效果(超過32位的部分溢出)。之所以需要部署這個方法,是因?yàn)镴avaScript有精度限制,超過2的53次方的值無法精確表示。這就是說,對于那些很大的數(shù)的乘法,低位數(shù)值往往都是不精確的,Math.imul方法可以返回正確的低位數(shù)值。
(0x7fffffff * 0x7fffffff)|0 // 0
上面這個乘法算式,返回結(jié)果為0。但是由于這兩個二進(jìn)制數(shù)的最低位都是1,所以這個結(jié)果肯定是不正確的,因?yàn)楦鶕?jù)二進(jìn)制乘法,計(jì)算結(jié)果的二進(jìn)制最低位應(yīng)該也是1。這個錯誤就是因?yàn)樗鼈兊某朔e超過了2的53次方,JavaScript 無法保存額外的精度,就把低位的值都變成了0。Math.imul 方法可以返回正確的值1。
Math.imul(0x7fffffff, 0x7fffffff) // 1
Math.fround()
Math.fround方法返回一個數(shù)的單精度浮點(diǎn)數(shù)形式。
Math.fround(0) // 0
Math.fround(1) // 1
Math.fround(1.337) // 1.3370000123977661
Math.fround(1.5) // 1.5
Math.fround(NaN) // NaN
對于整數(shù)來說,Math.fround 方法返回結(jié)果不會有任何不同,區(qū)別主要是那些無法用64個二進(jìn)制位精確表示的小數(shù)。這時,Math.fround 方法會返回最接近這個小數(shù)的單精度浮點(diǎn)數(shù)。
Math.hypot()
Math.hypot() 方法返回所有參數(shù)的平方和的平方根。
Math.hypot(3, 4); // 5
Math.hypot(3, 4, 5); // 7.0710678118654755
Math.hypot(); // 0
Math.hypot(NaN); // NaN
Math.hypot(3, 4, 'foo'); // NaN
Math.hypot(3, 4, '5'); // 7.0710678118654755
Math.hypot(-3); // 3
上面代碼中,3的平方加上4的平方,等于5的平方。
如果參數(shù)不是數(shù)值,Math.hypot方法會將其轉(zhuǎn)為數(shù)值。只要有一個參數(shù)無法轉(zhuǎn)為數(shù)值,就會返回NaN。
對數(shù)方法
ES6新增了4個對數(shù)相關(guān)方法。
Math.expm1()
Math.expm1(x) 返回 ex - 1,即 Math.exp(x) - 1。
Math.expm1(-1) // -0.6321205588285577
Math.expm1(0) // 0
Math.expm1(1) // 1.718281828459045
Math.log1p()
Math.log1p(x) 方法返回1 + x的自然對數(shù),即 Math.log(1 + x)。如果x小于-1,返回NaN。
Math.log1p(1) // 0.6931471805599453
Math.log1p(0) // 0
Math.log1p(-1) // -Infinity
Math.log1p(-2) // NaN
Math.log10()
Math.log10(x) 返回以10為底的x的對數(shù)。如果x小于0,則返回NaN。
Math.log10(2) // 0.3010299956639812
Math.log10(1) // 0
Math.log10(0) // -Infinity
Math.log10(-2) // NaN
Math.log10(100000) // 5
Math.log2()
Math.log2(x) 返回以2為底的x的對數(shù)。如果x小于0,則返回NaN。
Math.log2(3) // 1.584962500721156
Math.log2(2) // 1
Math.log2(1) // 0
Math.log2(0) // -Infinity
Math.log2(-2) // NaN
Math.log2(1024) // 10
Math.log2(1 << 29) // 29
三角函數(shù)方法
ES6新增了6個三角函數(shù)方法。
Math.sinh(x) 返回x的雙曲正弦(hyperbolic sine)
Math.cosh(x) 返回x的雙曲余弦(hyperbolic cosine)
Math.tanh(x) 返回x的雙曲正切(hyperbolic tangent)
Math.asinh(x) 返回x的反雙曲正弦(inverse hyperbolic sine)
Math.acosh(x) 返回x的反雙曲余弦(inverse hyperbolic cosine)
Math.atanh(x) 返回x的反雙曲正切(inverse hyperbolic tangent)