属性 | 描述 |
---|---|
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
方法 | 描述 |
---|---|
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
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
Math.acosh(0.999999999999);// NaN Math.acosh(1);// 0 Math.acosh(2);// 1.3169578969248166 Math.acosh(2.5);// 1.566799236972411
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
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
Math.atan(1); // 0.7853981633974483 Math.atan(0); // 0
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
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.
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
Math.ceil(.95);// 1 Math.ceil(4);// 4 Math.ceil(7.004);// 8 Math.ceil(-7.004);// -7
Math.clz32() // 32 Math.clz32(1) // 31 Math.clz32(1000) // 22 Math.clz32(true) // 31 Math.clz32(3.5) // 30
Math.cos(0); // 1 Math.cos(1); // 0.5403023058681398 Math.cos(Math.PI); // -1 Math.cos(2 * Math.PI); // 1
Math.cosh(0); // 1 Math.cosh(1); // 1.5430806348152437 Math.cosh(-1); // 1.5430806348152437
Math.exp(-1); // 0.36787944117144233 Math.exp(0); // 1 Math.exp(1); // 2.718281828459045
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
Math.floor( 45.95); // 45 Math.floor( 45.05); // 45 Math.floor( 4 ); // 4 Math.floor(-45.05); // -46 Math.floor(-45.95); // -46
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
Math.hypot(3, 4);// 5 Math.hypot(5, 12);// 13 Math.hypot(3, 4, 5));// 7.0710678118654755 Math.hypot(-5));// 5
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
Math.log(-1); // NaN, out of range Math.log(0); // -Infinity Math.log(1); // 0 Math.log(10); // 2.302585092994046
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
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
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
Math.max(10, 20); // 20 Math.max(-10, -20); // -10 Math.max(-10, 20); // 20
Math.min(123, 321);// 123 Math.min(-789, 321);// -789 Math.min(-996*-255, 0);// 0
Math.pow(7, 3);// 343 Math.pow(4, 0.5);// 2 Math.pow(7, -2);// 0.020408163265306124 Math.pow(-7, 0.5);// NaN
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之间的数字
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
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
Math.sin(0); // 0 Math.sin(1); // 0.8414709848078965 Math.sin(Math.PI / 2); // 1
Math.sinh(0) // 0 Math.sinh(1) // 1.1752011936438014 Math.sinh("-1") // -1.1752011936438014 Math.sinh("foo") // NaN
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
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
Math.tanh(0); // 0 Math.tanh(Infinity); // 1 Math.tanh(1); // 0.7615941559557649
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
原文:https://www.cnblogs.com/elfpower/p/12711205.html