Math 对象属性和方法

Math 对象

在 JavaScript 中, Math 对象用于表达特定数值与执行计算任务。 Math 不是一个构造器,它的所有属性与方法都是静态的。它提供了多个数学常数属性和数学函数方法。由于 Math 不是构造器,使用时无需创建实例,直接使用 Math.xx() 即可。

 

Math 对象的属性和方法

// Object.getOwnPropertyDescriptors(Math):
E                          : {value: 2.718281828459045, writable: false, enumerable: false, configurable: false}
LN2                        : {value: 0.6931471805599453, writable: false, enumerable: false, configurable: false}
LN10                       : {value: 2.302585092994046, writable: false, enumerable: false, configurable: false}
LOG2E                      : {value: 1.4426950408889634, writable: false, enumerable: false, configurable: false}
LOG10E                     : {value: 0.4342944819032518, writable: false, enumerable: false, configurable: false}
PI                         : {value: 3.141592653589793, writable: false, enumerable: false, configurable: false}
SQRT1_2                    : {value: 0.7071067811865476, writable: false, enumerable: false, configurable: false}
SQRT2                      : {value: 1.4142135623730951, writable: false, enumerable: false, configurable: false}
abs                        : {writable: true, enumerable: false, configurable: true, value: ƒ}
acos                       : {writable: true, enumerable: false, configurable: true, value: ƒ}
acosh                      : {writable: true, enumerable: false, configurable: true, value: ƒ}
asin                       : {writable: true, enumerable: false, configurable: true, value: ƒ}
asinh                      : {writable: true, enumerable: false, configurable: true, value: ƒ}
atan                       : {writable: true, enumerable: false, configurable: true, value: ƒ}
atan2                      : {writable: true, enumerable: false, configurable: true, value: ƒ}
atanh                      : {writable: true, enumerable: false, configurable: true, value: ƒ}
cbrt                       : {writable: true, enumerable: false, configurable: true, value: ƒ}
ceil                       : {writable: true, enumerable: false, configurable: true, value: ƒ}
clz32                      : {writable: true, enumerable: false, configurable: true, value: ƒ}
cos                        : {writable: true, enumerable: false, configurable: true, value: ƒ}
cosh                       : {writable: true, enumerable: false, configurable: true, value: ƒ}
exp                        : {writable: true, enumerable: false, configurable: true, value: ƒ}
expm1                      : {writable: true, enumerable: false, configurable: true, value: ƒ}
floor                      : {writable: true, enumerable: false, configurable: true, value: ƒ}
fround                     : {writable: true, enumerable: false, configurable: true, value: ƒ}
hypot                      : {writable: true, enumerable: false, configurable: true, value: ƒ}
imul                       : {writable: true, enumerable: false, configurable: true, value: ƒ}
log                        : {writable: true, enumerable: false, configurable: true, value: ƒ}
log1p                      : {writable: true, enumerable: false, configurable: true, value: ƒ}
log2                       : {writable: true, enumerable: false, configurable: true, value: ƒ}
log10                      : {writable: true, enumerable: false, configurable: true, value: ƒ}
max                        : {writable: true, enumerable: false, configurable: true, value: ƒ}
min                        : {writable: true, enumerable: false, configurable: true, value: ƒ}
pow                        : {writable: true, enumerable: false, configurable: true, value: ƒ}
random                     : {writable: true, enumerable: false, configurable: true, value: ƒ}
round                      : {writable: true, enumerable: false, configurable: true, value: ƒ}
sign                       : {writable: true, enumerable: false, configurable: true, value: ƒ}
sin                        : {writable: true, enumerable: false, configurable: true, value: ƒ}
sinh                       : {writable: true, enumerable: false, configurable: true, value: ƒ}
sqrt                       : {writable: true, enumerable: false, configurable: true, value: ƒ}
tan                        : {writable: true, enumerable: false, configurable: true, value: ƒ}
tanh                       : {writable: true, enumerable: false, configurable: true, value: ƒ}
trunc                      : {writable: true, enumerable: false, configurable: true, value: ƒ}
Symbol(Symbol.toStringTag) : {value: "Math", writable: false, enumerable: false, configurable: true}

 

Math 对象的属性

属性描述
E 返回算术常量 e,即自然对数的底数(约等于2.718)。
LN2 返回 2 的自然对数(约等于0.693)。
LN10 返回 10 的自然对数(约等于2.302)。
LOG2E 返回以 2 为底的 e 的对数(约等于 1.414)。
LOG10E 返回以 10 为底的 e 的对数(约等于0.434)。
PI 返回圆周率(约等于3.14159)。
SQRT1_2 返回返回 2 的平方根的倒数(约等于 0.707)。
SQRT2 返回 2 的平方根(约等于 1.414)。

 

1.Math.E:欧拉常数,也是自然对数的底数,约等于 2.718。

Math.E;         // 2.718281828459045

 

2.Math.LN2:2 的自然对数,约等于 0.693。

