Math objects and their methods & properties

Notes from April 27, 2005 class

The Math object in JavaScript has lots of higher-level math functions within it, and is considerably more complex than we'll get into in this class. If you want detailed information on what's available via the Math object, check Appendix B of the textbook.

JavaScript automatically creates the Math object and runs it in every script, you don't have to declare it or otherwise conjure it into being.

The Math.PI property

Among the properties of JavaScript's Math object are some useful math constants, such as the value of pi.

The formula for determining the area (x) of a circle with radius (r) is x = pi r squared. Since JavaScript's Math object already knows the value of pi, we don't have to look it up and hard-code it in, we can just use the PI property of the Math function.

var radius=prompt("Give the radius of the circle","");
var area=(Math.PI)*radius*radius;
document.write("The area is " + area);

The Math.abs() method

The Math.abs() method returns the absolute value of a number. Basically, the absolute value is the positive value of the number, even if it's a negative number. Math.abs(-32) would be 32, for example.

So, in this example:

var age=prompt("how old are you?", "");
var shoesize=prompt("what size shoes do you wear?","");
var magicnumber= (shoesize -= age);
document.write("The difference between your age and shoe size is "+Math.abs(magicnumber));

If your shoe size is 12 and your age is 52, the value of shoesize minus age would be -40, but the absolute value would be 40.

Methods for rounding numbers

parseInt() chops off everything after the decimal point
Math.ceil() rounds a number up to the next whole number
Math.floor() rounds a number down to the previous whole number
Math.round() rounds up if the decimal > .5, otherwise it rounds down

var rounding=prompt("Enter a number that's not a whole number","");
document.write(parseInt(rounding)+ " is "+rounding+" via parseInt().<br />");
document.write(Math.ceil(rounding)+ " is "+rounding+" via ceil().<br />");
document.write(Math.floor(rounding)+ " is "+rounding+" via floor().<br />");
document.write(Math.round(rounding)+ " is "+rounding+" via round().<br />");

The Math.random() method

Math.random() returns a random floating-point number between 0 and 1, where zero is included and one is not. You have to do some work to get whole numbers out of this. This takes some getting used to...

The book teaches us to gamble by way of this script that simulates rolling dice:

var throwCount;
var diceThrow;
for (throwCount = 0; throwCount < 10; throwCount++)
{
diceThrow = (Math.floor(Math.random() * 6) + 1);
document.write(diceThrow + "<br />");
}

Huh?

Math.random() gave us numbers >= 0 and < 1.

Multiplying that by 6 gave us numbers >=0 and < 6.

Adding 1 to that gave us numbers >=1 and < 7.

Math.floor rounded all these down to the nearest whole number, giving us whole numbers between 1 and 6

If you want random numbers between 1 and 100, just change the multiplier from 6 to 100.

Exponents: the Math.pow() method

Holy leaping larrikins, Batman, it's the pow() method. This raises a number to a specified power. It takes two parameters, the number you want to multiply and the exponent.

Earlier, we used this calculation to square the value of the radius of a circle and multiply it by the value of pi:

var area=(Math.PI)*radius*radius;

We can use Math.pow() to square the radius, instead.

var radius=prompt("Give the radius of the circle","");
var radius2=Math.pow(radius,2);
var area=(Math.PI)*radius2;
document.write("The area is " + area+"<br />");

We can also write out Avagadro's number:

document.write("Avagadro's number is 6.02 times 10 to the 23rd power, or "+6.02*Math.pow(10,23));

If you do this, you'll get an equation that includes e23. e23 is scientific notation for 10 to the 23rd power.

We'll try something simpler to make sure it looks right.

document.write("Ten to the third power, or 10 x 10 x 10, is "+Math.pow(10,3));