Math对象——Math.min()&Math.ceil()&Math.floor(

Math称之为数学函数,它也是对象数据类型的,主要是用来操作数字的

1、Math.abs()求绝对值

Math.abs(-1)

2、Math.ceil/Math.floor向上取整,向下取整

向上取整,无论是正数还是负数,都取最大的值

向下取整,无论是正数还是负数,都取最小的值

Math.ceil(1.2)

2

Math.ceil(-1.6)

-1

Math.floor(1.8)

1

Math.floor(-1.1)

-2

3、Math.round()四舍五入

正数的话,还是正常的,之前理解的,但是如果是负数,临界点必须大于5

Math.round(1.5)

2

Math.round(-1.5)

-1

Math.round(-1.51)

-2

4、Math.sqrt()开平方

Math.sqrt(9)

3

5、Math.pow(n,m)取幂

n的m次幂

Math.pow(3,2)  ==> 9

6、Math.PI

Math.PI  ===>3.141592653589793

7、Math.max/Math.min 获取最大值和最小值

Math.max(1,2,3)

Math.min(4,5,6)

8、Math.random()获取0~1之间的随机数(大于等于0小于1)

获取n到m之间的随机数:Math.random()*(m-n)+n;

//获取10到20之间的随机输

Math.random()*10+10

Math.random()方法返回介于0到1之间一个随机数,不包括0和1。如果想大于这个范围的话,可以套用一下公式:

值 = Math.floor(Math.random() * 总数 + 第一个值)

例如:

Math.floor(Math.random() * 10 + 1);        //随机产生1-10之间的任意数

Math.floor(Math.random() * 10 + 5)        //5-14之间的任意数

a.Math.random是取[0,1]的数;

b.取[min,max]的随机整数时使用如下公式:

Math.floor(Math.random().(max-min+1)+min)

c取[min,max]的随机整数时使用以下公式

Math.floor(Math.random().(max-min)+min)

d取[min,max]的随机整数时使用如下公式

Math.floor(Math.random().(max-min)+min+1)

1

**方法**

Math.abs(num)//返回num的绝对值

Math.exp(num)//返回Math.E的num次幂

Math.log(num)//返回num的自然对数

Math.pow(num,power)//返回num的power次幂

Math.sqrt(num)//返回num的平方根

Math.acos(x)//返回x的反余弦值

Math.asin(x)//返回x的反正弦值

Math.atan(x)//返回x的反正切值

Math.atan2(y,x)//返回y/x的反正切值

Math.cos(x)//返回x的余弦值

Math.sin(x)//返回x的正弦值

Math.tan(x)//返回x的正切值

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容