Home Web Front-end JS Tutorial Mastering JavaScript&#s Math Object: A Comprehensive Guide to Built-in Mathematical Functions and Properties

Mastering JavaScript&#s Math Object: A Comprehensive Guide to Built-in Mathematical Functions and Properties

Sep 08, 2024 pm 08:34 PM

Mastering JavaScript

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
Copy after login
  • Math.ceil(x) : Arrondit x à l'entier le plus proche.
  Math.ceil(4.2); // 5
Copy after login
  • Math.floor(x) : Arrondit x à l'entier le plus proche.
  Math.floor(4.7); // 4
Copy after login
  • Math.round(x) : Arrondit x à l'entier le plus proche.
  Math.round(4.5); // 5
Copy after login
  • Math.max(...values) : Renvoie le plus grand de zéro ou plusieurs nombres.
  Math.max(1, 5, 3); // 5
Copy after login
  • Math.min(...values) : Renvoie le plus petit de zéro ou plusieurs nombres.
  Math.min(1, 5, 3); // 1
Copy after login
  • Math.random() : renvoie un nombre pseudo-aléatoire compris entre 0 (inclus) et 1 (exclusif).
  Math.random(); // e.g., 0.237
Copy after login
  • Math.pow(base, exponent) : Renvoie la base élevée à la puissance exposant.
  Math.pow(2, 3); // 8
Copy after login
  • Math.sqrt(x) : Renvoie la racine carrée de x.
  Math.sqrt(9); // 3
Copy after login
  • Math.trunc(x) : renvoie la partie entière de x, en supprimant tous les chiffres fractionnaires.
  Math.trunc(4.9); // 4
Copy after login

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
Copy after login
  • 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
Copy after login

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
Copy after login

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)
Copy after login

3. Math.acosh(x)

Renvoie l'arccosinus hyperbolique de x.

console.log(Math.acosh(1)); // 0
console.log(Math.acosh(2)); // 1.3169578969248166
Copy after login

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)
Copy after login

5. Math.asinh(x)

Renvoie l'arc sinus hyperbolique de x.

console.log(Math.asinh(0)); // 0
console.log(Math.asinh(1)); // 0.881373587019543
Copy after login

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
Copy after login

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)
Copy after login

8. Math.atanh(x)

Renvoie l'arctangente hyperbolique de x.

console.log(Math.atanh(0)); // 0
console.log(Math.atanh(0.5)); // 0.5493061443340549
Copy after login

9. Math.cbrt(x)

Renvoie la racine cubique de x.

console.log(Math.cbrt(27)); // 3
console.log(Math.cbrt(-8)); // -2
Copy after login

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
Copy after login

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
Copy after login

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
Copy after login

13. Math.cosh(x)

Returns the hyperbolic cosine of x.

console.log(Math.cosh(0)); // 1
console.log(Math.cosh(1)); // 1.5430806348152437
Copy after login

14. Math.E

Returns Euler's number, approximately 2.718.

console.log(Math.E); // 2.718281828459045
Copy after login

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
Copy after login

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
Copy after login

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
Copy after login

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
Copy after login

19. Math.LN2

Returns the natural logarithm of 2, approximately 0.693.

console.log(Math.LN2); // 0.6931471805599453
Copy after login

20. Math.LN10

Returns the natural logarithm of 10, approximately 2.302.

console.log(Math.LN10); // 2.302585092994046
Copy after login

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
Copy after login

22. Math.log10(x)

Returns the base-10 logarithm of x.

console.log(Math.log10(10)); // 1
console.log(Math.log10(100)); // 2
Copy after login

23. Math.LOG10E

Returns the base-10 logarithm of e, approximately 0.434.

console.log(Math.LOG10E); // 0.4342944819032518
Copy after login

24. Math.log1p(x)

Returns the natural logarithm of 1 + x.

console.log(Math.log1p(1)); // 0.6931471805599453
console.log(Math.log1p(0)); // 0
Copy after login

