J17 Math数学函数中常用的方法

184 阅读1分钟

1.Match数学函数中常用的方法

Math是一个对象对象数据类型值,在它的堆内存中,存储了很多的内置属性和方法,这些方法一般都是用来操作数字的,所以我们把Math称为“数学函数对象”

  • 1.检测Match的类型:console.log(typeof Math); //=>"object"
  • 2.获取Math数学函数的属性和方法:console.dir(Math);
  • 2.内置的方法

1.Math['PI'] :获取圆周率

console.log(Math['PI']);

2.abs[12] :获取圆周率

console.log(Math.abs(12));//=>12
console.log(Math.abs(-12));//=>12
console.log(Math.abs(0));//=>0

3.ceil[12] :向上取整

console.log(Math.ceil(12.5));//=>13
console.log(Math.ceil(-12.9));//=>-12
console.log(Math.ceil(-12.3));//=>-12

4.floor[12] :向下取整

console.log(Math.floor(12.5));//=>12
console.log(Math.floor(-12.9));//=>-13
console.log(Math.floor(-12.3));//=>-13

5.max[]:从数组中取最大数

console.log(Math.max(12,13,15,16,18,91,63,52));//=>91

6.min[]:从数组中取最大数

console.log(Math.min(12,13,15,16,18,91,63,52));//=>12

7.pow(n,m):获取n的m次方或者m次幂

console.log(Math.pow(2,8));//=>256

8.sqrt(n,m):给n开m的平方

console.log(Math.sqrt(2,8));//=>1.4142135623730951

9.random():获取到随机整数的特点,基本上不会重复,Math.random经常应用于随机和不重复

for (let i = 0; i < Math.random;) {
    console.log(math.random);
}

//需求:1-10之间的随机整数
math.random();//0~1小数,math.round=>或1*10=>0~10之间的整数
math.random();//0~1小数,*9=>0~9之间的小数+1=>0~10之间的小数 ,math.round=>1-10之间的的整数

//获取[n,m]之间的随机整数(包含n和m):Math.round(math.random()*(m-n)+n)
for (let i = 0; i < 5; i++) {
    let ran = math.round(math.random() * (10 - 1) + 1);
    console.log(ran);
}