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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

Replace String Characters in JavaScript Replace String Characters in JavaScript Mar 11, 2025 am 12:07 AM

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

How do I create and publish my own JavaScript libraries? How do I create and publish my own JavaScript libraries? Mar 18, 2025 pm 03:12 PM

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

How do I optimize JavaScript code for performance in the browser? How do I optimize JavaScript code for performance in the browser? Mar 18, 2025 pm 03:14 PM

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

How do I debug JavaScript code effectively using browser developer tools? How do I debug JavaScript code effectively using browser developer tools? Mar 18, 2025 pm 03:16 PM

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

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

10 Ways to Instantly Increase Your jQuery Performance 10 Ways to Instantly Increase Your jQuery Performance Mar 11, 2025 am 12:15 AM

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

Using Passport With Sequelize and MySQL Using Passport With Sequelize and MySQL Mar 11, 2025 am 11:04 AM

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

How to Build a Simple jQuery Slider How to Build a Simple jQuery Slider Mar 11, 2025 am 12:19 AM

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

See all articles