JS09 - Math数学对象、常用属性、常用方法

204 阅读2分钟

Math 概述

  • Math is a built-in object that has properties and methods for mathematical constants and functions. It's not a function object.
  • Math works with the Number type. It doesn't work with BigInt.
  • Unlike many other global objects, Math is not a constructor. All properties and methods of Math are static. You refer to the constant pi as Math.PI and you call the sine function as Math.sin(x), where x is the method's argument. Constants are defined with the full precision of real numbers in JavaScript.

Math 常用属性

PI

  • The Math.PI property represents the ratio of the circumference of a circle to its diameter, approximately 3.14159
console.log(Math.PI)    //3.141592653589793

E

  • The Math.E property represents Euler's number, the base of natural logarithms, e, which is approximately 2.718
console.log(Math.E)     //2.718281828459045

Math 常用方法

随机 random()

  • Math.random()
//syntax: Math.random()
//return: A floating-point, pseudo-random number between 0 (inclusive) and 1 (exclusive)
//Getting a random number between 0 (inclusive) and 1 (exclusive)
console.log(Math.random())

/**Getting a random number between two values:
 * This example returns a random number between the specified values. 
 * The returned value is no lower than (and may possibly equal) min,
 * and is less than (and not equal) max.
 */
function getRandomArbitrary(max,min){
    return Math.random() * (max - min) + min;
}
console.log(getRandomArbitrary(10,15));

取整 round() ceil() floor()

  • The Math.round()  function returns the value of a number rounded to the nearest integer.
//四舍五入取整
console.log(Math.round(0.9),Math.round(-0.49);
// expected output: 1 -0
console.log(Math.round(5.95), Math.round(5.5), Math.round(5.05));
// expected output: 6 6 5
console.log(Math.round(-5.05), Math.round(-5.5), Math.round(-5.95));
// expected output: -5 -5 -6
  • The Math.ceil()  function always rounds up and returns the smaller integer greater than or equal to a given number.
//向上取整
console.log(Math.ceil(.95))     //1
console.log(Math.ceil(4.2))     //5
console.log(Math.ceil(-4.2))    //-4
console.log(Math.ceil(-4.5))    //-4
  • The Math.floor()  function always rounds down and returns the largest integer less than or equal to a given number.
//向下取整
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

绝对值 abs()

  • The Math.abs()  function returns the absolute value of a number.
console.log(Math.abs(-5.2))         //5.2
console.log(Math.abs(5.49))         //5.49
console.log(Math.abs(5.5))          //5.5
var absoluteValue = (x,y) => Math.abs(x - y)
console.log(absoluteValue(6,9))     //3
console.log(absoluteValue(6.4,9.9)) //3.5

平方根 sqrt()

  • The Math.sqrt()  function returns the square root of a number.
var calcHypotenus = (x,y) => Math.sqrt(x*x + y*y)
console.log(calcHypotenus(3,4)) //5
console.log(calcHypotenus(5,12)) //13
console.log(Math.sqrt(4))       //2
console.log(Math.sqrt(-9))      //NaN

指数 pow()

  • The Math.pow()  函数返回基数(base)的指数(exponent)次幂,即 base^exponent
//syntax: Math.pow(base, exponent)
//Returns NaN in one of the following cases:
-   `base` or `exponent` is `NaN`.
-   `base` is ±1 and `exponent` is ±`Infinity`.
-   `base < 0` and `exponent` is not an integer.
console.log(Math.pow(4,2))      //16
console.log(Math.pow(-2,2))     //4
console.log(Math.pow(2,8))     //256
console.log(Math.pow(2,10))     //1024
console.log(Math.pow("2",2))    //4
console.log(Math.pow(2,"2"))    //4
console.log(Math.pow(9,-2))     //0.012345679012345678...
console.log(Math.pow(-9,-2))    //0.012345679012345678...
console.log(Math.pow(2,2.2))    //NaN
//
console.log(Math.pow("二","2"))    //NaN
console.log(Math.pow("二","二"))    //NaN
//
console.log(Math.pow(1,Infinity))   //NaN
console.log(Math.pow(-1,Infinity))  //NaN
//
console.log(Math.pow(-2,2.2))   //NaN

最值 max() min() 多个参数,返回最大/最小的那个

  • The Math.max()  function returns the largest of the numbers given as input parameters, or -Infinity if there are no parameters.
/**Math.max()
 * Math.max(value0)
 * Math.max(value0, value1)
 * Math.max(value0, value1, …, valueN)
 */
 console.log(Math.max());           //-Infinity
 console.log(Math.max(10,20,30));   //30
 const array1 = [1, 3, 2];
 console.log(...array1)             //...=>遍历出数组元素
 console.log(Math.max(array1))      //NaN
 console.log(Math.max(...array1))   //3

示例- 随机整数

  • 取两个数字之间的随机整数,需要包含这两个数本身
function getRandomNumber(min,max){
    if(min > max){
        return console.error("getRandomNumber.min is larger than getRandomNumber.max");
    } else {
        let result = Math.floor(Math.random()*(max-min+1))+Math.floor(min)
        return result
    }
}
console.log(getRandomNumber(11,12))