Math 是一个内置对象,它拥有一些数学常数属性和数学函数方法。Math 不是一个函数对象。Math 用于 Number 类型。它不支持 BigInt。
与其他全局对象不同的是,Math 不是一个构造器。Math 的所有属性与方法都是静态的。引用圆周率的写法是 Math.PI,调用正余弦函数的写法是 Math.sin(x),x 是要传入的参数。Math 的常量是使用 JavaScript 中的全精度浮点数来定义的。
常用的Math函数
Math basic api example
示例代码1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| let mathArr = [5, 1, 10, 3, 6, 5]
const min = Math.min(...mathArr) console.log(`最小值:Math.min(...mathArr) = ${min}`)
const max = Math.max(...mathArr) console.log(`最大值:Math.max(...mathArr) = ${max}`)
const round = Math.round(4.5) console.log(`四舍五入:Math.round(4.5) = ${round}`)
const sqrt = Math.sqrt(4) console.log(`平方根:Math.sqrt(4) = ${sqrt}`)
const pow = Math.pow(2, 3) console.log(`基数的指数次幂:Math.pow(2, 3) = ${pow}`)
const floor = Math.floor(4.9) console.log(`小于给定数字的最大整数:Math.floor(4.9) = ${floor}`)
const random = Math.random() console.log(`随机数:Math.random() = ${random}`)
const cos = Math.cos(180) console.log(`余弦值:Math.cos(180) = ${cos}`)
const sin = Math.sin(90) console.log(`正弦值:Math.sin(90) = ${sin}`)
const ceil = Math.ceil(4.1) console.log(`大于或等于一个给定数字的最小整数:Math.ceil(4.1) = ${ceil}`)
|
更多
MDN Math
Something wrong with this article?
Click
here
to submit your revision.
Vector Landscape Vectors by Vecteezy