Math.LN2;       // 0.6931471805599453

 

3.Math.LN10:10 的自然对数,约等于 2.303。

Math.LN10;      // 2.302585092994046

 

4.Math.LOG2E:以 2 为底的 E 的对数,约等于 1.443。

Math.LOG2E;     // 1.4426950408889634

 

5.Math.LOG10E:以 10 为底的 E 的对数,约等于 0.434。

Math.LOG10E;    // 0.4342944819032518

 

6.Math.PI:圆周率,一个圆的周长和直径之比,约等于 3.14159。

Math.PI;        // 3.141592653589793

 

7.Math.SQRT1_2:二分之一 ½ 的平方根,同时也是 2 的平方根的倒数 12,约等于 0.707。

Math.SQRT1_2;   // 0.7071067811865476

 

8.Math.SQRT2:2 的平方根,约等于 1.414。

Math.SQRT2;     // 1.4142135623730951

 

Math 对象的方法

方法描述
abs(x) 返回数的绝对值。
acos(x) 返回数的反余弦值。
asin(x) 返回数的反正弦值。
atan(x) 以介于 -PI/2 与 PI/2 弧度之间的数值来返回 x 的反正切值。
atan2(y,x) 返回从 x 轴到点 (x,y) 的角度(介于 -PI/2 与 PI/2 弧度之间)。
ceil(x) 对数进行上舍入。
cos(x) 返回数的余弦。
exp(x) 返回 e 的指数。
floor(x) 对数进行下舍入。
log(x) 返回数的自然对数(底为e)。
max(x,y) 返回 x 和 y 中的最高值。
min(x,y) 返回 x 和 y 中的最低值。
pow(x,y) 返回 x 的 y 次幂。
random() 返回 0 ~ 1 之间的随机数。
round(x) 把数四舍五入为最接近的整数。
sin(x) 返回数的正弦。
sqrt(x) 返回数的平方根。
tan(x) 返回角的正切。
toSource() 返回该对象的源代码。
valueOf() 返回 Math 对象的原始值。

 

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

示例:
Math.abs();         // NaN
Math.abs(0);        // 0
Math.abs('1');      // 1
Math.abs('-1');     // 1
Math.abs(-2);       // 2
Math.abs(null);     // 0
Math.abs("str");    // NaN

 

2.Math.acos(x):返回一个数的反余弦值(单位为弧度)。
示例:
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(x):返回一个数的反双曲余弦值。
示例:
Math.acosh(0.999999999999);// NaN
Math.acosh(1);// 0
Math.acosh(2);// 1.3169578969248166
Math.acosh(2.5);// 1.566799236972411

 

4.Math.asin(x):返回一个数的反正弦值。
示例:
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(x):返回一个数的反双曲正弦值。
示例:
console.log(Math.asinh(1));// 0.881373587019543
console.log(Math.asinh(0));// 0
console.log(Math.asinh(-1));// -0.881373587019543
console.log(Math.asinh(2));// 1.4436354751788103

 

6.Math.atan(x):返回一个数的反正切值。
示例:
Math.atan(1);  // 0.7853981633974483
Math.atan(0);  // 0

 

7.Math.atanh(x):返回一个数的反双曲正切值。
示例:
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(y, x):返回 y/x 的反正切值。
示例:
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(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.ceil(x):返回大于一个数的最小整数,即一个数向上取整后的值。
示例:
Math.ceil(.95);// 1
Math.ceil(4);// 4
Math.ceil(7.004);// 8
Math.ceil(-7.004);// -7

 

11.Math.clz32(x):返回一个 32 位整数的前导零的数量。
示例:
Math.clz32()                 // 32
Math.clz32(1)                // 31
Math.clz32(1000)             // 22 
Math.clz32(true)             // 31
Math.clz32(3.5)              // 30

 

12.Math.cos(x):返回一个数的余弦值。
示例:
Math.cos(0);           // 1
Math.cos(1);           // 0.5403023058681398
Math.cos(Math.PI);     // -1
Math.cos(2 * Math.PI); // 1

 

13.Math.cosh(x):返回一个数的双曲余弦值。
示例:
Math.cosh(0);  // 1
Math.cosh(1);  // 1.5430806348152437
Math.cosh(-1); // 1.5430806348152437

 

14.Math.exp(x):返回欧拉常数的参数次方,Ex,其中 x 为参数,E 是欧拉常数(2.718...,自然对数的底数)。
示例:
Math.exp(-1); // 0.36787944117144233
Math.exp(0);  // 1
Math.exp(1);  // 2.718281828459045

 

15.Math.expm1(x):返回 exp(x) - 1 的值。
示例:
Math.expm1(-1);   // -0.6321205588285577
Math.expm1(0);    // 0
Math.expm1(1);    // 1.718281828459045
Math.expm1(-38)   // -1
Math.expm1("-38") // -1
Math.expm1("foo") // NaN

 

16.Math.floor(x):返回小于一个数的最大整数,即一个数向下取整后的值。
示例:
Math.floor( 45.95); // 45 
Math.floor( 45.05); // 45 
Math.floor( 4 ); // 4 
Math.floor(-45.05); // -46 
Math.floor(-45.95); // -46

 

17.Math.fround(x):返回最接近一个数的单精度浮点型表示。
示例:
Math.fround(1.5); // 1.5
Math.fround(1.5) === 1.5; // true
Math.fround(1.337); // 1.3370000123977661
Math.fround(1.337) === 1.337; // false
2 ** 150; // 1.42724769270596e+45
Math.fround(2 ** 150); // Infinity
Math.fround('abc'); // NaN
Math.fround(NaN); // NaN

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

 

18.Math.hypot([x[, y[, …]]]):返回其所有参数平方和的平方根。
示例:
Math.hypot(3, 4);// 5
Math.hypot(5, 12);// 13
Math.hypot(3, 4, 5));// 7.0710678118654755
Math.hypot(-5));// 5

 

19.Math.imul(x, y):返回 32 位整数乘法的结果。
示例:
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

 

20.Math.log(x):返回一个数的自然对数(㏒e,即 ㏑)。
示例:
Math.log(-1); // NaN, out of range
Math.log(0); // -Infinity
Math.log(1); // 0
Math.log(10); // 2.302585092994046

 

21.Math.log1p(x):返回一个数加 1 的和的自然对数(㏒e,即 ㏑)。
示例:
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

 

22.Math.log10(x):返回一个数以 10 为底数的对数。
示例:
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

 

23.Math.log2(x):返回一个数以 2 为底数的对数。
示例:
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

 

24.Math.max([x[, y[, …]]]):返回零到多个数值中最大值。
示例:
Math.max(10, 20);   //  20
Math.max(-10, -20); // -10
Math.max(-10, 20);  //  20

 

25.Math.min([x[, y[, …]]]):返回零到多个数值中最小值。
示例:
Math.min(123, 321);// 123
Math.min(-789, 321);// -789
Math.min(-996*-255, 0);// 0

 

26.Math.pow(x, y):返回一个数的 y 次幂。
示例:
Math.pow(7, 3);// 343
Math.pow(4, 0.5);// 2
Math.pow(7, -2);// 0.020408163265306124
Math.pow(-7, 0.5);// NaN

 

27.Math.random():返回一个 0 到 1 之间的伪随机数。
示例:
Math.random()*5    // 返回  1 到 5   之间的数字
Math.random()*10   // 返回  1 到 10  之间的数字
Math.random()*100  // 返回  1 到 100 之间的数字
Math.random()*-100 // 返回-100到 1  之间的数字

function getRandom(min, max) {
  return Math.floor(Math.random() * (max - min + 1) ) + min;
}
getRandom(66, 99)// 返回66-99之间的数字

 

28.Math.round(x):返回四舍五入后的整数。
示例:
Math.round()      // NaN
Math.round(0)     // 0
Math.round(2.6)   // 3
Math.round('2.6') // 3
Math.round(1.9)   // 2
Math.round(-1.6)  // -2
Math.round(1.2)   // 1

 

29.Math.sign(x):返回一个数的符号,得知一个数是正数、负数还是 0。
示例:
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

 

30.Math.sin(x):返回一个数的正弦值。
示例:
Math.sin(0);           // 0
Math.sin(1);           // 0.8414709848078965
Math.sin(Math.PI / 2); // 1

 

31.Math.sinh(x):返回一个数的双曲正弦值。
示例:
Math.sinh(0)      // 0
Math.sinh(1)      // 1.1752011936438014
Math.sinh("-1")   // -1.1752011936438014
Math.sinh("foo")  // NaN

 

32.Math.sqrt(x):返回一个数的平方根。
示例:
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

 

33.Math.tan(x):返回一个数的正切值。
示例:
Math.tan();                 // NaN
Math.tan(0* Math.PI/180);   // 0
Math.tan(15* Math.PI/180);  // 0.2679491924311227
Math.tan(45* Math.PI/180);  // 0.9999999999999999
Math.tan(-90* Math.PI/180); // -16331239353195370
Math.tan(90* Math.PI/180);  // 16331239353195370
Math.tan(180* Math.PI/180); // -1.2246467991473532e-16
Math.tan(360* Math.PI/180); // -2.4492935982947064e-16

 

34.Math.tanh(x):返回一个数的双曲正切值。
示例:
Math.tanh(0);        // 0
Math.tanh(Infinity); // 1
Math.tanh(1);        // 0.7615941559557649

 

35.Math.trunc(x):返回一个数的整数部分,直接去除其小数点及之后的部分。
示例:
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

 

 

 

JavaScript 中的三大对象 (本地对象、内置对象、 宿主对象)

本地对象

 

内置对象

 

宿主对象

 

posted @ 2020-06-03 15:41  elfpower  阅读(1657)  评论(0编辑  收藏  举报