# Math Math 是一个内置对象,它拥有一些数学常数属性和数学函数方法。 ## 静态属性 ### E ```ts function getNapier():number { return Math.E; } console.log(getNapier()); // expected output: 2.718281828459045 ``` ### LN10 ```ts function getNatLog10():number { return Math.LN10; } console.log(getNatLog10()); // expected output: 2.302585092994046 ``` ### LN2 ```ts function getNatLog2():number { return Math.LN2; } console.log(getNatLog2()); // expected output: 0.6931471805599453 ``` ### LOG2E ```ts function getLog2e():number { return Math.LOG2E; } console.log(getLog2e()); // expected output: 1.4426950408889634 ``` ### LOG10E ```ts function getLog10e():number { return Math.LOG10E; } console.log(getLog10e()); // expected output: 0.4342944819032518 ``` ### PI ```ts function calculateCircumference (radius:number):number { return 2 * Math.PI * radius; } console.log(calculateCircumference(1)); // expected output: 6.283185307179586 ``` ### SQRT1_2 ```ts function getRoot1_2():number { return Math.SQRT1_2; } console.log(getRoot1_2()); // expected output: 0.7071067811865476 ``` ### SQRT2 ```ts function getRoot2():number { return Math.SQRT2; } console.log(getRoot2()); // expected output: 1.4142135623730951 ``` ## 静态方法 ### clz32(x) ```ts console.log(Math.clz32(1)); // expected output: 31 console.log(Math.clz32(1000)); // expected output: 22 console.log(Math.clz32()); // expected output: 32 console.log(Math.clz32(3.5)); // expected output: 30 ``` ### sign(x) ```ts console.log(Math.sign(3)); // expected output: 1 console.log(Math.sign(-3)); // expected output: -1 console.log(Math.sign(0)); // expected output: 0 ``` ### log10(x) ```ts console.log(Math.log10(10)); // expected output: 1 console.log(Math.log10(100)); // expected output: 2 console.log(Math.log10(1)); // expected output: 0 ``` ### log2(x) ```ts console.log(Math.log2(2)); // expected output: 1 console.log(Math.log2(1024)); // expected output: 10 console.log(Math.log2(1)); // expected output: 0 ``` ### log1p(x) ```ts console.log(Math.log1p(Math.E-1)); // expected output: 1 console.log(Math.log1p(0)); // expected output: 0 ``` ### expm1(x) ```ts console.log(Math.expm1(1)); // expected output: 1.718281828459045 console.log(Math.expm1(-38)); // expected output: -1 ``` ### cosh(x) ```ts console.log(Math.cosh(0)); // expected output: 1 console.log(Math.cosh(1)); // expected output: 1.5430806348152437 console.log(Math.cosh(-1)); // expected output: 1.5430806348152437 ``` ### sinh(x) ```ts console.log(Math.sinh(0)); // expected output: 0 console.log(Math.sinh(1)); // expected output: 1.1752011936438014 ``` ### tanh(x) ```ts console.log(Math.tanh(-1)); // Expected output: -0.7615941559557649 console.log(Math.tanh(0)); // Expected output: 0 console.log(Math.tanh(1)); // Expected output: 0.7615941559557649 ``` ### acosh(x) ```ts console.log(Math.acosh(1)); // expected output: 0 console.log(Math.acosh(2)); // expected output: 1.3169578969248166 console.log(Math.acosh(2.5)); // expected output: 1.566799236972411 ``` ### asinh(x) ```ts console.log(Math.asinh(1)); // expected output: 0.881373587019543 console.log(Math.asinh(0)); // expected output: 0 console.log(Math.asinh(-1)); // expected output: -0.881373587019543 console.log(Math.asinh(2)); // expected output: 1.4436354751788103 ``` ### atanh(x) ```ts console.log(Math.atanh(0)); // expected output: 0 console.log(Math.atanh(0.5)); // expected output: 0.5493061443340548 ``` ### trunc(x) ```ts console.log(Math.trunc(13.37)); // Expected output: 13 console.log(Math.trunc(42.84)); // Expected output: 42 console.log(Math.trunc(0.123)); // Expected output: 0 ``` ### fround(x) ```ts console.log(Math.fround(1.5)); // expected output: 1.5 console.log(Math.fround(1.337)); // expected output: 1.3370000123977661 ``` ### abs(x) ```ts function difference(a:number, b:number):number { return Math.abs(a - b); } console.log(difference(3, 5)); // expected output: 2 console.log(difference(5, 3)); // expected output: 2 console.log(difference(1.23456, 7.89012)); // expected output: 6.6555599999999995 ``` ### acos(x) ```ts console.log(Math.acos(-1)); // expected output: 3.141592653589793 console.log(Math.acos(0)); // expected output: 1.5707963267948966 console.log(Math.acos(1)); // expected output: 0 ``` ### asin(x) ```ts console.log(Math.asin(-1)); // expected output: -1.5707963267948966 (-pi/2) console.log(Math.asin(0)); // expected output: 0 console.log(Math.asin(0.5)); // expected output: 0.5235987755982989 console.log(Math.asin(1)); // expected output: 1.5707963267948966 ``` ### atan(x) ```ts console.log(Math.atan(1)); // expected output: 0.7853981633974483 console.log(Math.atan(0)); // expected output: 0 ``` ### atan2(y, x) ```ts console.log(Math.atan2(90, 15)); // expected output: 1.4056476493802699 console.log(Math.atan2(15, 90)); // expected output: 0.16514867741462683 ``` ### ceil(x) ```ts console.log(Math.ceil(0.95)); // expected output: 1 console.log(Math.ceil(4)); // expected output: 4 console.log(Math.ceil(7.004)); // expected output: 8 console.log(Math.ceil(-7.004)); // expected output: -7 ``` ### cos(x) ```ts console.log(Math.cos(0)); // expected output: 1 console.log(Math.cos(1)); // expected output: 0.5403023058681398 ``` ### exp(x) ```ts console.log(Math.exp(-1)); // expected output: 0.36787944117144233 console.log(Math.exp(0)); // expected output: 1 console.log(Math.exp(1)); // expected output: 2.718281828459045 ``` ### floor(x) ```ts console.log(Math.floor(5.95)); // expected output: 5 console.log(Math.floor(5.05)); // expected output: 5 console.log(Math.floor(5)); // expected output: 5 console.log(Math.floor(-5.05)); // expected output: -6 ``` ### log(x) ```ts console.log(Math.log(1)); // expected output: 0 console.log(Math.log(10)); // expected output: 2.302585092994046 ``` ### max(values) ```ts console.log(Math.max(1, 3, 2)); // expected output: 3 console.log(Math.max(-1, -3, -2)); // expected output: -1 ``` ### min(values) ```ts console.log(Math.min(2, 3, 1)); // expected output: 1 console.log(Math.min(-2, -3, -1)); // expected output: -3 ``` ### pow(x, y) ```ts console.log(Math.pow(7, 3)); // expected output: 343 console.log(Math.pow(4, 0.5)); // expected output: 2 ``` ### random() ```ts function getRandomInt(max:number):number { return Math.floor(Math.random() * max); } console.log(getRandomInt(3)); // expected output: 0, 1 or 2 console.log(getRandomInt(1)); // expected output: 0 console.log(Math.random()); // expected output: a number from 0 to <1 ``` ### round(x) ```ts console.log(Math.round(20.49)); // expected output: 20 console.log(Math.round(20.5)); // expected output: 21 console.log(Math.round(-20.5)); // expected output: -20 console.log(Math.round(-20.51)); // expected output: -21 ``` ### sin(x) ```ts console.log(Math.sin(0)); // expected output: 0 console.log(Math.sin(1)); // expected output: 0.8414709848078965 ``` ### sqrt(x) ```ts function calcHypotenuse(a:number, b:number):number { return (Math.sqrt((a * a) + (b * b))); } console.log(calcHypotenuse(3, 4)); // expected output: 5 console.log(calcHypotenuse(5, 12)); // expected output: 13 console.log(calcHypotenuse(0, 0)); // expected output: 0 ``` ### tan(x) ```ts console.log(Math.tan(0)); // expected output: 0 console.log(Math.tan(1)); // expected output: 1.5574077246549023 ```