

注:本文为 《 JavaScript 完全手册(2018版) 》第33节,你可以查看该手册的完整目录。
Math 对象包含许多与数学相关的实用程序。我们在这里看看都有哪些。
常量
相目 | 描述 |
---|---|
Math.E |
常数e,自然对数的基数(表示~2.71828) |
Math.LN10 |
表示 10 的自然对数,约为 2.302 |
Math.LN2 |
表示 2 的自然对数,约为 0.693 |
Math.LOG10E |
表示以 10 为底数,e 的对数,约为 0.434 |
Math.LOG2E |
表示以 2 为底数,e 的对数,约为 1.442 |
Math.PI |
表示一个圆的周长与直径的比例,约为 3.14159 |
Math.SQRT1_2 |
表示 1/2 的平方根,约为 0.707 |
Math.SQRT2 |
表示 2 的平方根,约为 1.414 |
函数
所有这些函数方法都是静态的,Math 不能被实例化。
Math.abs()
返回数字的绝对值
Math.abs(2.5) //2.5 Math.abs(-2.5) //2.5
Math.acos()
返回反余弦值,参数必须在 -1 到 1 之间。
Math.acos(0.8) //0.6435011087932843
Math.asin()
返回反正弦值,参数必须在 -1 到 1 之间。
Math.asin(0.8) //0.9272952180016123
Math.atan()
返回反正切值
Math.atan(30) //1.5374753309166493
Math.atan2()
返回其参数商的反正切值
Math.atan2(30, 20) //0.982793723247329
Math.ceil()
向上取整
Math.ceil(2.5) //3 Math.ceil(2) //2 Math.ceil(2.1) //3 Math.ceil(2.99999) //3
Math.cos()
用弧度表示角度的余弦值
Math.cos(0) //1 Math.cos(Math.PI) //-1
Math.exp()
返回 Math.E 的参数次方
Math.exp(1) //2.718281828459045 Math.exp(2) //7.38905609893065 Math.exp(5) //148.4131591025766
Math.floor()
向下取整
Math.ceil(2.5) //2 Math.ceil(2) //2 Math.ceil(2.1) //2 Math.ceil(2.99999) //2
Math.log()
返回基数 e 的自然对数
Math.log(10) //2.302585092994046 Math.log(Math.E) //1
Math.max()
返回传入的一系列数字中的最大值
Math.max(1,2,3,4,5) //5 Math.max(1) //1
Math.min()
返回传入的一系列数字中的最小值
Math.min(1,2,3,4,5) //1 Math.min(1) //1
Math.pow()
返回第一个参数的第二参数次方
Math.pow(1, 2) //1 Math.pow(2, 1) //2 Math.pow(2, 2) //4 Math.pow(2, 4) //16
Math.random()
返回 0.0 到 1.0 之间的伪随机值
Math.random() //0.9318168241227056 Math.random() //0.35268950194094395
Math.round()
四舍五入
Math.round(1.2) //1 Math.round(1.6) //2
Math.sin()
用弧度计算角度的正弦值
Math.sin(0) //0 Math.sin(Math.PI) //1.2246467991473532e-16)
Math.sqrt()
开方
Math.sqrt(4) //2 Math.sqrt(16) //4 Math.sqrt(5) //2.23606797749979
Math.tan()
用弧度计算角度的正切值
Math.tan(0) //0 Math.tan(Math.PI) //-1.2246467991473532e-16
最新评论
写的挺好的
有没有兴趣翻译 impatient js? https://exploringjs.com/impatient-js/index.html
Flexbox playground is so great!
感谢总结。
awesome!
这个好像很早就看到类似的文章了
比其他的教程好太多了
柯理化讲的好模糊…没懂