Looking for the previous guiStuff?
It's still here, the content didn't go anywhere. You may want to check out this new guiStuff though -- It's rather informative.
References/Tutorials:
Intro Documents:
guiStuff:
::Stuff for the multi-spec coder;
Coding, formats, standards, and other practical things.
Home // JavaScript // The Math Object
<!-- JavaScript
Math Object
Introduction
The Math object in JavaScript is another key feature which are used in almost all our programs written in JavaScript. Using the Methods and Properties of the Math object you perform almost any calculations needed however complex they may be. So as you know there are two parts (Methods and Properties) to all objects in JavaScript, same is the case with the Math object.How to use the Math property and methods:
var aNum = Math.propertyname;
var aNum = Math.methodname();
var aNum = Math.methodname();
All the methods of the
Math object take one or two parameters for the function
call except the random() function. The object functions are called without
creating an instance of the Math Object. We shall see the various Properties and
Methods of the Math object now.Math object Properties
We have already seen how to use the properties of the Math object now we shall see what they actually are and what they do. The properties are mentioned below along with the syntax of use.E:
Math.EThe 'E' property is the symbol for the base of natural logarithm ( e ) which returns a constant value. ( approximately 2.71828 )
LN2:
Math.LN2The 'LN2' property will return the logarithm of 2 to the base 'e', which is also the same as natural logarithm. The constant returned has the approximate value of 0.69314.
LN10:
Math.LN10The 'LN10' property will return the natural logarithm of 10, approximate value of 2.303.
PI:
Math.PIThe 'PI' is one of the more widely used property of the Math object and returns the value of the constant, PI (? / p ) - 3.1416 (aprox.)
SQRT2 / SQRT1_2:
Math.SQRT2 / Math.SQRT1_2The 'SQRT2' returns the square root of 2 and 'SQRT1_2' returns the square root of half.
LOG2E / LOG10E:
Math.LOG2E / Math.LOG10EThe 'LOG2E' returns logarithm of 2 to the base 'e' and 'LOG10E' returns the logarithm of 10 to the same base.
Note: All the properties of the Math object returns a constant value when they are used, and above were the list of all the Properties of the Math object.
Math object Methods
random() :
Math.random() The
random() function returns a pseudorandom number within the range of 0 and
1. The numbers obtained by the use of this function are floating point. To
convert these floating point numbers to integers we make use of another Math
object method called floor(). The
Math.floor() will remove anything on the right side of the decimal and
return you the integer on the left side. Be careful here as even if the number
is 2.999 the floor (2.999) method will convert it into 2. You can basically call
this a round down function, since it rounds the value of the number to the lower
integer. Therefore this function is not to be used for rounding off to the
nearest integer, we will come to that too.
var rand = Math.floor(Math.random(x));
The above statement will store either a 0 or a 1 in variable
rand.Now lets try and make more use of the
random() function. If you want to obtain
pseudorandom numbers between 0 and a bigger number, lets say 10. We will
appropriately modify the random() function to accommodate this change.
var result = Math.floor(Math.random( ) * (i + 1));
The above statement will return a number between 0 and
i.To change it even further what if you want to keep numbers only higher than a particular number and within a certain range. Then we can alter it even further;
var rand = Math.floor(Math.random( ) * (i - j + 1)) + j;
This code above will return a number higher than
j but lower than i. Since we discussed about the
floor() function returning the number which is
rounded down to the smaller integer, we can now take a look at the ceil()
function which will round up the number x to the higher integer.
var rand = ceil(22.49)
This will return 23 into the variable
rand.The
round() function will round the number x to the nearest integer, taking 0.5
as the cut-off. Thus the rounding off of 0.5 and + will return 1.
var rand = round(22.49);
var rand = round(22.50);
var rand = round(22.51);
var rand = round(22.50);
var rand = round(22.51);
The values returned in these cases will be 22, 23 and 23 respectively.
abs():
Math.abs()The
abs() method returns the absolute value of the number x. Hence a positive
number will return the same value and a negative number will be changed to
positive.
document.write(Math.abs(2));
document.write(Math.abs(-1));
document.write(Math.abs(-1));
These statements will print 2 and 1 respectively.
sqrt():
Math.sqrt()The
sqrt(x) will return the square root of the number which is taken as the
parameter. You can only supply positive values for x, if a negative value is
passed within the sqrt(x) function then the result will be NaN.
document.write(Math.sqrt(2));
document.write(Math.sqrt(16));
document.write(Math.sqrt(-1));
document.write(Math.sqrt(16));
document.write(Math.sqrt(-1));
The above statements will print 1.414, 4 and
NaN respectively.max(x, y) / min(x, y) :
Math.min(x, y) / Math.max(x, y)The above
max(x, y) and min(x, y) functions returns the larger and smaller of
the two numbers (x, y) respectively.
document.write(Math.max(2, 3));
document.write(Math.min(-1, 2));
document.write(Math.min(-1, 2));
The values these statements will print are 3 and -1 respectively.
sin() / cos() / tan() :
Math.sin() / Math.cos() / Math.tan()The
sin(x), cos(x) and tan(x) functions returns the sine, cosine and tangent of
the angles specified as parameters. The angles x, need to be specified in
radians to get correct results.
document.write(Math.sin(0.2));
document.write(Math.cos(0));
document.write(Math.tan(-1));
document.write(Math.cos(0));
document.write(Math.tan(-1));
We will get 0.1986, 1 and 0.3093 printed on the screen as a result of these statements.
asin() / acos() / atan() :
Math.asin() / Math.acos() / Math.atan()The
asin(x), acos(x) and atan(x) function are inverse functions of the sin(x),
cos(x) and tan(x) functions. The asin(x) will return the arc sine of x between
PI/2 and -PI/2 radians. The acos(x) will return the arc cosine of x between 0
and PI radians. The atan(x) will give the arc tan of x between PI/2 and -PI/2
radians.
document.write(Math.asin(1));
document.write(Math.acos(2));
document.write(Math.atan(-1));
document.write(Math.acos(2));
document.write(Math.atan(-1));
The values printed due to the above statements are 1.5707,
NaN and 0.7853.pow(x, y):
Math.pow(x, y)The
pow(x, y) method will return xy which is the yth power of x.
document.write(Math.pow(2,2));
This will print the value value 4.
exp():
Math.exp()The
exp(x) method will return the resulting number after performing the
operation of E raised to the power of x.
document.write(Math.exp(2));
document.write(Math.exp(1));
document.write(Math.exp(1));
The values returned by these statements are 7.3890 and 2.518 respectively.
log():
Math.log()The
log(x) function returns the logarithm of x to the base E.
document.write(Math.log(2));
document.write(Math.log(-2));
document.write(Math.log(-2));
The above statements will return 0.693 and
NaN. NaN since log of a negative
number cannot be calculated.Return to the JavaScript section, or go the to Main page.