JavaScript Math 对象
JavaScript Math 对象
Section titled “JavaScript Math 对象”JavaScript 内置的 Math 对象提供了执行数学运算的属性(properties)和方法(methods)。它不是一个构造函数(constructor),因此你不能创建 Math 的实例。它的所有属性和方法都是静态的(static),可以直接通过 Math 访问。
常用 Math 属性(常量)
Section titled “常用 Math 属性(常量)”JavaScript 提供了几个内置的数学常量:
console.log(Math.PI); // Output: 3.141592653589793 (PI)console.log(Math.E); // Output: 2.718281828459045 (Euler's number)console.log(Math.SQRT2); // Output: 1.4142135623730951 (Square root of 2)console.log(Math.SQRT1_2); // Output: 0.7071067811865476 (Square root of 1/2)console.log(Math.LN2); // Output: 0.6931471805599453 (Natural logarithm of 2)console.log(Math.LN10); // Output: 2.302585092994046 (Natural logarithm of 10)console.log(Math.LOG2E); // Output: 1.4426950408889634 (Base 2 logarithm of E)console.log(Math.LOG10E); // Output: 0.4342944819032518 (Base 10 logarithm of E)Math.min() 和 Math.max(): 查找参数列表中的最小值或最大值。
console.log(Math.min(0, 150, 30, 20, -8, -200)); // Output: -200console.log(Math.max(0, 150, 30, 20, -8, -200)); // Output: 150
// To find min/max in an array, use the spread operator:const numbers = [10, 5, 100, 2, 25];console.log(Math.min(...numbers)); // Output: 2console.log(Math.max(...numbers)); // Output: 100Math.abs(x): 返回 x 的绝对值(正值)。
console.log(Math.abs(-7.25)); // Output: 7.25Math.pow(x, y): 返回 x 的 y 次幂(xy)。
console.log(Math.pow(2, 3)); // Output: 8 (2 * 2 * 2)console.log(Math.pow(4, 0.5)); // Output: 2 (Square root of 4)Math.sqrt(x): 返回 x 的平方根。
console.log(Math.sqrt(9)); // Output: 3Math.cbrt(x): 返回 x 的立方根(ES6+)。
console.log(Math.cbrt(27)); // Output: 3Math.sign(x): 返回一个数的符号,指示 x 是正数、负数还是零(ES6+)。(正数返回 1,负数返回 -1,零返回 0)
console.log(Math.sign(10)); // Output: 1console.log(Math.sign(-5)); // Output: -1console.log(Math.sign(0)); // Output: 0Math.round(x): 将 x 四舍五入到最接近的整数。
console.log(Math.round(4.7)); // Output: 5console.log(Math.round(4.4)); // Output: 4console.log(Math.round(4.5)); // Output: 5 (rounds .5 up)Math.ceil(x): 将 x 向上舍入到最接近的整数。
console.log(Math.ceil(4.4)); // Output: 5console.log(Math.ceil(4.0)); // Output: 4Math.floor(x): 将 x 向下舍入到最接近的整数。
console.log(Math.floor(4.7)); // Output: 4console.log(Math.floor(4.0)); // Output: 4Math.trunc(x): 通过移除小数部分返回 x 的整数部分(ES6+)。
console.log(Math.trunc(4.7)); // Output: 4console.log(Math.trunc(-4.2)); // Output: -4Math.random(): 返回一个介于 0(包含)和 1(不包含)之间的伪随机浮点数。
console.log(Math.random()); // e.g., 0.123456789
// To get a 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;}console.log(getRandomInt(1, 10)); // Random integer between 1 and 10三角函数和对数函数
Section titled “三角函数和对数函数”Math 对象还包括标准三角函数(如 Math.sin()、Math.cos()、Math.tan()、Math.asin()、Math.acos()、Math.atan()、Math.atan2())和对数函数(Math.log()(自然对数)、Math.log10()(以 10 为底的对数,ES6+)、Math.log2()(以 2 为底的对数,ES6+)、Math.exp()(Ex))。
三角函数的参数通常以弧度为单位。
console.log(Math.sin(Math.PI / 2)); // Output: 1 (sin(90 degrees))console.log(Math.log(Math.E)); // Output: 1 (ln(e))console.log(Math.log10(100)); // Output: 2 (log base 10 of 100)Math 对象方法概览
Section titled “Math 对象方法概览”完整 Math 参考
Section titled “完整 Math 参考”要获取包含所有 Math 属性和方法的详细描述和示例的完整参考,请查阅 MDN Web 文档中关于 JavaScript Math 对象的内容。