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)
E
- The
Math.E property represents Euler's number, the base of natural logarithms, e, which is approximately 2.718
console.log(Math.E)
Math 常用方法
随机 random()
console.log(Math.random())
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);
console.log(Math.round(5.95), Math.round(5.5), Math.round(5.05));
console.log(Math.round(-5.05), Math.round(-5.5), Math.round(-5.95));
- 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))
console.log(Math.ceil(4.2))
console.log(Math.ceil(-4.2))
console.log(Math.ceil(-4.5))
- 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));
console.log(Math.floor(5.05));
console.log(Math.floor(5));
console.log(Math.floor(-5.05));
绝对值 abs()
- The
Math.abs() function returns the absolute value of a number.
console.log(Math.abs(-5.2))
console.log(Math.abs(5.49))
console.log(Math.abs(5.5))
var absoluteValue = (x,y) => Math.abs(x - y)
console.log(absoluteValue(6,9))
console.log(absoluteValue(6.4,9.9))
平方根 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))
console.log(calcHypotenus(5,12))
console.log(Math.sqrt(4))
console.log(Math.sqrt(-9))
指数 pow()
- The
Math.pow() 函数返回基数(base)的指数(exponent)次幂,即 base^exponent
- `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))
console.log(Math.pow(-2,2))
console.log(Math.pow(2,8))
console.log(Math.pow(2,10))
console.log(Math.pow("2",2))
console.log(Math.pow(2,"2"))
console.log(Math.pow(9,-2))
console.log(Math.pow(-9,-2))
console.log(Math.pow(2,2.2))
console.log(Math.pow("二","2"))
console.log(Math.pow("二","二"))
console.log(Math.pow(1,Infinity))
console.log(Math.pow(-1,Infinity))
console.log(Math.pow(-2,2.2))
最值 max() min() 多个参数,返回最大/最小的那个
- The
Math.max() function returns the largest of the numbers given as input parameters, or -Infinity if there are no parameters.
console.log(Math.max());
console.log(Math.max(10,20,30));
const array1 = [1, 3, 2];
console.log(...array1)
console.log(Math.max(array1))
console.log(Math.max(...array1))
示例- 随机整数
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))