📜  JavaScript |数学对象(1)

📅  最后修改于: 2023-12-03 15:16:08.572000             🧑  作者: Mango

JavaScript: Math Object

The Math object in JavaScript provides a collection of mathematical constants and functions. It is not a constructor, which means you cannot create a new instance of the Math object. All the properties and methods of the Math object are static, which means you can access them using the Math object without creating an instance.

Mathematical Constants

The Math object provides several mathematical constants, including:

  • Math.PI: the ratio of the circumference of a circle to its diameter, approximately equal to 3.14159.
  • Math.E: Euler's constant, approximately equal to 2.71828.
  • Math.LOG10E: the base-10 logarithm of the constant Math.E, approximately equal to 0.43429.
  • Math.LOG2E: the base-2 logarithm of the constant Math.E, approximately equal to 1.44269.
  • Math.SQRT1_2: the square root of 0.5, approximately equal to 0.70711.
  • Math.SQRT2: the square root of 2, approximately equal to 1.41421.

Example code snippet:

console.log(Math.PI); // Output: 3.141592653589793
console.log(Math.E); // Output: 2.718281828459045
Mathematical Functions

The Math object provides several mathematical functions, including:

  • Math.abs(): returns the absolute value of a number.
  • Math.ceil(): rounds a number up to the nearest integer.
  • Math.floor(): rounds a number down to the nearest integer.
  • Math.round(): rounds a number to the nearest integer.
  • Math.max(): returns the maximum value of a set of numbers.
  • Math.min(): returns the minimum value of a set of numbers.
  • Math.pow(): returns the base to the exponent power.
  • Math.sqrt(): returns the square root of a number.

Example code snippet:

console.log(Math.abs(-5)); // Output: 5
console.log(Math.ceil(4.3)); // Output: 5
console.log(Math.floor(4.9)); // Output: 4
console.log(Math.round(4.5)); // Output: 5
console.log(Math.max(2, 4, 6, 8)); // Output: 8
console.log(Math.min(2, 4, 6, 8)); // Output: 2
console.log(Math.pow(2, 3)); // Output: 8
console.log(Math.sqrt(4)); // Output: 2
Trigonometric Functions

The Math object also provides several trigonometric functions, including:

  • Math.sin(): returns the sine of a number.
  • Math.cos(): returns the cosine of a number.
  • Math.tan(): returns the tangent of a number.
  • Math.asin(): returns the arcsine of a number.
  • Math.acos(): returns the arccosine of a number.
  • Math.atan(): returns the arctangent of a number.
  • Math.atan2(): returns the angle between the positive x-axis and the point (x,y).

Example code snippet:

console.log(Math.sin(Math.PI / 2)); // Output: 1
console.log(Math.cos(Math.PI)); // Output: -1
console.log(Math.tan(Math.PI / 4)); // Output: 1
console.log(Math.asin(1)); // Output: 1.5707963267948966
console.log(Math.acos(0)); // Output: 1.5707963267948966
console.log(Math.atan(1)); // Output: 0.7853981633974483
console.log(Math.atan2(1, 1)); // Output: 0.7853981633974483
Random Numbers

The Math object also provides a random() method that returns a pseudorandom number between 0 and 1.

Example code snippet:

console.log(Math.random()); // Output: a pseudorandom number between 0 and 1
Conclusion

In summary, the Math object in JavaScript provides a rich collection of mathematical constants and functions that can help you perform complex numerical computations in your applications. Whether you need to perform simple arithmetic operations or advanced mathematical computations, the Math object has you covered.