JavaScript 中 Math 对象的用途是什么?
Math 是一个内置对象,具有数学函数和常量的属性和方法。它不是一个函数对象。 Math 对象适用于 Number 类型。 Math 对象没有构造函数。 Math 的所有属性和方法都是固定的/静态的。余弦函数称为 Math.cos(y),而常数 pi 称为 Math.PI,其中 y 是方法的参数。 Math 的所有属性和方法都是静态的,可以通过将 Math 用作对象来调用,而无需创建它。在本文中,我们将讨论 Javascript 中使用的各种可用方法和属性。我们将从 Javascript 中的数学属性开始。
静态数学属性:数学属性及其描述如下:
句法:
Math.property
Property | Description | Return value |
---|---|---|
Math.E | Euler’s constant, the base of natural logarithms is approximately 2.718. | Euler’s number |
Math.LN2 | Natural logarithm of 2 which is approximately 0.693147180. | natural logarithm of 2 |
Math.LN10 | Natural logarithm of 10 which is approximately 2.302585. | natural logarithm of 10 |
Math.LOG2E | Base-2 logarithm of E which is approximately 1.442695. | base 2 logarithms of E |
Math.LOG10E | Base-10 logarithm of E which is approximately 0.43429844. | base 10 logarithms of E |
Math.PI | The ratio of the circumference of a circle to its diameter i.e. 3.14159. | PI value |
Math.SQRT1_2 | The square root of 1/2 is approximately 0.70710678. | The square root of 1/2 |
Math.SQRT2 | The square root of 2 is approximately 1.41421356. | The square root of 2 |
例子:
HTML
Document
HTML
Document
输出:
静态数学方法:下面列出了与数学对象关联的方法及其描述。
句法:
Math.method(number)
Method | Description |
---|---|
Math.abs(y) | The positive value of y is returned. |
Math.acos(y) | The arccosine of y is returned. |
Math.acosh(y) | Hyperbolic arccosine of y is returned. |
Math.asin(y) | The arcsine of y is returned. |
Math.asinh(y) | A number’s hyperbolic arcsine is returned. |
Math.atan(y) | The arctangent of y is returned. |
Math.atanh(y) | Returns the hyperbolic arctangent of y. |
Math.atan2(y, x) | The arctangent of the quotient of its arguments is returned. |
Math.cbrt(y) | Returns the cube root of y. |
Math.ceil(y) | Returns the smallest integer greater than or equal to y. |
Math.clz32(y) | Returns the number of leading zero bits of the 32-bit integer y. |
Math.cos(y) | Returns the cosine of the angle y. |
Math.cosh(y) | Returns the hyperbolic cosine of y. |
Math.exp(y) | Returns e^(y), with x being the input and e being Euler’s constant (2.718…, the natural logarithm’s base). |
Math.expm1(y) | Returns subtracting 1 from exp(y). |
Math.floor(y) | Returns the largest integer less than or equal to y. |
Math.fround(y) | The nearest number’s single-precision float representation is returned. |
Math.hypot([x[, y[, …]]]) | Returns the square root of the sum of squares of its parameters. |
Math.imul(x, y) | The result of the 32-bit integer multiplication of x and y is returned. |
Math.log(y) | Returns the natural logarithm of the number. |
Math.log1p(y) | For a number y, the natural logarithm of 1 + y is returned. |
Math.log10(y) | The base-10 logarithm of y. is returned. |
Math.log2(y) | The base-2 logarithm of y is returned. |
Math.max([x[, y[, …]]]) | Largest number is returned from x,y. |
Math.min([x[, y[, …]]]) | The smallest of all the numbers is returned from x,y. |
Math.pow(x, y) | Returns the exponent power y of the base value x (that is, x^y). |
Math.random() | An arbitrary number between 0 and 1 is returned. |
Math.round(y) | The value of y, rounded to the closest integer, is returned. |
Math.sign(y) | The sign of y, which indicates whether y is positive, negative, or zero is returned. |
Math.sin(y) | The sine of the angle y is returned. |
Math.sinh(y) | Hyperbolic sine of y is returned. |
Math.sqrt(y) | The positive square root of y is returned. |
Math.tan(y) | The tangent of y is returned. |
Math.tanh(y) | The hyperbolic tangent of y is returned. |
Math.trunc(y) | Removes all fractional digits from x and returns the integer part of it. |
例子:
HTML
Document
输出: