Techcss
Javascript_page_menu

JavaScript Math Object

The 'Math' object consists methds and properties which perform complex operations exclusively than only arithmetic operators able to do alone.


The special values and functions

Name Description
E Euler's constant (approximately 2.718)
LN10 Natural logarithm of 10 (approx. 2.302)
LN2 Natural logarithm of 2 (approx. 0.693)
PI Ratio of circumference to diameter of a circle (approx. 3.1415)
SQRT1_2 Square root of 1/2 (approx. 0.707)
SQRT2 Square root of 2 (approx 1.414)
abs( ) Calculates absolute value of number
acos( ) Calculates arc cosine of number
asin( ) Valculates arc sine of number
atan( ) Calculates arc tanent of number
ceil( ) Returns next integer greater than or equal to a number
cos( ) Calculates cosine of number
exp( ) Calculates power of a number
floor( ) Returns next integer less than or equal to a number
log( ) Calculates natural logarithm of a number
max( ) Returns the greater of two numbers - takes 2 arguments
min( ) Returns the least of two numbers - takes 2 arguments
pow( ) Calculates value of one number to the power of another - takes 2 arguments
random( ) Returns a random number between 0 and 1
round( ) Rounds a number to the nearest integer
sin( ) Calculates the sine of a number
sqrt( ) Calculates square root of a number
tan( ) Calculates the tangent of a number

Example
<html>
<head>
<script>
	function circle(form,changed) {

		with (Math) {

		var area = form.area.value;
		var dia = form.diameter.value;
		var circ = form.circumference.value;

		if(changed == "area") 
     		{
			var rad = (sqrtarea/PI)
			dia = 2*rad;
            circ = PI*dia;
      		}
         if(changed=="diameter")
         	{
            area = PI*(dia/2)*(dia/2);
            circ = PI*dia;
            	}
          if(changed=="circumference")
          	{
            dia=circ/PI;
            area=PI*(dia/2)*(dia/2);
            }
     
     form.area.value = area;
     form.diameter.value = dia;
     form.circumference.value = circ;
     	}
     }
     
     function square(form.changed)
     {
      with(Math) 
      {
       var area=form.area.value;
       var circum=form.circumference.value;
       
       if(changed=="area")
       {
        var side = sqrt(area);
        circum = 4*side;
       }
        
       if(changed=="circumference")
       {
        var side = circum/4;
        area = side*side;
       }
       form.area.value=area;
       form.circumference.value=circum;
       }
      }
</script>
</head>
<body>
	<table width = "100%">
	<tr>
	<td>
	<h1>Circle</h1>
	<form method="POST">
		Area: <input type="text" name="area" value="0"
		onChanged="circle(this.form, this.name);">
		<br>
		Circumference: <input type="text" name="circumference" value="0"
		onChange="circle(this.form, this.name);">
		<br>
	</form>
	</td>
	<td>
	<h1>Square</h1>
 <form method="POST">
		Area: <input type="text" name="area" value="0"
		onChange="square(this.form, this.name)">
		<br />
		Circumference: <input type="text" name="circumference" value="0"
		onChange="square(this.form, this.name);">
		<br />
	</form>
</td>
</tr>
</table>
</body>
</html>
 
JavaScript String Example     JavaScript string demo download