美文网首页
Math对象的扩展-es6

Math对象的扩展-es6

作者: 前端的爬行之旅 | 来源:发表于2019-05-23 14:37 被阅读0次

Math.trunc()

Math.trunc方法用于去除一个属的小数部分,返回整数部分。

// 替代方法:
Math.trunc = Math.trunc || function(x){
    return x < 0 ? Math.ceil(x) : Math.floor(x); 
}

Math.sign()

Math.sign方法判断一个数是正数,负数,还是零。

  • 参数是正数,返回+1;
  • 参数是负数,返回-1;
  • 参数是0,返回0;
  • 参数是-0,返回-0;
  • 参数是其他值,返回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; 
}

Math.sign()

  • 参数是负数,返回true;
  • 参数是其他值,返回false;

Math.cbrt()

Math.cbrt方法用于计算一个数的立方根。

// 替代方法:
Math.cbrt= Math.cbrt|| function(x){
    var y = Math.pow(Math.cbrt(x), 1/3);
    return x > 0 ? -y : y; 
}

Math.hypotbit()

Math.hypot方法用于返回所有参数的平方和的平凡根。

对数方法

  • Math.expm1()
  • Math.log1p()
  • Math.log10()
  • Math.log2()

双曲函数的方法

  • Math.sinh(x)返回x的双曲正弦
  • Math.cosh(x)返回x的双曲余弦
  • Math.tanh(x)返回x的双曲正切
  • Math.asinh(x)返回x的反双曲正弦
  • Math.acosh(x)返回x的反双曲余弦
  • Math.atanh(x)返回x的反双曲正切

相关文章

网友评论

      本文标题:Math对象的扩展-es6

      本文链接:https://www.haomeiwen.com/subject/zofvzqtx.html