引用类型之Math

247 阅读10分钟

持续创作,加速成长!这是我参与「掘金日新计划 · 10 月更文挑战」的第17天,点击查看活动详情

写在前面

前面我们在值类型和引用类型中简单介绍了一种单体内置对象-Math。Math对象其实在实际开发中用的还是比较多的,这篇文章主要是介绍一下Math对象常用的方法和属性。

Math 是一个内置对象,它拥有一些数学常数属性和数学函数方法。Math 不是一个函数对象。用于Number类型,不支持BigInt类型。与其他全局对象不同的是,Math 不是一个构造器。Math 的所有属性与方法都是静态的。

属性

1. Math.E

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

function getNapier() {
   return Math.E
}

getNapier() // 2.718281828459045
2. Math.LN2

Math.LN2 属性表示 2 的自然对数(ln2),约为 0.693.

function getNatLog2() {
   return Math.LN2
}

getNatLog2() // 0.6931471805599453
3. Math.LN10

Math.LN10 属性表示 10 的自然对数(ln10),约为 2.302

function getNatLog10() {
   return Math.LN10
}

getNatLog10() // 2.302585092994046
4. Math.LOG2E

Math.LOG2E 属性表示以 2 为底数,e 的对数(log2E),约为 1.442

function getLog2e() {
   return Math.LOG2E
}

getLog2e() // 1.4426950408889634
5. Math.LOG10E

Math.LOG10E 属性表示以 10 为底数,e 的对数(log10E),约为 0.434。

function getLog10e() {
   return Math.LOG10E
}

getLog10e() // 0.4342944819032518
6. Math.PI

Math.PI 表示一个圆的周长与直径的比例,约为 3.14159。

function calculateCircumference (radius) {
  return 2 * Math.PI * radius;
}

calculateCircumference(1);  // 6.283185307179586
7. Math.SQRT1_2

Math.SQRT1_2 属性表示 1/2 的平方根,约为 0.707。

function getRoot1_2() {
   return Math.SQRT1_2
}

getRoot1_2() // 0.7071067811865476
8. Math.SQRT2

Math.SQRT2 属性表示 2 的平方根,约为 1.414

function getRoot2() {
   return Math.SQRT2;
}

getRoot2(); // 1.4142135623730951

方法

1. Math.abs()

Math.abs(x) 函数返回一个数字的绝对值。

Math.abs("-1"); // 1
Math.abs(-2); // 2
Math.abs(null); // 0
Math.abs(""); // 0
Math.abs([]); // 0
Math.abs([2]); // 2
Math.abs([1, 2]); // NaN
Math.abs({}); // NaN
Math.abs("string"); // NaN
Math.abs(); // NaN
2. Math.acos()

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

2.1 语法

Math.acos(x);

2.2 参数说明

  • x: 一个数值

acos 方法以 -1 到 1 的一个数为参数,返回一个 0 到 pi(弧度)的数值。如果传入的参数值超出了限定的范围,将返回 NaN。

2.3 应用

// 对于小于 -1 或大于 1 的值,Math.acos 返回 NaN。
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()

Math.acosh() 函数返回一个数的反双曲余弦值。

3.1 语法

Math.acosh(x)

3.2 参数说明

  • x: 一个数字

3.3 用法

// 当参数小于 1 时,Math.acosh() 将返回 NaN。
Math.acosh(-1);  // NaN
Math.acosh(0);   // NaN
Math.acosh(0.5); // NaN
Math.acosh(1);   // 0
Math.acosh(2);   // 1.3169578969248166
4. Math.asin()

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

4.1 语法

Math.asin(x)

4.2 参数说明

  • x: 一个数字

asin 方法接受 -1 到 1 之间的数值作为参数,返回一个介于 - π 2 到 π 2 弧度的数值。如果接受的参数值超出范围,则返回 NaN。

4.3 用法

// 对于小于 -1 或大于 1 的参数值,Math.asin 返回 NaN。
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
5. Math.asinh()

Math.asinh() 返回一个数值的反双曲正弦值。

5.1 语法

Math.asinh(x)

5.2 参数

  • x: 一个数值

5.3 用法

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

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

6.1 语法

Math.atan(x)

6.2 参数说明

  • x:返回一个数值

atan 返回一个 - π 2 到 π 2 弧度之间的数值。

6.3 用法

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

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

7.1 语法

Math.atanh(x)

7.2 参数说明

  • x: 一个数值

7.3 用法

// 对于大于 1 或是小于-1 的值,函数返回 NaN 。
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
8. Math.atan2()

Math.atan2() 返回从原点 (0,0) 到 (x,y) 点的线段与 x 轴正方向之间的平面角度 (弧度值),也就是 Math.atan2(y,x)。

8.1 语法

Math.atan2(y, x)

8.2 参数说明

  • y: 数值
  • x: 数值

atan2 方法返回一个 -pi 到 pi 之间的数值,表示点 (x, y) 对应的偏移角度。这是一个逆时针角度,以弧度为单位,正 X 轴和点 (x, y) 与原点连线 之间。注意此函数接受的参数:先传递 y 坐标,然后是 x 坐标。

atan2 接受单独的 x 和 y 参数,而 atan 接受两个参数的比值。

8.3 用法

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.
9. Math.cbrt()

Math.cbrt() 函数返回任意数字的立方根。

9.1 语法

Math.cbrt(x)

9.2 参数说明

  • x: 一个数值

用法

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.clz32()

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

10.1 语法

Math.clz32 (x)

10.2 参数说明

  • x: 一个数字

如果 x 不是数字类型,则它首先会被转换成数字类型,然后再转成 32 位无符号整形数字。

如果转换后的 32 位无符号整形数字是 0, 则返回 32, 因为此时所有位上都是 0.

NaN, Infinity, -Infinity 这三个数字转成 32 位无符号整形数字后都是 0.

10.3 用法

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
11. Math.cos()

Math.cos(x)

11.1 语法

Math.cos(x)

11.2 参数说明

  • x: 一个以弧度为单位的数值

cos 方法返回一个 -1 到 1 之间的数值,表示角度(单位:弧度)的余弦值。

11.3 用法

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() 函数返回数值的双曲余弦函数。

12.1 语法

Math.cosh(x)

12.2 参数说明

  • x: 数值

12.3 用法

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

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

13.1 语法

Math.exp(x)

13.2 参数说明

  • x: 一个数值

13.3 用法

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

Math.expm1()  函数返回 E^x - 1, 其中 x 是该函数的参数,E 是自然对数的底数 2.718281828459045

14.1 语法

Math.expm1(x)

14.2 参数说明

  • x: 任意数字

参数 x 会被自动类型转换成 number 类型。

14.3 用法

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

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

15.1 语法

Math.fround(doubleFloat)

15.2 参数说明

  • doubleFloat: 一个 Number。若参数为非数字类型,则会被转投成数字。无法转换时,设置成NaN

15.3 用法

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

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

16.1 语法

Math.hypot([value1[,value2, ...]])

16.2 参数

  • value1, value2, ...: 任意个数字

用法

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)
17. Math.imul()

该函数将两个参数分别转换为 32 位整数,相乘后返回 32 位结果,类似 C 语言的 32 位整数相乘。

17.1 语法

var product = Math.imul(a, b)

17.2 参数说明

  • a: 被乘数
  • b: 乘数

17.3 用法

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
18. Math.log()

Math.log()  函数返回一个数的自然对数

18.1 语法

Math.log(x)

18.2 参数

  • x: 一个数字。

如果指定的 number 为负数,则返回值为 NaN

18.3 用法

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

Math.log1p()  函数返回一个数字加 1 后的自然对数 (底为 E), 既log(x+1).

19.1 语法

Math.log1p(x)

19.2 参数说明

  • x: 任意数字

如果参数的值小于-1, 则返回 NaN.

19.3 用法

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
20. Math.log10()

Math.log10()  函数返回一个数字以 10 为底的对数。

20.1 语法

Math.log10(x)

20.2 参数说明

  • x: 任意数字

如果传入的参数小于 0,则返回 NaN

20.3 用法

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.log2()

Math.log2()  函数返回一个数字以 2 为底的对数。

21.1 语法

Math.log2(x)

21.2 参数说明

  • x: 任意数字

21.3 用法

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
22. Math.sign()

Math.sign()  函数返回一个数字的符号,指示数字是正数,负数还是零。

22.1 语法

Math.sign(x);

22.2 参数说明

  • x: 任意数字

22.3 用法

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
23. Math.sin()

Math.sin()  函数返回一个数值的正弦值。

23.1 语法

Math.sin(x)

23.2 参数说明

  • x: 一个数值(以弧度为单位)

sin 方法返回一个 -1 到 1 之间的数值,表示给定角度(单位:弧度)的正弦值。

23.3 用法

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

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

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

24.1 语法

Math.sinh(x)

24.2 参数说明

  • x: 任意数字 (单位为度).

24.3 用法

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

Math.sqrt()  函数返回一个数的平方根

25.1 语法

Math.sqrt(x)

25.2 参数说明

  • x: 一个数值

如果参数 number 为负值,则 sqrt 返回NaN

25.3 用法

Math.sqrt(9); // 3
Math.sqrt(2); // 1.414213562373095

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

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

26.1 语法

Math.tan(x)

26.2 参数说明

  • X: 一个数值,表示一个角(单位:弧度)。

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

26.3 用法

function getTan(x) {
   return Math.tan(x);
}
27. Math.tanh()

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

27.1 语法

Math.tanh(x)

27.2 参数说明

  • x: 待计算的数字

27.3 用法

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

返回字符串 "Math"

28. Math.trunc()

Math.trunc()  方法会将数字的小数部分去掉,只保留整数部分。

28.1 语法

Math.trunc(value)

28.2 参数说明

  • value:任意数字

28.3 用法

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
29. Math.max()

Math.max()  函数返回作为输入参数的最大数字,如果没有参数,则返回 -Infinity

29.1 语法

Math.max()
Math.max(value0)
Math.max(value0, value1)
Math.max(value0, value1, /* … ,*/ valueN)

29.2 参数说明

  • value1value2, … , valueN: 0 个或多个数字,将在其中选择,并返回最大的值。

给定数值中最大的数。如果任一参数不能转换为数值,则返回 NaN。如果没有提供参数,返回 -Infinity

29.3 用法

Math.max(10, 20); //  20
Math.max(-10, -20); // -10
Math.max(-10, 20); //  20
30. Math.min()

Math.min()  函数返回作为输入参数的数字中最小的一个,如果没有参数,则返回 Infinity

30.1 语法

Math.min()
Math.min(value0)
Math.min(value0, value1)
Math.min(value0, value1, /* … ,*/ valueN)

30.2 参数说明

  • value1, …, valueN: 0 个或多个数字,将在其中选择,并返回最小值。

给定数值中最小的数。如果任一参数不能转换为数值,则返回 NaN。如果没有提供参数,返回 Infinity

30.3 用法

const x = 10;
const y = -20;
const z = Math.min(x, y); // -20
31. Math.pow()

Math.pow()  函数返回基数(base)的指数(exponent)次幂,即 base^exponent

31.1 语法

Math.pow(base, exponent)

31.2 参数说明

  • base:基数
  • exponent:指数

31.3 用法

function raisePower(x,y) {
   return Math.pow(x,y)
}
32. Math.random()

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

32.1 语法

Math.random()

32.3 用法

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

Math.round()  函数返回一个数字四舍五入后最接近的整数。

33.1 语法

Math.round(x)

33.2 参数说明

  • x: 一个数值。

33.3 用法

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

Math.floor()  函数总是返回小于等于一个给定数字的最大整数。

34.1 语法

Math.floor(x)

34.2 参数说明

  • x: 一个数字

34.3 用法

Math.floor(-Infinity); // -Infinity
Math.floor(-45.95); // -46
Math.floor(-45.05); // -46
Math.floor(-0); // -0
Math.floor(0); // 0
Math.floor(4); //   4
Math.floor(45.05); //  45
Math.floor(45.95); //  45
Math.floor(Infinity); // Infinity
35. Math.ceil()

Math.ceil() 函数总是四舍五入并返回大于等于给定数字的最小整数。

35.1 语法

Math.ceil(x)

35.2 参数说明

  • x: 一个数值

35.3 用法

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