ES6 - Math 对象
ES6+ - Math 对象
Section titled “ES6+ - Math 对象”JavaScript 中内置的 Math 对象提供了用于数学常数和函数的属性 (property) 和方法 (method)。Math 不是一个构造函数 (constructor);它所有的属性和方法都是静态 (static) 的。你可以直接通过 Math 对象本身调用它们(例如 Math.PI,Math.sqrt(x))。
Math 属性(常数)
Section titled “Math 属性(常数)”这些是常见的数学常数:
| 属性 | 描述 |
|---|---|
Math.E | 欧拉常数,自然对数的底数(约 2.718)。 |
Math.LN2 | 2 的自然对数(约 0.693)。 |
Math.LN10 | 10 的自然对数(约 2.302)。 |
Math.LOG2E | E 的以 2 为底的对数(约 1.442)。 |
Math.LOG10E | E 的以 10 为底的对数(约 0.434)。 |
Math.PI | 圆周长与直径之比(约 3.14159)。 |
Math.SQRT1_2 | 1/2 的平方根(约 0.707)。 |
Math.SQRT2 | 2 的平方根(约 1.414)。 |
用于计算指数和根的函数:
| 函数 | 描述 |
|---|---|
Math.pow(x, y) | 返回 x 的 y 次幂。 |
Math.sqrt(x) | 返回 x 的正平方根。 |
Math.cbrt(x) (ES6) | 返回 x 的立方根。 |
Math.exp(x) | 返回 e^x,其中 e 是欧拉常数 (Math.E)。 |
Math.expm1(x) (ES6) | 返回 e^x - 1。对于较小的 x,它比 Math.exp(x) - 1 更精确。 |
Math.hypot(...values) (ES6) | 返回其参数平方和的平方根(例如 Math.hypot(3, 4) 返回 5)。 |
用于计算对数的函数。在 JavaScript 中,Math.log() 指的是自然对数。
| 函数 | 描述 |
|---|---|
Math.log(x) | 返回 x 的自然对数(以 e 为底)。 |
Math.log10(x) (ES6) | 返回 x 的以 10 为底的对数。 |
Math.log2(x) (ES6) | 返回 x 的以 2 为底的对数。 |
Math.log1p(x) (ES6) | 返回 1 + x 的自然对数。对于较小的 x,它比 Math.log(1 + x) 更精确。 |
舍入和符号函数
Section titled “舍入和符号函数”用于舍入数字和确定其符号的函数:
| 函数 | 描述 |
|---|---|
Math.abs(x) | 返回 x 的绝对值(非负值)。 |
Math.sign(x) (ES6) | 返回 x 的符号:负数返回 -1,正数返回 +1,0 返回 0,-0 返回 -0,NaN 返回 NaN。 |
Math.ceil(x) | 返回大于或等于 x 的最小整数(向上取整)。 |
Math.floor(x) | 返回小于或等于 x 的最大整数(向下取整)。 |
Math.round(x) | 返回四舍五入到最接近整数的 x(0.5 向上取整)。 |
Math.trunc(x) (ES6) | 通过移除小数部分来返回 x 的整数部分(不进行舍入)。 |
Math.min(...values) | 返回零个或多个数字中最小的一个。 |
Math.max(...values) | 返回零个或多个数字中最大的一个。 |
标准三角函数。所有角度都以弧度为单位。
| 函数 | 描述 |
|---|---|
Math.sin(x) | x 的正弦(以弧度为单位)。 |
Math.cos(x) | x 的余弦(以弧度为单位)。 |
Math.tan(x) | x 的正切(以弧度为单位)。 |
Math.asin(x) | x 的反正弦(反函数);结果以弧度为单位。 |
Math.acos(x) | x 的反余弦(反函数);结果以弧度为单位。 |
Math.atan(x) | x 的反正切(反函数);结果以弧度为单位。 |
Math.atan2(y, x) | 商 y/x 的反正切;结果以弧度为单位,并考虑 y 和 x 的符号来确定象限。 |
Math.sinh(x) (ES6) | x 的双曲正弦。 |
Math.cosh(x) (ES6) | x 的双曲余弦。 |
Math.tanh(x) (ES6) | x 的双曲正切。 |
Math.asinh(x) (ES6) | x 的反双曲正弦。 |
Math.acosh(x) (ES6) | x 的反双曲余弦。 |
Math.atanh(x) (ES6) | x 的反双曲正切。 |
Math.random()
Section titled “Math.random()”Math.random() 函数返回一个介于 0(包含)到 1(不包含)之间的浮点型伪随机数。
示例:生成随机数
Section titled “示例:生成随机数”// Random number between 0 (inclusive) and 1 (exclusive)const randomNumber01 = Math.random();console.log(`Random number [0, 1): ${randomNumber01}`);
// Random integer between min (inclusive) and max (inclusive)function getRandomInt(min, max) { min = Math.ceil(min); max = Math.floor(max); return Math.floor(Math.random() * (max - min + 1)) + min;}
const randomDieRoll = getRandomInt(1, 6);console.log(`Random die roll (1-6): ${randomDieRoll}`);
const randomLargeInt = getRandomInt(50, 100);console.log(`Random integer (50-100): ${randomLargeInt}`);输出(会变化):
Section titled “输出(会变化):”Random number [0, 1): 0.xxxxxxxxxxxxxxxRandom die roll (1-6): (一个介于 1 到 6 之间的数字)Random integer (50-100): (一个介于 50 到 100 之间的数字)Math.random() 不适合用于加密安全的随机数。请使用 Web Crypto API 中的 Crypto.getRandomValues() 方法来代替。