HTML Dom tutorial 48-javascript math object
Math objects are not classes of objects like date and string, so there is no constructor math (). functions like math. Sin () are just functions, not methods of an object.
1: Math object Method
Method |
Description |
FF |
N |
IE |
ABS (X) |
Absolute Value of the returned number |
1 |
2 |
3 |
ACOs (X) |
Returns the arc cosine of a number. |
1 |
2 |
3 |
Asin (X) |
Returns the arc sine of the number. |
1 |
2 |
3 |
Atan (X) |
Returns the arc tangent of X using a value between-PI/2 and PI/2 radian. |
1 |
2 |
3 |
Atan2 (Y, X) |
Returns the angle from the X-axis to the point (x, y) (between-PI/2 and PI/2 radian) |
1 |
2 |
3 |
Ceil (X) |
Rounds up a number. |
1 |
2 |
3 |
Cos (X) |
Returns the cosine of the number. |
1 |
2 |
3 |
Exp (X) |
Returns the index of E. |
1 |
2 |
3 |
Floor (X) |
Deprecated a number. |
1 |
2 |
3 |
Log (X) |
Returns the natural logarithm of the number (Base E) |
1 |
2 |
3 |
Max (x, y) |
Returns the highest value in X and Y. |
1 |
2 |
3 |
Min (x, y) |
Returns the lowest value in X and Y. |
1 |
2 |
3 |
Pow (x, y) |
Returns the Power Y of X. |
1 |
2 |
3 |
Random () |
Returns 0 ~ Random number between 1 |
1 |
2 |
3 |
Round (X) |
Returns a number rounded to the nearest integer. |
1 |
2 |
3 |
Sin (X) |
Returns the sine of the number. |
1 |
2 |
3 |
SQRT (X) |
Returns the square root of a number. |
1 |
2 |
3 |
Tan (X) |
Returns the tangent of an angle. |
1 |
2 |
3 |
Tosource () |
Representing the objectSource code |
1 |
4 |
- |
Valueof () |
Returns the original value of a math object. |
1 |
2 |
4 |
2: attributes of the math object
Attribute |
Description |
FF |
N |
IE |
Constructor |
Reference to the function that creates this object |
1 |
2 |
4 |
E |
Constant E, the base number of the natural logarithm (approximately 2.718) |
1 |
2 |
3 |
Ln2 |
Returns the natural logarithm of 2 (approximately 0.693) |
1 |
2 |
3 |
Ln10 |
Returns the natural logarithm of 10 (approximately 2.302) |
1 |
2 |
3 |
Log2e |
Returns the base-2 logarithm of E (approximately 1.414) |
1 |
2 |
3 |
Log10e |
Returns the base-10 logarithm of E (approximately 0.434) |
1 |
2 |
3 |
Pi |
Returns the circumference rate (approximately 3.14159) |
1 |
2 |
3 |
Prototype |
Allows you to add attributes and methods to an object |
1 |
2 |
4 |
Sqrt1_2 |
Returns the square root of 2 except 1 (approximately 0.707) |
1 |
2 |
3 |
Sqrt2 |
Returns the square root of 2 (approximately 1.414) |
1 |
2 |
3 |
3: ABS () method
In this example, I will obtain the absolute values of positive and negative numbers:
<SCRIPT type = "text/JavaScript">
Document. Write (math. Abs (7.25) + "<br/> ")
Document. Write (math. Abs (-7.25) + "<br/> ")
Document. Write (math. Abs (7.25-10 ))
</SCRIPT>
Output:
7.25
7.25
4: floor () method
The floor () method performs an round-down computation. It returns an integer that is less than or equal to the function parameter and the nearest one:
<SCRIPT type = "text/JavaScript">
Document. Write (math. Floor (0.60) + "<br/> ")
Document. Write (math. Floor (0.40) + "<br/> ")
Document. Write (math. Floor (5) + "<br/> ")
Document. Write (math. Floor (5.1) + "<br/> ")
Document. Write (math. Floor (-5.1) + "<br/> ")
Document. Write (math. Floor (-5.9 ))
</SCRIPT>
Output:
0
0
5
5
-6
-6
5: Random () method
In this example, we will obtain a random number between 0 and 1:
<SCRIPT type = "text/JavaScript">
Document. Write (math. Random ())
</SCRIPT>
Output:
0.16184044615443094