JavaScript MathObject
Math Object
The Math object is used to perform mathematical tasks.
The Math object is not a class of objects like Date and String, so it does not have a constructor Math().
Syntax
var x = Math.PI; // 返回 PI var y = Math.sqrt(16); // 返回 16 的平方根
For the Math object tutorial, please refer to this site'sJavaScript Math Object Tutorial。
Math Object Properties
| Property | Description |
|---|---|
| E | Returns the arithmetic constant e, i.e., the base of natural logarithms (approximately 2.718). |
| LN2 | Returns the natural logarithm of 2 (approximately 0.693). |
| LN10 | Returns the natural logarithm of 10 (approximately 2.302). |
| LOG2E | Returns the logarithm of e with base 2 (approximately 1.4426950408889634). |
| LOG10E | Returns the logarithm of e with base 10 (approximately 0.434). |
| PI | Returns pi (approximately 3.14159). |
| SQRT1_2 | Returns the reciprocal of the square root of 2 (approximately 0.707). |
| SQRT2 | Returns the square root of 2 (approximately 1.414). |
Math Object Methods
| Method | Description |
|---|---|
| abs(x) | Returns the absolute value of x. |
| acos(x) | Returns the arccosine of x. |
| asin(x) | Returns the arcsine of x. |
| atan(x) | Returns the arctangent of x as a numeric value between -PI/2 and PI/2 radians. |
| atan2(y,x) | Returns the angle from the x-axis to the point (x, y) (between -PI/2 and PI/2 radians). |
| ceil(x) | Rounds a number up. |
| cos(x) | Returns the cosine of a number. |
| exp(x) | Returns Exraised to the power of x. |
| floor(x) | Rounds x down. |
| log(x) | Returns the natural logarithm of a number (base e). |
| max(x,y,z,...,n) | Returns the highest value among x, y, z, ..., n. |
| min(x,y,z,...,n) | Returns the lowest value among x, y, z, ..., n. |
| pow(x,y) | Returns x raised to the power of y. |
| random() | Returns a random number between 0 and 1. |
| round(x) | Rounds to the nearest integer. |
| sin(x) | Returns the sine of a number. |
| sqrt(x) | Returns the square root of a number. |
| tan(x) | Returns the tangent of an angle. |
| tanh(x) | Returns the hyperbolic tangent of a number. |
| trunc(x) | Removes the decimal part of a number, keeping only the integer part. |