Objet mathématique JavaScript
L'objet JavaScript Math vous permet d'effectuer des tâches mathématiques sur des nombres.
Exemple
Math.PI;
L'objet mathématique
Contrairement aux autres objets, l'objet Math n'a pas de constructeur.
L'objet Math est statique.
Toutes les méthodes et propriétés peuvent être utilisées sans créer d'abord un objet Math.
Propriétés mathématiques (constantes)
La syntaxe de toute propriété Math est : .Math.property
JavaScript fournit 8 constantes mathématiques accessibles en tant que propriétés Math :
Exemple
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
Méthodes mathématiques
La syntaxe des méthodes Math any est la suivante : Math.method(number)
Nombre en entier
Il existe 4 méthodes courantes pour arrondir un nombre à un nombre entier :
Math.rond(x) | Renvoie x arrondi à son entier le plus proche |
Math.ceil(x) | Renvoie x arrondi à son entier le plus proche |
Math.floor(x) | Renvoie x arrondi à son entier le plus proche |
Math.trunc(x) | Renvoie la partie entière de x ( nouveau dans ES6 ) |
Math.rond()
Math.round(x)
renvoie l'entier le plus proche :
Exemples
Math.round(4.6);
Math.round(4.5);
Math.round(4.4);
Math. plafond()
Math.ceil(x)
renvoie la valeur de x arrondie à son entier le plus proche :
Exemple
Math.ceil(4.9);
Math.ceil(4.7);
Math.ceil(4.4);
Math.ceil(4.2);
Math.ceil(-4.2);
Math.étage()
Math.floor(x)
renvoie la valeur de x arrondie à l'entier inférieur le plus proche :
Exemple
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)
renvoie la partie entière de x :
Exemple
Math.trunc(4.9);
Math.trunc(4.7);
Math.trunc(4.4);
Math.trunc(4.2);
Math.trunc(-4.2);
Math.signe()
Math.sign(x)
renvoie si x est négatif, nul ou positif :
Exemple
Math.sign(-4);
Math.sign(0);
Math.sign(4);
Math.trunc() et Math.sign() ont été ajoutés à JavaScript 2015 - ES6 .
Math.pow()
Math.pow(x, y)
renvoie la valeur de x à la puissance y :
Exemple
Math.pow(8, 2);
Math.sqrt()
Math.sqrt(x)
renvoie la racine carrée de x :
Exemple
Math.sqrt(64);
Math.abs()
Math.abs(x)
renvoie la valeur absolue (positive) de x :
Exemple
Math.abs(-4.7);
Math.sin ()
Math.sin(x)
renvoie le sinus (une valeur comprise entre -1 et 1) de l'angle x (exprimé en radians).
Si vous souhaitez utiliser des degrés au lieu de radians, vous devez convertir les degrés en radians :
Angle en radians = Angle en degrés x PI / 180.
Exemple
Math.sin(90 * Math.PI / 180); // returns 1 (the sine of 90 degrees)
Math.cos()
Math.cos(x)
renvoie le cosinus (une valeur comprise entre -1 et 1) de l'angle x (exprimé en radians).
Si vous souhaitez utiliser des degrés au lieu de radians, vous devez convertir les degrés en radians :
Angle en radians = Angle en degrés x PI / 180.
Exemple
Math.cos(0 * Math.PI / 180); // returns 1 (the cos of 0 degrees)
Math.min() et Math.max()
Math.min()
et Math.max()
peut être utilisé pour trouver la valeur la plus basse ou la plus haute dans une liste d'arguments :
Exemple
Math.min(0, 150, 30, 20, -8, -200);
Exemple
Math.max(0, 150, 30, 20, -8, -200);
Math.aléatoire()
Math.random()
renvoie un nombre aléatoire entre 0 (inclus) et 1 (exclusif) :
Exemple
Math.random();
Vous en apprendrez plus Math.random()
dans le prochain chapitre de ce tutoriel.
La méthode Math.log()
Math.log(x)
renvoie le logarithme népérien de x.
Le logarithme népérien renvoie le temps nécessaire pour atteindre un certain niveau de croissance :
Exemples
Math.log(1);
Math.log(2);
Math.log(3);
Math.E et Math.log() sont jumeaux.
Combien de fois devons-nous multiplier Math.E pour obtenir 10 ?
Math.log(10);
La méthode Math.log2()
Math.log2(x)
renvoie le logarithme en base 2 de x.
Combien de fois faut-il multiplier 2 pour obtenir 8 ?
Math.log2(8);
La méthode Math.log10()
Math.log10(x)
renvoie le logarithme en base 10 de x.
Combien de fois doit-on multiplier 10 pour obtenir 1000 ?
Math.log10(1000);
Méthodes mathématiques 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) |
Référence mathématique complète
Pour une référence complète, consultez notre référence complète d'objets mathématiques .
La référence contient des descriptions et des exemples de toutes les propriétés et méthodes Math.