ECMAScript 6(9)数值的扩展 (9) Math对象扩展

    xiaoxiao2022-07-13  147

    目录

     

    Math对象的扩展

    1.  Math.trunc() 去除小数部分,返回整数

    2.  Math.sign() 判断正数、负数、零

    3.  Math.cart() 计算数值的立方根

    4.  Math.clz32() 

     5.  Math.imul()

    6.  Math.fround()

    7.  Math.hypot()

    8.  对数方法 

    9.  双曲函数方法

    10  指数运算符 **


    Math对象的扩展

    1.  Math.trunc() 去除小数部分,返回整数

    1.  简单来说,如果不是number类型,或者不能隐式转换成number类型,则返回NaN; 2.  被转换的值如果不是number类型, 会先转换成number类型后, 再省略小数部分.

    Math.trunc(1); //1 Math.trunc(1.9); //1 Math.trunc(-10.9); //-10 Math.trunc(-0); //-0,注意会保留负号 Math.trunc('-10.9'); //-10,可以识别字符串 Math.trunc(0o10); //8,可以正常识别二进制、八进制和十六进制数字 var obj = {}; obj.toString = function () { return '+2.9'; } Math.trunc(obj); //2,对于对象通过toString隐式转换,不会保留+号(但会保留减号) Math.trunc('a10'); //NaN,不能被隐式转换为number Math.trunc([1, 2]); //NaN,也不能 Math.trunc({}); //NaN,显然也不能

     低版本浏览器兼容函数实现 : 

    Math.trunc = Math.trunc || function(x) { return x < 0 ? Math.ceil(x) : Math.floor(x); };

     

    2.  Math.sign() 判断正数、负数、零

    正数返回+1;负数返回-1;正0返回0;负0返回-0;其他返回NaN;非number会被隐式转成number再判断 Math.sign(-5) // -1 Math.sign(5) // +1 Math.sign(0) // +0 Math.sign(-0) // -0 Math.sign(NaN) // NaN Math.sign(Number.MIN_VALUE); //1 Math.sign(Number.MIN_VALUE / 2); //0,这个是因为已经是最小的数字了,再除以在存储的时候和0一样,于是就变成0了 var obj = {}; obj.toString = function () { return '+2.9'; } Math.sign(obj); //1,说明是被隐式转换过的 Math.sign('a10'); //NaN,不能被隐式转换为number Math.sign([1, 2]); //NaN,也不能 Math.sign({}); //NaN,显然也不能

     

    低版本浏览器兼容函数实现 :

    Math.sign = Math.sign || function(x) { x = +x; // convert to a number if (x === 0 || isNaN(x)) { return x; } return x > 0 ? 1 : -1; };

    3.  Math.cart() 计算数值的立方根

    1. 简单来说,求变量的开立方根结果。例如数字8开立方根的结果是2。

    2.  开平方根是Math.sqrt();

    3.  变量会被隐式转换后再计算,不符合要求的输入内容会返回NaN。

    Math.cbrt(1); //1 Math.cbrt(8): //2 Math.cbrt(-8); //-2 Math.cbrt(0); //0 var obj = {}; obj.toString = function () { return '8'; } Math.cbrt(obj); //2,说明会被隐式转换 Math.cbrt({}); //NaN

    4.  Math.clz32() 

    将参数转为 32 位无符号整数的形式,然后返回这个 32 位值里面有多少个前导 0。

    Math.clz32(0) // 32 Math.clz32(1) // 31 Math.clz32(1000) // 22 Math.clz32(0b01000000000000000000000000000000) // 1 Math.clz32(0b00100000000000000000000000000000) // 2

     5.  Math.imul()

    方法返回两个数以 32 位带符号整数形式相乘的结果,返回的也是一个 32 位的带符号整数。

    Math.imul(2, 4) // 8 Math.imul(-1, 8) // -8 Math.imul(-2, -2) // 4

    6.  Math.fround()

     返回一个数的32位单精度浮点数形式。

    Math.fround(0) // 0 Math.fround(1) // 1 Math.fround(2 ** 24 - 1) // 16777215

    7.  Math.hypot()

    1. 返回所有参数的平方和的平方根。 

    2.   参数可能存在多个;

    3.   简单来说就是每个参数先自乘获得平方,然后再将结果的和相加,然后开方即可。

    4.   注意和Math.sqrt()的结果可能所有差异(主要是因为精度问题)

    Math.hypot(3, 4); // 5 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

    8.  对数方法 

    1. Math.expm1(x)返回 ex - 1,即Math.exp(x) - 1。

    2. Math.log1p(x)方法返回1 + x的自然对数,即Math.log(1 + x)。如果x小于-1,返回NaN。

    3. Math.log10(x)返回以 10 为底的x的对数。如果x小于 0,则返回 NaN。

    4. Math.log2(x)返回以 2 为底的x的对数。如果x小于 0,则返回 NaN。

     详细使用方法戳阮一峰ES6

    9.  双曲函数方法

    ES6 新增了 6 个双曲函数方法。

    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)

    10  指数运算符 **

    就是两个乘号  后面数值表示几次方

    2**2 === Math.pow(2, 2); //true 2**3 === Math.pow(2, 3); //true 2**2 // 4 2**3 // 8

     

    最新回复(0)