Javascript内建对象Math

it2022-05-05  107

Math既不能当做一般函数来调用,也不能用于new操作符来创建对象。

Math的属性都是不可修改的,因此他们都以名字大写的方式来表示自己与一般属性变量的不同。

数字Π:

Math.PI 3.141592653589793

2的平方根:

Math.SQRT2 1.4142135623730951

欧拉常数e:

Math.E 2.718281828459045

2的自然对数:

Math.LN2 0.6931471805599453

10的自然对数:

Math.LN10 2.302585092994046

random()所返回的是0到1之间的某个数,下面给出一些常用示例:

1、获取0到100之间的某个数:

100*Math.random()

2、获取max和min之间的值,可以通过一个公式(

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

)来获取。如获取2到10之间的某个数:

8*Math.random() + 2

如果只需要整数的话,使用Math.floor() 用于舍弃和Math.ceil()用于取入,也可以直接调用Math.round()进行四舍五入:

Math.floor(1.23); 1 Math.floor(1.63); 1 Math.ceil(1.23); 2 Math.ceil(1.63); 2 Math.round(1.23); 1 Math.round(1.63); 2

获取随机数0或1:

Math.round(Math.random());

Math.max()获取最大值、Math.min()获取最小值:

Math.max(2, 5); 5 Math.min(2, 5); 2 Math.max(2, 5, 3, 9, 10,11); 11 Math.min(12, 5, 3, 9, 10,11); 3

示例:对表单中输入的月份进行验证时,可以使用下面方式来确保数据正常工作:

Math.min(Math.max(1, input), 12);

Math的其他数学计算方法:

指数运算(2的3次方):

Math.pow(2,3); 8

平方根:

Math.sqrt(9); 3

正弦、余弦函数:

Math.sin(number); Math.cos(number);

 其中number的为弧度值,返回参数number的正弦值,返回值介于 [-1, 1] 之间。

角度 = 360 * 弧度 / (2 * Math.PI)

化简为:

角度 = 180 * 弧度 / Math.PI

 

转载于:https://www.cnblogs.com/cleam/p/4753466.html

相关资源:各显卡算力对照表!

最新回复(0)