25. Math.log2(x)

Returns the base-2 logarithm of x.

console.log(Math.log2(2)); // 1
console.log(Math.log2(8)); // 3
Copy after login

26. Math.LOG2E

Returns the base-2 logarithm of e, approximately 1.442.

console.log(Math.LOG2E); // 1.4426950408889634
Copy after login

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
Copy after login

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
Copy after login

29. Math.PI

Returns the value of π, approximately 3.14159.

console.log(Math.PI); // 3.141592653589793
Copy after login

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
Copy after login

31. Math.random()

Returns a pseudo-random number between 0 (inclusive) and 1 (exclusive).

console.log(Math.random()); // e.g., 0.237
Copy after login

32. Math.round(x)

Rounds x to the nearest integer.

console.log(Math.round(4.5)); // 5
console.log(Math.round(4.4)); // 4
Copy after login

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
Copy after login

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
Copy after login

35. Math.sinh(x)

Returns the hyperbolic sine of x.

console.log(Math.sinh(0)); // 0
console.log(Math.sinh(1)); // 1.1752011936438014
Copy after login

36. Math.sqrt(x)

Returns the square root of x.

console.log(Math.sqrt(9)); // 3
console.log(Math.sqrt(16));

 // 4
Copy after login

37. Math.SQRT1_2

Returns the square root of 1/2, approximately 0.707.

console.log(Math.SQRT1_2); // 0.7071067811865476
Copy after login

38. Math.SQRT2

Returns the square root of 2, approximately 1.414.

console.log(Math.SQRT2); // 1.4142135623730951
Copy after login

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
Copy after login

40. Math.tanh(x)

Returns the hyperbolic tangent of x.

console.log(Math.tanh(0)); // 0
console.log(Math.tanh(1)); // 0.7615941559557649
Copy after login

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
Copy after login

The above is the detailed content of Mastering JavaScript&#s 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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What should I do if I encounter garbled code printing for front-end thermal paper receipts? What should I do if I encounter garbled code printing for front-end thermal paper receipts? Apr 04, 2025 pm 02:42 PM

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...

Demystifying JavaScript: What It Does and Why It Matters Demystifying JavaScript: What It Does and Why It Matters Apr 09, 2025 am 12:07 AM

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

Who gets paid more Python or JavaScript? Who gets paid more Python or JavaScript? Apr 04, 2025 am 12:09 AM

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

How to achieve parallax scrolling and element animation effects, like Shiseido's official website?
or:
How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? How to achieve parallax scrolling and element animation effects, like Shiseido's official website? or: How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? Apr 04, 2025 pm 05:36 PM

Discussion on the realization of parallax scrolling and element animation effects in this article will explore how to achieve similar to Shiseido official website (https://www.shiseido.co.jp/sb/wonderland/)...

Is JavaScript hard to learn? Is JavaScript hard to learn? Apr 03, 2025 am 12:20 AM

Learning JavaScript is not difficult, but it is challenging. 1) Understand basic concepts such as variables, data types, functions, etc. 2) Master asynchronous programming and implement it through event loops. 3) Use DOM operations and Promise to handle asynchronous requests. 4) Avoid common mistakes and use debugging techniques. 5) Optimize performance and follow best practices.

The Evolution of JavaScript: Current Trends and Future Prospects The Evolution of JavaScript: Current Trends and Future Prospects Apr 10, 2025 am 09:33 AM

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

How to merge array elements with the same ID into one object using JavaScript? How to merge array elements with the same ID into one object using JavaScript? Apr 04, 2025 pm 05:09 PM

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

How to implement panel drag and drop adjustment function similar to VSCode in front-end development? How to implement panel drag and drop adjustment function similar to VSCode in front-end development? Apr 04, 2025 pm 02:06 PM

Explore the implementation of panel drag and drop adjustment function similar to VSCode in the front-end. In front-end development, how to implement VSCode similar to VSCode...

See all articles