Math
Math è un oggetto precostruito che possiede proprietà e metodi per restituire i risultati di costanti e funzioni matematiche. Math non è una funzione.
Descrizione
Diversamente dagli altri oggetti globali, Math non è un costruttore. Tutte le proprietà e i metodi di Math sono statici. Per riferirsi alla costante Pi si usa Math.PI e per chiamare la funzione Seno si usa Math.sin(x) , dove x è l'argomento del metodo. Le costanti sono definite con la piena precisione dei numeri reali in JavaScript.
Proprietà
Math.E
- Costante di Eulero e base dei logaritmi naturali, approssimativamente 2.718.
Math.LN2
- Logaritmo naturale di 2, approssimativamente 0.693.
Math.LN10
- Logaritmo naturale di 10, approssimativamente 0.693.
Math.LOG2E
- Logaritmo a base 2 di E, approssimativamente 1.443.
Math.LOG10E
- Logaritmo a base 10 di E, approssimativamente 1.443.
Math.PI
- Rapporto tra la circonferenza di un cerchio ed il suo diametro, approssimativamente 3.14159.
Math.SQRT1_2
- Radice quadrata di 1/2 ; equivale a 1 fratto la radice quadrata di 2, approsimativamente 0.707.
Math.SQRT2
- Radice quadrata di 2, approssimativamente 1.414.
Metodi
Ricorda che le funzioni trigonometriche (sin()
, cos()
, tan()
, asin()
, acos()
, atan()
, atan2()
) si aspettano e restituiscono gli angoli in radianti. To convert radians to degrees, divide by (Math.PI / 180)
, and multiply by this to convert the other way.
Ricorda che la precisione di molte delle funzioni matematiche è dipendente dall'sistema di implementazione. questo significa che browser differenti possono dare risultati diversi, e anche lo stessomotore JS su un differente sistema operativo o architettura può dare risultati discordi.
Math.abs(x)
- Returns the absolute value of a number.
Math.acos(x)
- Returns the arccosine of a number.
Math.acosh(x)
- Returns the hyperbolic arccosine of a number.
Math.asin(x)
- Returns the arcsine of a number.
Math.asinh(x)
- Returns the hyperbolic arcsine of a number.
Math.atan(x)
- Returns the arctangent of a number.
Math.atanh(x)
- Returns the hyperbolic arctangent of a number.
Math.atan2(y, x)
- Returns the arctangent of the quotient of its arguments.
Math.cbrt(x)
- Returns the cube root of a number.
Math.ceil(x)
- Returns the smallest integer greater than or equal to a number.
Math.clz32(x)
- Returns the number of leading zeroes of a 32-bit integer.
Math.cos(x)
- Returns the cosine of a number.
Math.cosh(x)
- Returns the hyperbolic cosine of a number.
Math.exp(x)
- Returns Ex, where x is the argument, and E is Euler's constant (2.718…), the base of the natural logarithm.
Math.expm1(x)
- Returns subtracting 1 from
exp(x)
. Math.floor(x)
- Returns the largest integer less than or equal to a number.
Math.fround(x)
- Returns the nearest single precision float representation of a number.
Math.hypot([x[, y[, …]]])
- Returns the square root of the sum of squares of its arguments.
Math.imul(x, y)
- Returns the result of a 32-bit integer multiplication.
Math.log(x)
- Returns the natural logarithm (loge, also ln) of a number.
Math.log1p(x)
- Returns the natural logarithm of
1 + x
(loge, also ln) of a number. Math.log10(x)
- Returns the base 10 logarithm of a number.
Math.log2(x)
- Returns the base 2 logarithm of a number.
Math.max([x[, y[, …]]])
- Returns the largest of zero or more numbers.
Math.min([x[, y[, …]]])
- Returns the smallest of zero or more numbers.
Math.pow(x, y)
- Returns base to the exponent power, that is,
baseexponent
. Math.random()
- Returns a pseudo-random number between 0 and 1.
Math.round(x)
- Returns the value of a number rounded to the nearest integer.
Math.sign(x)
- Returns the sign of the x, indicating whether x is positive, negative or zero.
Math.sin(x)
- Returns the sine of a number.
Math.sinh(x)
- Returns the hyperbolic sine of a number.
Math.sqrt(x)
- Returns the positive square root of a number.
Math.tan(x)
- Returns the tangent of a number.
Math.tanh(x)
- Returns the hyperbolic tangent of a number.
Math.toSource()
- Returns the string
"Math"
. Math.trunc(x)
- Returns the integral part of the number x, removing any fractional digits.
Specifications
Specification | Status | Comment |
---|---|---|
ECMAScript 1st Edition (ECMA-262) | Standard | Initial definition. Implemented in JavaScript 1.1. |
ECMAScript 5.1 (ECMA-262) The definition of 'Math' in that specification. |
Standard | |
ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'Math' in that specification. |
Standard | New methods log10() , log2() , log1p() , expm1() , cosh() , sinh() , tanh() , acosh() , asinh() , atanh() , hypot() , trunc() , sign() , imul() , fround() , cbrt() and clz32() added. |
ECMAScript (ECMA-262) The definition of 'Math' in that specification. |
Living Standard |
Browser compatibility
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Basic support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |