Oggetto matematico JavaScript
L'oggetto JavaScript Math ti consente di eseguire compiti matematici sui numeri.
Esempio
Math.PI;
L'oggetto matematico
A differenza di altri oggetti, l'oggetto Math non ha un costruttore.
L'oggetto Math è statico.
Tutti i metodi e le proprietà possono essere utilizzati senza prima creare un oggetto Math.
Proprietà matematiche (costanti)
La sintassi per qualsiasi proprietà Math è: .Math.property
JavaScript fornisce 8 costanti matematiche a cui è possibile accedere come proprietà matematiche:
Esempio
Math.E // returns Euler's number
Math.PI // returns PI
Math.SQRT2 // returns the square root of 2
Math.SQRT1_2 // returns the square root of 1/2
Math.LN2 // returns the natural logarithm of 2
Math.LN10 // returns the natural logarithm of 10
Math.LOG2E // returns base 2 logarithm of E
Math.LOG10E // returns base 10 logarithm of E
Metodi matematici
La sintassi per Math qualsiasi metodo è: Math.method(number)
Numero a Intero
Esistono 4 metodi comuni per arrotondare un numero a un numero intero:
Math.round(x) | Restituisce x arrotondato al suo intero più vicino |
Math.ceil(x) | Restituisce x arrotondato per eccesso al numero intero più vicino |
Math.floor(x) | Restituisce x arrotondato per difetto al numero intero più vicino |
Math.trunc(x) | Restituisce la parte intera di x ( nuovo in ES6 ) |
Math.round()
Math.round(x)
restituisce l'intero più vicino:
Esempi
Math.round(4.6);
Math.round(4.5);
Math.round(4.4);
Math.ceil()
Math.ceil(x)
restituisce il valore di x arrotondato per eccesso al numero intero più vicino:
Esempio
Math.ceil(4.9);
Math.ceil(4.7);
Math.ceil(4.4);
Math.ceil(4.2);
Math.ceil(-4.2);
Math.floor()
Math.floor(x)
restituisce il valore di x arrotondato per difetto al numero intero più vicino:
Esempio
Math.floor(4.9);
Math.floor(4.7);
Math.floor(4.4);
Math.floor(4.2);
Math.floor(-4.2);
Math.trunc()
Math.trunc(x)
restituisce la parte intera di x:
Esempio
Math.trunc(4.9);
Math.trunc(4.7);
Math.trunc(4.4);
Math.trunc(4.2);
Math.trunc(-4.2);
Math.sign()
Math.sign(x)
restituisce se x è negativo, nullo o positivo:
Esempio
Math.sign(-4);
Math.sign(0);
Math.sign(4);
Math.trunc() e Math.sign() sono stati aggiunti a JavaScript 2015 - ES6 .
Math.pow()
Math.pow(x, y)
restituisce il valore di x alla potenza di y:
Esempio
Math.pow(8, 2);
Math.sqrt()
Math.sqrt(x)
restituisce la radice quadrata di x:
Esempio
Math.sqrt(64);
Math.abs()
Math.abs(x)
restituisce il valore assoluto (positivo) di x:
Esempio
Math.abs(-4.7);
Math.sin ()
Math.sin(x)
restituisce il seno (un valore compreso tra -1 e 1) dell'angolo x (espresso in radianti).
Se vuoi usare i gradi invece dei radianti, devi convertire i gradi in radianti:
Angolo in radianti = Angolo in gradi x PI / 180.
Esempio
Math.sin(90 * Math.PI / 180); // returns 1 (the sine of 90 degrees)
Math.cos()
Math.cos(x)
restituisce il coseno (un valore compreso tra -1 e 1) dell'angolo x (espresso in radianti).
Se vuoi usare i gradi invece dei radianti, devi convertire i gradi in radianti:
Angolo in radianti = Angolo in gradi x PI / 180.
Esempio
Math.cos(0 * Math.PI / 180); // returns 1 (the cos of 0 degrees)
Math.min() e Math.max()
Math.min()
e Math.max()
può essere utilizzato per trovare il valore più basso o più alto in un elenco di argomenti:
Esempio
Math.min(0, 150, 30, 20, -8, -200);
Esempio
Math.max(0, 150, 30, 20, -8, -200);
Matematica.casuale()
Math.random()
restituisce un numero casuale compreso tra 0 (incluso) e 1 (escluso):
Esempio
Math.random();
Imparerai di più Math.random()
nel prossimo capitolo di questo tutorial.
Il metodo Math.log()
Math.log(x)
restituisce il logaritmo naturale di x.
Il logaritmo naturale restituisce il tempo necessario per raggiungere un certo livello di crescita:
Esempi
Math.log(1);
Math.log(2);
Math.log(3);
Math.E e Math.log() sono gemelli.
Quante volte dobbiamo moltiplicare Math.E per ottenere 10?
Math.log(10);
Il metodo Math.log2()
Math.log2(x)
restituisce il logaritmo in base 2 di x.
Quante volte dobbiamo moltiplicare 2 per ottenere 8?
Math.log2(8);
Il metodo Math.log10()
Math.log10(x)
restituisce il logaritmo in base 10 di x.
Quante volte dobbiamo moltiplicare 10 per ottenere 1000?
Math.log10(1000);
Metodi matematici JavaScript
Method | Description |
---|---|
abs(x) | Returns the absolute value of x |
acos(x) | Returns the arccosine of x, in radians |
acosh(x) | Returns the hyperbolic arccosine of x |
asin(x) | Returns the arcsine of x, in radians |
asinh(x) | Returns the hyperbolic arcsine of x |
atan(x) | Returns the arctangent of x as a numeric value between -PI/2 and PI/2 radians |
atan2(y, x) | Returns the arctangent of the quotient of its arguments |
atanh(x) | Returns the hyperbolic arctangent of x |
cbrt(x) | Returns the cubic root of x |
ceil(x) | Returns x, rounded upwards to the nearest integer |
cos(x) | Returns the cosine of x (x is in radians) |
cosh(x) | Returns the hyperbolic cosine of x |
exp(x) | Returns the value of Ex |
floor(x) | Returns x, rounded downwards to the nearest integer |
log(x) | Returns the natural logarithm (base E) of x |
max(x, y, z, ..., n) | Returns the number with the highest value |
min(x, y, z, ..., n) | Returns the number with the lowest value |
pow(x, y) | Returns the value of x to the power of y |
random() | Returns a random number between 0 and 1 |
round(x) | Rounds x to the nearest integer |
sign(x) | Returns if x is negative, null or positive (-1, 0, 1) |
sin(x) | Returns the sine of x (x is in radians) |
sinh(x) | Returns the hyperbolic sine of x |
sqrt(x) | Returns the square root of x |
tan(x) | Returns the tangent of an angle |
tanh(x) | Returns the hyperbolic tangent of a number |
trunc(x) | Returns the integer part of a number (x) |
Riferimento matematico completo
Per un riferimento completo, vai al nostro Riferimento completo di oggetti matematici .
Il riferimento contiene descrizioni ed esempi di tutte le proprietà e i metodi di Math.