0% found this document useful (0 votes)
1 views6 pages

Math Methods

Maths methods to use.

Uploaded by

duharpita
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views6 pages

Math Methods

Maths methods to use.

Uploaded by

duharpita
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Math Methods

Math.pow(x,y)
The Math.pow(x,y) method is used to return the value of x to the power y

<html>
<body>
<h2>The pow() function</h2>
<p>This method returns the result of x raised to power y</p>
<script>
function powerFunction(p1, p2) {
return Math.pow(p1,p2);
}
document.write( powerFunction(4, 3))
</script>
</body>
</html>

The Math.sqrt( ) method returns the square root of x

<html>
<body>
<h2>The sqrt() method</h2>
<p>This method returns the square root of the value passed</p>
<script>
function myFunction(p1) {
return Math.sqrt(p1);
}
document.write( myFunction(64))
</script>
</body>
</html>
Math.ceil() and Math.floor() methods
<html>
<head>
<title>The Math.ceil and Math.floor()</title>
<p>Click the button to round up the number</p>
<button onclick="roundup()">Round Up</button>
<p>Click the button to round down the number</p>
<button onclick="roundown()">Round Down</button>
<p>The Math.ceil() function returns the integer greater than or equal to the provided
number.</p>
<p id="msg1"></p>
<p>The Math.floor() function returns the integer smaller than or equal to the provided
number.</p>
<p id="msg2"></p>
<script>
function roundup(){
document.getElementById("msg1").innerHTML = Math.ceil(53.478);}
function roundown(){
document.getElementById("msg2").innerHTML = Math.floor(53.478);}
</script>
</head>
</html>

Math.max() and Math.min() methods


<html>
<head>
<title>Max and Min Methods</title>
<p>The <code>Math.max()</code> function returns the largest of zero or more
numbers.</p>
<p>The <code>Math.min()</code> function returns the smallest of zero or more
numbers.</p>
<script>
document.write(Math.max(-8,9,34,52,18,23),"<Br>")
document.write(Math.min(-8,9,34,52,18,23))
</script></head></html>
Math.PI property

<!DOCTYPE html>
<html><head>
<title>Math.PI</title>
<p><code>Math.PI</code> is a static property that represents the ratio of the
circumference of a circle to its diameter.
<br> Therefore the result will always the same number, regardless of the circle's size.</p>
<p id="msg"></p>
<script>
document.getElementById("msg").innerHTML = Math.PI;
</script>
</head>
</html>

Math.round() method
<!DOCTYPE html>
<title>Math.round()</title>
<p>The <code>Math.round()</code> function returns the value of the given number
rounded to the nearest integer.</p>
Rounding off 53.478
<p id="msg1"></p>
Rounding off 53.512
<p id="msg2"></p>
<script>
document.getElementById("msg1").innerHTML = Math.round(53.478);
document.getElementById("msg2").innerHTML = Math.round(53.512);
</script>
The Math.random()
The Math.random() function returns a floating-point, random number between 0 (inclusive)
and 1 (exclusive).

<!DOCTYPE html>
<html>
<head><title>Math.random()</title></head>
<body>
<p>The <code>Math.random()</code> function returns a floating-point, random number
between 0 (inclusive) and 1 (exclusive).</p>
<p id="msg1"></p>
Manipulating the random number
<p id="msg2"></p>
<script>
document.getElementById("msg1").innerHTML = Math.random();
document.getElementById("msg2").innerHTML = Math.random()*100;
</script>
</body>
</html>

<!DOCTYPE html>
<HTML>
<head><title>Manipulating Math.random()</title></head>
<body>
<p><code>Math.random()</code> returns a floating-point number
<p id="msg1"></p>
We can multiply Math.random() by 100 then use <code>Math.floor()</code>
to return the largest integer less than or equal to the random number.
<p id="msg2"></p>
We can multiply Math.random() by 100 then use <code>Math.ceil()</code>
to return the smallest integer greater than or equal to the random number.<br>
This provides a more uniform distribution than using <code>Math.round()</code>.</p>
<p id="msg3"></p>
<script>
document.getElementById("msg1").innerHTML = Math.random();
document.getElementById("msg2").innerHTML = Math.floor(Math.random() * 100);
document.getElementById("msg3").innerHTML = Math.ceil(Math.random() * 100);
</script>
</body>
</HTML>

You might also like