L'objet JavaScript Math est un objet intégré qui fournit une collection de fonctions et de constantes mathématiques. Ce n'est pas un constructeur, vous ne pouvez donc pas en créer des instances ; au lieu de cela, il est utilisé directement via ses méthodes et propriétés statiques.
L'objet Math comprend plusieurs constantes utiles pour les calculs mathématiques :
L'objet Math propose plusieurs méthodes pour effectuer des opérations mathématiques :
Math.abs(-5); // 5
Math.ceil(4.2); // 5
Math.floor(4.7); // 4
Math.round(4.5); // 5
Math.max(1, 5, 3); // 5
Math.min(1, 5, 3); // 1
Math.random(); // e.g., 0.237
Math.pow(2, 3); // 8
Math.sqrt(9); // 3
Math.trunc(4.9); // 4
Voici quelques exemples pratiques de la façon dont vous pouvez utiliser l'objet Math :
function getRandomInt(min, max) { return Math.floor(Math.random() * (max - min + 1)) + min; } console.log(getRandomInt(1, 10)); // e.g., 7
function calculateHypotenuse(a, b) { return Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2)); } console.log(calculateHypotenuse(3, 4)); // 5
Renvoie la valeur absolue de x.
console.log(Math.abs(-10)); // 10 console.log(Math.abs(5.5)); // 5.5
Renvoie l'arccosinus (cosinus inverse) de x, en radians.
console.log(Math.acos(1)); // 0 console.log(Math.acos(0)); // 1.5707963267948966 (π/2)
Renvoie l'arccosinus hyperbolique de x.
console.log(Math.acosh(1)); // 0 console.log(Math.acosh(2)); // 1.3169578969248166
Renvoie l'arc sinus (sinus inverse) de x, en radians.
console.log(Math.asin(0)); // 0 console.log(Math.asin(1)); // 1.5707963267948966 (π/2)
Renvoie l'arc sinus hyperbolique de x.
console.log(Math.asinh(0)); // 0 console.log(Math.asinh(1)); // 0.881373587019543
Renvoie l'arctangente (tangente inverse) de x, en radians.
console.log(Math.atan(1)); // 0.7853981633974483 (π/4) console.log(Math.atan(0)); // 0
Renvoie l'arctangente du quotient de ses arguments, en radians.
console.log(Math.atan2(1, 1)); // 0.7853981633974483 (π/4) console.log(Math.atan2(-1, -1)); // -2.356194490192345 (-3π/4)
Renvoie l'arctangente hyperbolique de x.
console.log(Math.atanh(0)); // 0 console.log(Math.atanh(0.5)); // 0.5493061443340549
Renvoie la racine cubique de x.
console.log(Math.cbrt(27)); // 3 console.log(Math.cbrt(-8)); // -2
Arrondit x vers le haut à l'entier le plus proche.
console.log(Math.ceil(4.2)); // 5 console.log(Math.ceil(-4.7)); // -4
Renvoie le nombre de zéros non significatifs dans la représentation binaire 32 bits de x.
console.log(Math.clz32(1)); // 31 console.log(Math.clz32(0x80000000)); // 0
Renvoie le cosinus de x (où x est en radians).
console.log(Math.cos(0)); // 1 console.log(Math.cos(Math.PI)); // -1
Returns the hyperbolic cosine of x.
console.log(Math.cosh(0)); // 1 console.log(Math.cosh(1)); // 1.5430806348152437
Returns Euler's number, approximately 2.718.
console.log(Math.E); // 2.718281828459045
Returns the value of e raised to the power of x.
console.log(Math.exp(1)); // 2.718281828459045 console.log(Math.exp(0)); // 1
Returns the value of e raised to the power of x, minus 1.
console.log(Math.expm1(1)); // 1.718281828459045 console.log(Math.expm1(0)); // 0
Rounds x downwards to the nearest integer.
console.log(Math.floor(4.7)); // 4 console.log(Math.floor(-4.2)); // -5
Returns the nearest (32-bit single precision) float representation of x.
console.log(Math.fround(1.337)); // 1.336914 console.log(Math.fround(1.5)); // 1.5
Returns the natural logarithm of 2, approximately 0.693.
console.log(Math.LN2); // 0.6931471805599453
Returns the natural logarithm of 10, approximately 2.302.
console.log(Math.LN10); // 2.302585092994046
Returns the natural logarithm (base e) of x.
console.log(Math.log(Math.E)); // 1 console.log(Math.log(10)); // 2.302585092994046
Returns the base-10 logarithm of x.
console.log(Math.log10(10)); // 1 console.log(Math.log10(100)); // 2
Returns the base-10 logarithm of e, approximately 0.434.
console.log(Math.LOG10E); // 0.4342944819032518
Returns the natural logarithm of 1 + x.
console.log(Math.log1p(1)); // 0.6931471805599453 console.log(Math.log1p(0)); // 0
Returns the base-2 logarithm of x.
console.log(Math.log2(2)); // 1 console.log(Math.log2(8)); // 3
Returns the base-2 logarithm of e, approximately 1.442.
console.log(Math.LOG2E); // 1.4426950408889634
Returns the largest of zero or more numbers.
console.log(Math.max(1, 5, 3)); // 5 console.log(Math.max(-1, -5, -3)); // -1
Returns the smallest of zero or more numbers.
console.log(Math.min(1, 5, 3)); // 1 console.log(Math.min(-1, -5, -3)); // -5
Returns the value of π, approximately 3.14159.
console.log(Math.PI); // 3.141592653589793
Returns the value of base raised to the power of exponent.
console.log(Math.pow(2, 3)); // 8 console.log(Math.pow(5, 0)); // 1
Returns a pseudo-random number between 0 (inclusive) and 1 (exclusive).
console.log(Math.random()); // e.g., 0.237
Rounds x to the nearest integer.
console.log(Math.round(4.5)); // 5 console.log(Math.round(4.4)); // 4
Returns the sign of a number, indicating whether the number is positive, negative, or zero.
console.log(Math.sign(-5)); // -1 console.log(Math.sign(0)); // 0 console.log(Math.sign(5)); // 1
Returns the sine of x (where x is in radians).
console.log(Math.sin(0)); // 0 console.log(Math.sin(Math.PI / 2)); // 1
Returns the hyperbolic sine of x.
console.log(Math.sinh(0)); // 0 console.log(Math.sinh(1)); // 1.1752011936438014
Returns the square root of x.
console.log(Math.sqrt(9)); // 3 console.log(Math.sqrt(16)); // 4
Returns the square root of 1/2, approximately 0.707.
console.log(Math.SQRT1_2); // 0.7071067811865476
Returns the square root of 2, approximately 1.414.
console.log(Math.SQRT2); // 1.4142135623730951
Returns the tangent of x (where x is in radians).
console.log(Math.tan(0)); // 0 console.log(Math.tan(Math.PI / 4)); // 1
Returns the hyperbolic tangent of x.
console.log(Math.tanh(0)); // 0 console.log(Math.tanh(1)); // 0.7615941559557649
Returns the integer part of a number by removing any fractional digits.
console.log(Math.trunc(4.9)); // 4 console.log(Math.trunc(-4.9)); // -4
The above is the detailed content of Mastering JavaScripts Math Object: A Comprehensive Guide to Built-in Mathematical Functions and Properties. For more information, please follow other related articles on the PHP Chinese website!