Math objects
The Math object provides a number of useful methods and properties for mathematical processing. For example,
Math provides the following property:
Math.PI
that is useful for many geometric calculations.
An example of some other methods provided by the Math object include:
Math.abs();
Math.round();
Math.max( n1, n2 );,etc
Explanation:
1.math.PI: it is show the Î (pi) value.
2.Math.round(): it returns the value of x rounded to its nearest integer.
3. Math.pow(x,y): it returns the value of x to the power of y.
4.Math.sqrt(x): it returns the square root of x.
5.Math.abs(x): it returns the absolute (positive) value of x.
6.Math.sin(x): it returns the sine (a value between -1 and 1) of the angle x (given in radians).
If you want to use degrees instead of radians, you have to convert degrees to radians:
Angle in radians = Angle in degrees x PI / 180.
7.Math.min() and Math.max()
Math.min()
and Math.max()
can be used to find the lowest or highest value in a list of arguments.
8. Math.random() : it returns a random number between 0 (inclusive), and 1 (exclusive).
These may be used in the following way:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>math object</title>
</head>
<body bgcolor="orange">
<script type="text/javascript">
document.write("Math.PI="+Math.PI+"<br>");
document.write("Math.round(4.7)=" +Math.round(4.7)+"<br>");
document.write("Math.pow(4,2)="
+Math.pow(4,2)+"<br>");
document.write("Math.sqrt(64)="
+Math.sqrt(64)+"<br>");
document.write("Math.abs(-13)="
+Math.abs(-13)+"<br>");
document.write("Math.sin(90 * Math.PI / 180)="
+Math.sin(90 * Math.PI / 180)+"<br>");
document.write("Math.min(-500,-60,1,2,100)="
+Math.min(-500,-60,1,2,100)+"<br>");
document.write("Math.max(-500,-60,1,2,100)="
+Math.max(-500,-60,1,2,100)+"<br>");
document.write("Math.random(2)="
+Math.random(2)+"<br>");