Math API

作者: PYFang | 来源:发表于2018-03-01 15:55 被阅读0次

属性

Math.E 表示自然对数的底数(或称为基数),e,约等于 2.718。

function getNapier() {
   return Math.E
}

getNapier() // 2.718281828459045

方法

1、Math.abs() 函数返回指定数字 “x“ 的绝对值。
传入一个非数字形式的字符串或者 undefined/empty 变量,将返回 NaN。传入 null将返回 0

Math.abs('-1');     // 1
Math.abs(-2);       // 2
Math.abs(null);     // 0
Math.abs("string"); // NaN
Math.abs();         // NaN

2、Math.acos() 返回一个数的反余弦值(单位为弧度)
描述:

  • acos 方法以 -11 的一个数为参数,返回一个 0pi(弧度)的数值。如果传入的参数值超出了限定的范围,将返回 NaN
  • 由于acosMath 的静态方法,所以应该像这样使用:Math.acos(),而不是作为你创建的 Math实例的属性(Math 不是一个构造函数)。
Math.acos(-2);  // NaN
Math.acos(-1);  // 3.141592653589793
Math.acos(0);   // 1.5707963267948966
Math.acos(0.5); // 1.0471975511965979
Math.acos(1);   // 0
Math.acos(2);   // NaN

3、Math.acosh() 返回一个数字的反双曲余弦值
描述:因为acosh()Math对象的静态方法,所以你应该像这样使用它:Math.acosh(), 而不是作为你创建的Math实例的属性(Math不是构造函数)。

Math.acosh(-1);  // NaN
Math.acosh(0);   // NaN
Math.acosh(0.5); // NaN
Math.acosh(1);   // 0
Math.acosh(2);   // 1.3169578969248166

返回指定参数的反双曲余弦值,如果指定的参数小于 1 则返回NaN

4、Math.asin() 方法返回一个数值的反正弦(单位为弧度)
描述

  • asin 方法接受 -11 之间的数值作为参数,返回一个介于 - π2π2 弧度的数值。如果接受的参数值超出范围,则返回 NaN。
  • 由于 asinMath 的静态方法,所有应该像这样使用:Math.asin(),而不是作为你创建的 Math 实例的方法。
Math.asin(-2);  // NaN
Math.asin(-1);  // -1.5707963267948966 (-pi/2)
Math.asin(0);   // 0
Math.asin(0.5); // 0.5235987755982989
Math.asin(1);   // 1.570796326794897 (pi/2)
Math.asin(2);   // NaN

对于小于 -1或大于 1 的参数值,Math.asin 返回 NaN

5、Math.asinh() 函数返回给定数字的反双曲正弦值
返回值:给定数值的反双曲正弦值
描述:由于asinh()Math的静态方法, 用户应该直接通过Math.asinh()来使用, 而不是先创建出Math对象再调用该方法(Math不是一个构造器)。

Math.asinh(1);  // 0.881373587019543
Math.asinh(0);  // 0

其他实现方式
由于arsinh ( x ) = ln ( x + √x2 + 1 ),因此该函数可以被如下的函数所模拟:

Math.asinh = Math.asinh || function(x) {
  if (x === -Infinity) {
    return x;
  } else {
    return Math.log(x + Math.sqrt(x * x + 1));
  }
}

6、Math.atan() 函数返回一个数值的反正切(以弧度为单位)

Math.atan(1);  // 0.7853981633974483
Math.atan(0);  // 0

7、Math.atan2() 返回其参数比值的反正切值
描述

  • atan2 方法返回一个 -pipi 之间的数值,表示点 (x, y) 对应的偏移角度。这是一个逆时针角度,以弧度为单位,正X轴和点(x, y) 与原点连线 之间。注意此函数接受的参数:先传递 y 坐标,然后是 x 坐标。
  • atan2 接受单独的 xy 参数,而atan 接受两个参数的比值。
Math.atan2(90, 15) // 1.4056476493802699
Math.atan2(15, 90) // 0.16514867741462683

Math.atan2( ±0, -0 )               // ±PI.
Math.atan2( ±0, +0 )               // ±0.
Math.atan2( ±0, -x )               // ±PI for x > 0.
Math.atan2( ±0, x )                // ±0 for x > 0.
Math.atan2( -y, ±0 )               // -PI/2 for y > 0.
Math.atan2( y, ±0 )                // PI/2 for y > 0.
Math.atan2( ±y, -Infinity )        // ±PI for finite y > 0.
Math.atan2( ±y, +Infinity )        // ±0 for finite y > 0.
Math.atan2( ±Infinity, x )         // ±PI/2 for finite x.
Math.atan2( ±Infinity, -Infinity ) // ±3*PI/4.
Math.atan2( ±Infinity, +Infinity ) // ±PI/4.

