zoukankan      html  css  js  c++  java
  • es6 Number.isFinite()、Number.isNaN()、Number.isInteger()、Math.trunc()、Math.sign()、Math.cbrt()、Math.fround()、Math.hypot()、Math 对数方法

    ES6在Number对象上,新提供了Number.isFinite()和Number.isNaN()两个方法,用来检查Infinite和NaN这两个特殊值。

    Number.isFinite()用来检查一个数值是否非无穷(infinity)。

    Number.isFinite(15); // true
    Number.isFinite(0.8); // true
    Number.isFinite(NaN); // false
    Number.isFinite(Infinity); // false
    Number.isFinite(-Infinity); // false
    Number.isFinite("foo"); // false
    Number.isFinite("15"); // false
    Number.isFinite(true); // false
    

    Number.isNaN()用来检查一个值是否为NaN。

    Number.isNaN(NaN); // true
    Number.isNaN(15); // false
    Number.isNaN("15"); // false
    Number.isNaN(true); // false
    

      

    Number.isInteger()用来判断一个值是否为整数。需要注意的是,在JavaScript内部,整数和浮点数是同样的储存方法,所以3和3.0被视为同一个值。

    Number.isInteger(25) // true
    Number.isInteger(25.0) // true
    Number.isInteger(25.1) // false
    Number.isInteger("15") // false
    Number.isInteger(true) // false
    

      

    Math对象新增的方法,都是静态方法,只能在Math对象上调用。

    Math.trunc():去除一个数的小数部分,返回整数部分。

    Math.trunc(4.1) // 4
    Math.trunc(-4.1) // -4

    注意:对于空值和无法截取整数的值,返回NaN。

    Math.sign():判断一个数到底是正数、负数、还是零。

    返回五种值:参数为正数,返回+1;参数为负数,返回-1;参数为0,返回0;参数为-0,返回-0;其他值,返回NaN。

    Math.sign(-5) // -1
    Math.sign(5) // +1
    Math.sign(0) // +0
    Math.sign(-0) // -0
    Math.sign('hubwiz'); // NaN
    

     Math.cbrt():计算一个数的立方根。

    Math.cbrt(-1); // -1
    Math.cbrt(0);  // 0
    Math.cbrt(2);  // 1.2599210498948732

    Math.fround():返回一个数的单精度浮点数形式。

    Math.fround(0);     // 0
    Math.fround(1.337); // 1.3370000123977661
    Math.fround(NaN);   // NaN

    Math.hypot():返回所有参数的平方和的平方根。

    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

    如果参数不是数值,Math.hypot方法会将其转为数值。只要有一个参数无法转为数值,就会返回NaN。

    Math 对数方法

    Math.expm1(x):返回ex - 1。

    Math.expm1(-1); // -0.6321205588285577
    Math.expm1(0);  // 0
    Math.expm1(1);  // 1.718281828459045

    Math.log1p(x):返回1 + x的自然对数。如果x小于-1,返回NaN。

    Math.log1p(1);  // 0.6931471805599453
    Math.log1p(0);  // 0
    Math.log1p(-1); // -Infinity
    Math.log1p(-2); // NaN

    Math.log10(x):返回以10为底的x的对数。如果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(x):返回以2为底的x的对数。如果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.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)
  • 相关阅读:
    SGU 495 Kids and Prizes
    HDU 3853 LOOPS
    HDU 4089 Activation
    HDU 4405 Aeroplane chess
    ZOJ 3329 One Person Game
    POJ 2096 Collecting Bugs
    POJ1573(Robot Motion)
    poj2632(Crashing Robots)
    poj1068(Parencodings)
    poj2506(Tiling)
  • 原文地址:https://www.cnblogs.com/jkr666666/p/8032908.html
Copyright © 2011-2022 走看看