


Mastering JavaScripts Math Object: A Comprehensive Guide to Built-in Mathematical Functions and Properties
L'objet mathématique JavaScript : un aperçu
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.
1. Constantes
L'objet Math comprend plusieurs constantes utiles pour les calculs mathématiques :
- Math.E : La base des logarithmes naturels, approximativement égale à 2,718.
- Math.LN2 : Le logarithme népérien de 2, approximativement égal à 0,693.
- Math.LN10 : Le logarithme népérien de 10, approximativement égal à 2,303.
- Math.LOG2E : Le logarithme base 2 de E, approximativement égal à 1,442.
- Math.LOG10E : Le logarithme base 10 de E, approximativement égal à 0,434.
- Math.PI : Le rapport de la circonférence d'un cercle à son diamètre, approximativement égal à 3,14159.
- Math.SQRT1_2 : La racine carrée de 1/2, approximativement égale à 0,707.
- Math.SQRT2 : La racine carrée de 2, approximativement égale à 1,414.
2. Méthodes
L'objet Math propose plusieurs méthodes pour effectuer des opérations mathématiques :
- Math.abs(x) : renvoie la valeur absolue de x.
Math.abs(-5); // 5
- Math.ceil(x) : Arrondit x à l'entier le plus proche.
Math.ceil(4.2); // 5
- Math.floor(x) : Arrondit x à l'entier le plus proche.
Math.floor(4.7); // 4
- Math.round(x) : Arrondit x à l'entier le plus proche.
Math.round(4.5); // 5
- Math.max(...values) : Renvoie le plus grand de zéro ou plusieurs nombres.
Math.max(1, 5, 3); // 5
- Math.min(...values) : Renvoie le plus petit de zéro ou plusieurs nombres.
Math.min(1, 5, 3); // 1
- Math.random() : renvoie un nombre pseudo-aléatoire compris entre 0 (inclus) et 1 (exclusif).
Math.random(); // e.g., 0.237
- Math.pow(base, exponent) : Renvoie la base élevée à la puissance exposant.
Math.pow(2, 3); // 8
- Math.sqrt(x) : Renvoie la racine carrée de x.
Math.sqrt(9); // 3
- Math.trunc(x) : renvoie la partie entière de x, en supprimant tous les chiffres fractionnaires.
Math.trunc(4.9); // 4
3. Exemples d'utilisation
Voici quelques exemples pratiques de la façon dont vous pouvez utiliser l'objet Math :
- Générer un entier aléatoire
function getRandomInt(min, max) { return Math.floor(Math.random() * (max - min + 1)) + min; } console.log(getRandomInt(1, 10)); // e.g., 7
- Calcul de l'hypoténuse
function calculateHypotenuse(a, b) { return Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2)); } console.log(calculateHypotenuse(3, 4)); // 5
4. Limites et remarques
- Problèmes de précision : l'arithmétique à virgule flottante peut entraîner des problèmes de précision. Par exemple, Math.sqrt(2) * Math.sqrt(2) peut ne pas être exactement égal à 2 en raison d'erreurs d'arrondi.
- Pas un constructeur : L'objet Math n'a pas de capacités de constructeur. Toutes les propriétés et méthodes sont statiques.
Méthodes et propriétés des objets mathématiques
1. Math.abs(x)
Renvoie la valeur absolue de x.
console.log(Math.abs(-10)); // 10 console.log(Math.abs(5.5)); // 5.5
2. Math.acos(x)
Renvoie l'arccosinus (cosinus inverse) de x, en radians.
console.log(Math.acos(1)); // 0 console.log(Math.acos(0)); // 1.5707963267948966 (π/2)
3. Math.acosh(x)
Renvoie l'arccosinus hyperbolique de x.
console.log(Math.acosh(1)); // 0 console.log(Math.acosh(2)); // 1.3169578969248166
4. Math.asin(x)
Renvoie l'arc sinus (sinus inverse) de x, en radians.
console.log(Math.asin(0)); // 0 console.log(Math.asin(1)); // 1.5707963267948966 (π/2)
5. Math.asinh(x)
Renvoie l'arc sinus hyperbolique de x.
console.log(Math.asinh(0)); // 0 console.log(Math.asinh(1)); // 0.881373587019543
6. Math.atan(x)
Renvoie l'arctangente (tangente inverse) de x, en radians.
console.log(Math.atan(1)); // 0.7853981633974483 (π/4) console.log(Math.atan(0)); // 0
7. Math.atan2(y, x)
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)
8. Math.atanh(x)
Renvoie l'arctangente hyperbolique de x.
console.log(Math.atanh(0)); // 0 console.log(Math.atanh(0.5)); // 0.5493061443340549
9. Math.cbrt(x)
Renvoie la racine cubique de x.
console.log(Math.cbrt(27)); // 3 console.log(Math.cbrt(-8)); // -2
10. Math.ceil(x)
Arrondit x vers le haut à l'entier le plus proche.
console.log(Math.ceil(4.2)); // 5 console.log(Math.ceil(-4.7)); // -4
11. Math.clz32(x)
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
12. Math.cos(x)
Renvoie le cosinus de x (où x est en radians).
console.log(Math.cos(0)); // 1 console.log(Math.cos(Math.PI)); // -1
13. Math.cosh(x)
Returns the hyperbolic cosine of x.
console.log(Math.cosh(0)); // 1 console.log(Math.cosh(1)); // 1.5430806348152437
14. Math.E
Returns Euler's number, approximately 2.718.
console.log(Math.E); // 2.718281828459045
15. Math.exp(x)
Returns the value of e raised to the power of x.
console.log(Math.exp(1)); // 2.718281828459045 console.log(Math.exp(0)); // 1
16. Math.expm1(x)
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
17. Math.floor(x)
Rounds x downwards to the nearest integer.
console.log(Math.floor(4.7)); // 4 console.log(Math.floor(-4.2)); // -5
18. Math.fround(x)
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
19. Math.LN2
Returns the natural logarithm of 2, approximately 0.693.
console.log(Math.LN2); // 0.6931471805599453
20. Math.LN10
Returns the natural logarithm of 10, approximately 2.302.
console.log(Math.LN10); // 2.302585092994046
21. Math.log(x)
Returns the natural logarithm (base e) of x.
console.log(Math.log(Math.E)); // 1 console.log(Math.log(10)); // 2.302585092994046
22. Math.log10(x)
Returns the base-10 logarithm of x.
console.log(Math.log10(10)); // 1 console.log(Math.log10(100)); // 2
23. Math.LOG10E
Returns the base-10 logarithm of e, approximately 0.434.
console.log(Math.LOG10E); // 0.4342944819032518
24. Math.log1p(x)
Returns the natural logarithm of 1 + x.
console.log(Math.log1p(1)); // 0.6931471805599453 console.log(Math.log1p(0)); // 0
25. Math.log2(x)
Returns the base-2 logarithm of x.
console.log(Math.log2(2)); // 1 console.log(Math.log2(8)); // 3
26. Math.LOG2E
Returns the base-2 logarithm of e, approximately 1.442.
console.log(Math.LOG2E); // 1.4426950408889634
27. Math.max(...values)
Returns the largest of zero or more numbers.
console.log(Math.max(1, 5, 3)); // 5 console.log(Math.max(-1, -5, -3)); // -1
28. Math.min(...values)
Returns the smallest of zero or more numbers.
console.log(Math.min(1, 5, 3)); // 1 console.log(Math.min(-1, -5, -3)); // -5
29. Math.PI
Returns the value of π, approximately 3.14159.
console.log(Math.PI); // 3.141592653589793
30. Math.pow(base, exponent)
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
31. Math.random()
Returns a pseudo-random number between 0 (inclusive) and 1 (exclusive).
console.log(Math.random()); // e.g., 0.237
32. Math.round(x)
Rounds x to the nearest integer.
console.log(Math.round(4.5)); // 5 console.log(Math.round(4.4)); // 4
33. Math.sign(x)
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
34. Math.sin(x)
Returns the sine of x (where x is in radians).
console.log(Math.sin(0)); // 0 console.log(Math.sin(Math.PI / 2)); // 1
35. Math.sinh(x)
Returns the hyperbolic sine of x.
console.log(Math.sinh(0)); // 0 console.log(Math.sinh(1)); // 1.1752011936438014
36. Math.sqrt(x)
Returns the square root of x.
console.log(Math.sqrt(9)); // 3 console.log(Math.sqrt(16)); // 4
37. Math.SQRT1_2
Returns the square root of 1/2, approximately 0.707.
console.log(Math.SQRT1_2); // 0.7071067811865476
38. Math.SQRT2
Returns the square root of 2, approximately 1.414.
console.log(Math.SQRT2); // 1.4142135623730951
39. Math.tan(x)
Returns the tangent of x (where x is in radians).
console.log(Math.tan(0)); // 0 console.log(Math.tan(Math.PI / 4)); // 1
40. Math.tanh(x)
Returns the hyperbolic tangent of x.
console.log(Math.tanh(0)); // 0 console.log(Math.tanh(1)); // 0.7615941559557649
41. Math.trunc(x)
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Detailed explanation of JavaScript string replacement method and FAQ This article will explore two ways to replace string characters in JavaScript: internal JavaScript code and internal HTML for web pages. Replace string inside JavaScript code The most direct way is to use the replace() method: str = str.replace("find","replace"); This method replaces only the first match. To replace all matches, use a regular expression and add the global flag g: str = str.replace(/fi

Article discusses creating, publishing, and maintaining JavaScript libraries, focusing on planning, development, testing, documentation, and promotion strategies.

The article discusses strategies for optimizing JavaScript performance in browsers, focusing on reducing execution time and minimizing impact on page load speed.

The article discusses effective JavaScript debugging using browser developer tools, focusing on setting breakpoints, using the console, and analyzing performance.

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

This article outlines ten simple steps to significantly boost your script's performance. These techniques are straightforward and applicable to all skill levels. Stay Updated: Utilize a package manager like NPM with a bundler such as Vite to ensure

Sequelize is a promise-based Node.js ORM. It can be used with PostgreSQL, MySQL, MariaDB, SQLite, and MSSQL. In this tutorial, we will be implementing authentication for users of a web app. And we will use Passport, the popular authentication middlew

This article will guide you to create a simple picture carousel using the jQuery library. We will use the bxSlider library, which is built on jQuery and provides many configuration options to set up the carousel. Nowadays, picture carousel has become a must-have feature on the website - one picture is better than a thousand words! After deciding to use the picture carousel, the next question is how to create it. First, you need to collect high-quality, high-resolution pictures. Next, you need to create a picture carousel using HTML and some JavaScript code. There are many libraries on the web that can help you create carousels in different ways. We will use the open source bxSlider library. The bxSlider library supports responsive design, so the carousel built with this library can be adapted to any