8、Math.atanh() 函数返回一个数值反双曲正切值

Math.atanh(-2);  // NaN
Math.atanh(-1);  // -Infinity
Math.atanh(0);   // 0
Math.atanh(0.5); // 0.5493061443340548
Math.atanh(1);   // Infinity
Math.atanh(2);   // NaN

9、Math.cbrt() 函数返回任意数字的立方根(ES6规范)
描述:参数 x 会被自动类型转换成 number 类型。cbrt"cube root" 的缩写, 意思是立方根.

Math.cbrt(NaN); // NaN
Math.cbrt(-1); // -1
Math.cbrt(-0); // -0
Math.cbrt(-Infinity); // -Infinity
Math.cbrt(0); // 0
Math.cbrt(1); // 1
Math.cbrt(Infinity); // Infinity
Math.cbrt(null); // 0
Math.cbrt(2);  // 1.2599210498948734

10、Math.ceil() 向上取整

Math.ceil(.95);    // 1
Math.ceil(4);      // 4
Math.ceil(7.004);  // 8
Math.ceil(-0.95);  // -0
Math.ceil(-4);     // -4
Math.ceil(-7.004); // -7

11、Math.clz32() 函数返回一个数字在转换成 32 无符号整形数字的二进制形式后, 开头的 0 的个数, 比如 1000000 转换成 32 位无符号整形数字的二进制形式后是 00000000000011110100001001000000, 开头的0 的个数是 12 个, 则 Math.clz32(1000000) 返回 12.(ES6规范)
描述:

  • "clz32"CountLeadingZeroes32 的缩写.
  • 如果 x 不是数字类型, 则它首先会被转换成数字类型, 然后再转成 32 位无符号整形数字.
  • 如果转换后的32 位无符号整形数字是 0, 则返回 32, 因为此时所有位上都是0.
  • NaN, Infinity, -Infinity 这三个数字转成 32 位无符号整形数字后都是 0.
  • 这个函数主要用于那些编译目标为 JS 语言的系统中, 比如 Emscripten.
Math.clz32(1)                // 31
Math.clz32(1000)             // 22 
Math.clz32()                 // 32
[NaN, Infinity, -Infinity, 0, -0, null, undefined, "foo", {}, []].filter(function (n) {
  return Math.clz32(n) !== 32
})                           // []
Math.clz32(true)             // 31
Math.clz32(3.5)              // 30

Math.cos() 函数返回一个数值的余弦值
描述:cos 方法返回一个-11 之间的数值,表示角度(单位:弧度)的余弦值。

Math.cos(0);           // 1
Math.cos(1);           // 0.5403023058681398

Math.cos(Math.PI);     // -1
Math.cos(2 * Math.PI); // 1

12、Math.cosh() 函数返回数值的双曲余弦函数

Math.cosh(0);  // 1
Math.cosh(1);  // 1.5430806348152437
Math.cosh(-1); // 1.5430806348152437

13、Math.exp() 函数返回 exx 表示参数,e欧拉常数(Euler's constant),自然对数的底数。

Math.exp(-1); // 0.36787944117144233
Math.exp(0);  // 1
Math.exp(1);  // 2.718281828459045

14、Math.expm1() 函数返回Ex- 1, 其中x 是该函数的参数, E 是自然对数的底数 2.718281828459045.
描述:参数 x 会被自动类型转换成 number 类型。expm1"exponent minus 1"的缩写.

Math.expm1(1)     // 1.7182818284590453
Math.expm1(-38)   // -1
Math.expm1("-38") // -1
Math.expm1("foo") // NaN

15、Math.floor() 向下取整

Math.floor( 45.95); 
// 45 
Math.floor( 45.05); 
// 45 
Math.floor( 4 ); 
// 4 
Math.floor(-45.05); 
// -46 
Math.floor(-45.95); 
// -46

16、Math.fround() 可以将任意的数字转换为离它最近的单精度浮点数形式的数字
描述:

  • JavaScript 内部使用64位的双浮点数字,支持很高的精度。但是,有时你需要用32位浮点数字,比如你从一个Float32Array 类型数组代表的是平台字节顺序为32位的浮点数型数组(对应于 C 浮点数据类型) 。 如果需要控制字节顺序, 使用 DataView 替代。其内容初始化为0。一旦建立起来,你可以使用这个对象的方法对其元素进行操作,或者使用标准数组索引语法 (使用方括号)。") 读取值时。这时会产生混乱:检查一个64位浮点数和一个32位浮点数是否相等会失败,即使二个数字几乎一模一样。
  • 要解决这个问题,可以使用 Math.fround() 来将64位的浮点数转换为32位浮点数。在内部,JavaScript继续把这个数字作为64位浮点数看待,仅仅是在尾数部分的第23位执行了“舍入到偶数”的操作,并将后续的尾数位设置为0。如果数字超出32位浮点数的范围,则返回 Infinity全局属性 Infinity 是一个数值,表示无穷大。")或 -Infinity

数字1.5可以在二进制数字系统中精确表示,32位和64位的值相同:

Math.fround(1.5); // 1.5
Math.fround(1.5) === 1.5; // true

但是,数字1.337却无法在二进制数字系统中精确表示,所以32位和64位的值是不同的:

Math.fround(1.337); // 1.3370000123977661
Math.fround(1.337) === 1.337; // false

2150 超出32位浮点,所以返回Infinity:

2 ** 150; // 1.42724769270596e+45
Math.fround(2 ** 150); // Infinity

如果参数无法转换成数字,或者为 NaN全局属性NaN表示 Not-A-Number 的值。") (NaN),Math.fround() 会返回 NaN

Math.fround('abc'); // NaN
Math.fround(NaN); // NaN

在某些精度不高的场合下,可以通过将二个浮点数转换成32位浮点数进行比较,以解决64位浮点数比较结果不正确的问题:

0.1 + 0.2 == 0.3;    //false

function equal(v1, v2) {
    return Math.fround(v1) == Math.fround(v2);
}

equal(0.1 + 0.2, 0.3);   //true

17、Math.hypot() 函数返回它的所有参数的平方和的平方根
描述:

  • 如果不传入任何参数, 则返回 +0 .
  • 如果参数列表中有至少一个参数不能被转换为数字,则返回 NaN
  • 如果只传入一个参数, 则 Math.hypot(x) 的效果等同于 Math.abs(x).
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, +"foo" => NaN
Math.hypot(3, 4, "5")   // 7.0710678118654755, +"5" => 5
Math.hypot(-3)          // 3, the same as Math.abs(-3)

18、Math.imul() 该函数返回两个参数的类c32位整数乘法运算的运算结果

语法:
Math.imul(a, b)
 // a 被乘数.
 // b 乘数.

例:
Math.imul(2, 4) // 8
Math.imul(-1, 8) // -8
Math.imul(-2, -2) // 4
Math.imul(0xffffffff, 5) //-5
Math.imul(0xfffffffe, 5) //-10

19、Math.log() 函数返回一个数的自然对数
描述:如果指定的 number 为负数,则返回值为 NaN。

例子1:使用Math.log
下面的函数返回指定变量的自然对数:

Math.log(-1); // NaN, out of range
Math.log(0); // -Infinity
Math.log(1); // 0
Math.log(10); // 2.302585092994046

例子2: 使用Math.log时基于不同的底数
下面的函数返回以 x 为底 y 的对数(即logx y):

function getBaseLog(x, y) {
    return Math.log(y) / Math.log(x);
}
如果你运行 getBaseLog(10, 1000),则会返回2.9999999999999996,非常接近实际答案:3,原因是浮点数精度问题。

20、Math.log10() 函数返回一个数字以 10 为底的对数,如果传入的参数小于 0, 则返回 NaN

Math.log10(10)   // 1
Math.log10(100)  // 2
Math.log10("100")// 2
Math.log10(1)    // 0
Math.log10(0)    // -Infinity
Math.log10(-2)   // NaN
Math.log10("foo")// NaN

21、Math.log1p() 函数返回一个数字加1后的自然对数 (底为 E), 既log(x+1)
描述:如果参数的值小于 -1, 则返回 NaN.

Math.log1p(Math.E-1)  // 1
Math.log1p(0)         // 0
Math.log1p("0")       // 0
Math.log1p(-1)        // -Infinity
Math.log1p(-2)        // NaN
Math.log1p("foo")     // NaN

函数 y = log(x+1) 的图形是这样的:

image.png
22、Math.log2() 函数返回一个数字以 2 为底的对数,如果传入的参数小于 0, 则返回 NaN.
Math.log2(2)     // 1
Math.log2(1024)  // 10
Math.log2(1)     // 0
Math.log2(0)     // -Infinity
Math.log2(-2)    // NaN
Math.log2("1024")// 10
Math.log2("foo") // NaN

23、Math.max() 函数返回一组数中的最大值
语法:Math.max([value1[,value2, ...]])
返回值:返回给定的一组数字中的最大值。如果给定的参数中至少有一个参数无法被转换成数字,则会返回NaN
描述:

  • 如果没有参数,则结果为- Infinity
  • 如果有任一参数不能被转换为数值,则结果为NaN
Math.max(10, 20);   //  20
Math.max(-10, -20); // -10
Math.max(-10, 20);  //  20

下面的方法使用 apply() 方法寻找一个数值数组中的最大元素。getMaxOfArray([1,2,3]) 等价于 Math.max(1, 2, 3),但是你可以使用 getMaxOfArray ()作用于任意长度的数组上。

function getMaxOfArray(numArray) {
    return Math.max.apply(null, numArray);
}

或者通过使用最新的扩展语句spread operator,获得数组中的最大值变得更容易。

var arr = [1, 2, 3];
var max = Math.max(...arr);

22、Math.min() 返回零个或更多个数值的最小值

  • 给定数值中最小的数。如果任一参数不能转换为数值,则返回NaN
    下例找出 x 和 y 的最小值,并把它赋值给 z:
var x = 10, y = -20;
var z = Math.min(x, y);

使用 Math.min() 裁剪值(Clipping a value)
Math.min 经常用于裁剪一个值,以便使其总是小于或等于某个边界值。例如:

var x = f(foo);

if (x > boundary) {
    x = boundary;
}
可以写成
var x = Math.min(f(foo), boundary);

另外,Math.max()也可以被用来以相似的方式裁剪一个值

23、Math.pow() 函数返回基数 (base)的指数(exponent)次幂,即 baseexponent
语法:Math.pow(base, exponent)
参数:

  • base:基数
  • exponent:指数
function raisePower(x,y) {
   return Math.pow(x,y)
}
如果 x 是 7 ,且 y 是 2,则 raisePower 函数返回 49 (7 的 2 次幂)

24、Math.random() 函数返回一个浮点, 伪随机数在范围[0,1),也就是说,从0(包括0)往上,但是不包括1(排除1),然后您可以缩放到所需的范围。实现将初始种子选择到随机数生成算法;它不能被用户选择或重置。他不能被用户选择或重置

得到一个大于等于0,小于1之间的随机数

function getRandom() {
  return Math.random();
}

得到一个两数之间的随机数
这个例子返回了一个在指定值之间的随机数。这个值比min大(可能与min相等), 以及比max小(但是不等于max).

function getRandomArbitrary(min, max) {
  return Math.random() * (max - min) + min;
}

得到一个两数之间的随机整数
这个例子返回了一个在指定值之间的随机整数。这个值比min大(如果min不是整数,那么下一个整数大于min), 以及比max小(但是不等于max).

function getRandomInt(min, max) {
  min = Math.ceil(min);
  max = Math.floor(max);
  return Math.floor(Math.random() * (max - min)) + min; //The maximum is exclusive and the minimum is inclusive
}

得到一个两数之间的随机整数,包括两个数在内
getRandomInt() 函数在最小值之上,它将排除最大值. 如果你需要结果包含最小值和最大值,怎么办呢? getRandomIntInclusive() 函数将能完成.

function getRandomIntInclusive(min, max) {
  min = Math.ceil(min);
  max = Math.floor(max);
  return Math.floor(Math.random() * (max - min + 1)) + min; //The maximum is inclusive and the minimum is inclusive 
}

25、Math.round() 函数返回一个数字四舍五入后最接近的整数
描述:如果参数的小数部分大于0.5,则舍入到下一个绝对值更大的整数。如果参数的小数部分小于0.5,则舍入到下一个绝对值更小的整数。如果参数的小数部分恰好等于0.5,见分晓舍入到下一个在正无穷方向中的整数。注意:与很多其他评议中的round()函数不同,Math.round()并不总是舍入到远离0的方向(尤其是在负数的小数部分愉好等于0.5的情况下)

x = Math.round(20.49)  // 20
x = Math.round(20.5)  // 21
x = Math.round(-20.5)  // -20
x = Math.round(-20.51)  // -21

26、Math.sign() 函数返回一个数字的符号, 指示数字是正数,负数还是零
描述:

  • 此函数共有5种返回值, 分别是 1, -1, 0, -0, NaN. 代表的各是正数, 负数, 正零, 负零, NaN。
  • 传入该函数的参数会被隐式转换成数字类型。
Math.sign(3);     //  1
Math.sign(-3);    // -1
Math.sign("-3");  // -1
Math.sign(0);     //  0
Math.sign(-0);    // -0
Math.sign(NaN);   // NaN
Math.sign("foo"); // NaN
Math.sign();      // NaN

27、Math.sin() 函数返回一个数值的正弦值
sin 方法返回一个 -11 之间的数值,表示给定角度(单位:弧度)的正弦值。

Math.sin(0);           // 0
Math.sin(1);           // 0.8414709848078965

Math.sin(Math.PI / 2); // 1

28、 Math.sinh() 函数返回一个数字(单位为角度)的双曲正弦值

Math.sinh(0)      // 0
Math.sinh(1)      // 1.1752011936438014
Math.sinh("-1")   // -1.1752011936438014
Math.sinh("foo")  // NaN

双曲正弦的图像如下:

image.png
29、Math.sqrt() 函数返回一个数的平方根
如果参数 number 为负值,则sqrt返回 NaN
Math.sqrt(9); // 3
Math.sqrt(2); // 1.414213562373095

Math.sqrt(1);  // 1
Math.sqrt(0);  // 0
Math.sqrt(-1); // NaN

Math.tan() 方法返回一个数值的正切值
tan 方法返回一个数值,表示一个角的正切值。

下面的函数返回变量 x 的正切值:

function getTan(x) {
   return Math.tan(x);
}
由于 Math.tan() 函数接受弧度数值,但是通常使用度更方便,下面的函数可以接受以度为单位的数值,将其转为弧度,然后返回其正切值。

function getTanDeg(deg) {
   var rad = deg * Math.PI/180;
   return Math.tan(rad);
}

30、Math.tanh() 函数将会返回一个数的双曲正切函数值

Math.tanh(0);        // 0
Math.tanh(Infinity); // 1
Math.tanh(1);        // 0.7615941559557649

多种实现方式

tanh()可以通过Math.exp()函数来构拟:
Math.tanh = Math.tanh || function(x) {
  if (x === Infinity) {
    return 1;
  } else if (x === -Infinity) {
    return -1;
  } else {
    return (Math.exp(x) - Math.exp(-x)) / (Math.exp(x) + Math.exp(-x));
  }
}

或者只调用一次Math.exp():
Math.tanh = Math.tanh || function(x) {
  if (x === Infinity) {
    return 1;
  } else if (x === -Infinity) {
    return -1;
  } else {
    var y = Math.exp(2 * x);
    return (y - 1) / (y + 1);
  }
}

31、Math.trunc() 方法会将数字的小数部分去掉,只保留整数部分
传入该方法的参数会被隐式转换成数字类型

Math.trunc(13.37)    // 13
Math.trunc(42.84)    // 42
Math.trunc(0.123)    //  0
Math.trunc(-0.123)   // -0
Math.trunc("-1.123") // -1
Math.trunc(NaN)      // NaN
Math.trunc("foo")    // NaN
Math.trunc()         // NaN

相关文章

  • 3.js基础--Math

    Math基础 专门用于封装数学计算所用的API或常量 Math不能new Math的API 1.取整 1>上取整:...

  • Math API

    属性 Math.E 表示自然对数的底数(或称为基数),e,约等于 2.718。 方法 1、Math.abs() 函...

  • java 中常用的类

    Math Math 类,包含用于执行基本数学运算的方法 常用API 取整 lstaticdoubleabs(dou...

  • java基础系列03--常用API方法

    java常用API方法 Math方法: Math.random(); 返回一个double类型的[0,1)之间的数...

  • Date,Math,对象API,数组API

    Date 在这里需要注意的是:在获取月份时需要+1,因为月份的取值是从0开始的。 Math Math.random...

  • tensorflow math api 汇总

    Defined intensorflow/python/ops/math_ops.py

  • 5.数值的扩展

    回到目录 各进制表示法 Number 的 api Math 扩展 API 例子 对数方法 略 双曲函数方法 略 指...

  • Math和Date

    Math对象 Math常用API 生成随机数 四舍五入 最大值 最小值 绝对值 向上取整 向下取证 π 各进制间数...

  • JS基础面试题——日期、Math、数组、对象

    主要介绍一下日期和Math及数组和对象的api,还有三道简单的面试题 知识点: 1. 日期 2. Math Mat...

  • math

    Math是11个内置对象中唯一一个不需要new的对象 Math的api传入的参数是数字,返回的也是数字,所以非数字...

网友评论

      本文标题:Math API

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