Online Documentation Server
 ПОИСК
ods.com.ua Web
 КАТЕГОРИИ
Home
Programming
Net technology
Unixes
Security
RFC, HOWTO
Web technology
Data bases
Other docs

 


 ПОДПИСКА

 О КОПИРАЙТАХ
Вся предоставленная на этом сервере информация собрана нами из разных источников. Если Вам кажется, что публикация каких-то документов нарушает чьи-либо авторские права, сообщите нам об этом.




Previous Table of Contents Next

Here are a few true expressions to demonstrate the basic min() and max() methods:

Math.max(1, 2) == 2
Math.min(2, Math.abs(–2)) == 2
Math.min(2, –2) == –2

pow()

Given two numeric arguments, this method returns the first one to the power of the second. Here are a few true expressions demonstrating the method:

Math.pow(10, 2) == 100
Math.pow(0.5, 0.5) == 0.7071067811865476
Math.pow(Math.SQRT2, 4) == 4.000000000000001

random()

This method returns a random number between 0 and 1. It is obviously a floating point number. The returned number’s precision is usually 16 digits after the decimal point (or less, if last digits were chosen to be 0). Here is a simple example:

for (var i = 0; i < 5; ++i) {
   document.write(Math.random() + "<BR>")
}

The output of this loop depends on the output of random() and it is guaranteed that your results will not be the same as the following:

.924853870611902
.8248305636609181
.9539277224126104
.9806934571332098
.7639888801207115

This method is mostly used to create random integer numbers between x and y. Suppose x is 0 and y is a given number. You should multiply the value that random() returns by y and then round it off. For example, to generate a random number between 0 and 37 you can use the following expression:

Math.round(Math.random() * 37)

If you want an integer between 15 and 37 you can create a random integer between 0 and 22 and then add 15. Be very careful when attempting to create random numbers.

round()

The Math.round() method returns the nearest integer to the argument. If the argument’s decimal part is equal to 0.5, the number is rounded upwards. Here are a few true expressions to demonstrate the method:

Math.round(3.7) == 4
Math.round(4.5) == 5
Math.round(16.1) == 16
Math.round(0) == 0

sqrt()

This method returns the square root of the argument. For example:

Math.sqrt(4) == 2

Math.sqrt(0) == 0
Math.sqrt(0.25) == 0.5

If the argument is a negative number, the method returns zero, which happens to be the wrong answer. It would be better if an error was generated instead, because this wrong answer can go undetected.

Trigonometric Methods

Trigonometric methods are obviously those that deal with trigonometry.

You should also know how to convert an angle from degrees to radians, and vice versa. Here is the basic conversion table:

Table 15-1. Degree-radian conversion table.


Degrees Radians

360 2[pi]
270 1.5[pi]
180 [pi]
90 0.5[pi]

All angles in JavaScript are measured by radians, so the conversion table should help you visualize the size of an angle in radians.

cos()

The Math.cos() method accepts one argument, the angle of a triangle. It returns the cosine of that value, which must be specified in radians. The following statement prints “–1”:

document.write(Math.cos(Math.PI))

acos()

The Math.acos() method also accepts one argument. It returns the arc cosine of the argument in radians; that is, it accepts the cosine of a certain value and returns that value—the opposite of the Math.cos() method. Therefore, the following statement prints the value of PI:

document.write(Math.acos(–1))

sin()

The Math.sin() function returns the sine of its argument. Keep in mind that the argument must be in radian units. Here is a statement that prints “1”:

document.write(Math.sin(0.5 * Math.PI))

asin()

The Math.asin() method accepts one argument and returns its arc sine in radians. The following statement prints half the value of pi:

document.write(Math.asin(1))

tan()

The Math.tan() method returns the tangent of its argument, which is equal to the quotient of the value’s sine and cosine. Take a look at the following script segment:

var val = 0.25 * Math.PI
document.write("sine = " + Math.sin(val) + "<BR>")
document.write("cosine = " + Math.cos(val) + "<BR>")
document.write("tangent = " + Math.tan(val))

The output of these statements in Netscape Navigator is:

sine = .7071067811865475
cosine = .7071067811865476
tangent = .9999999999999999

From this script you can learn:

  • The sine of ? * pi is equal to its cosine which is also equal to the square root of ?.
  • The cosine and sine methods sometimes return inaccurate results.

Take extra caution regarding the second concept—inaccuracy. The following statements:

var val = 0.25 * Math.PI
if (Math.tan(val) == 1)
   do_this
else
   do_that

should be replaced by:

var val = 0.25 * Math.PI
if (Math.tan(val) > 0.99 && Math.tan(val) < 1.01)
   do_this
else
   do_that

Inaccuracy is more obvious in this case, because the result differs even from browser to browser. The following is the output received from Microsoft Internet Explorer:

sine = 0.707106781186547
cosine = 0.707106781186548
tangent = 1

Notice that the sine and cosine values differ. Also notice that Internet Explorer appends a leading zero digit to all numbers between –1 and 1 (not inclusive).

atan()

As you could expect, the Math.atan() method returns the arc tangent of its argument. For example, the following returns one-fourth of the value of pi:

document.write(Math.atan(1))

atan2()

The Math.atan2() returns the angle (theta component) of the polar coordinate (r, theta) that corresponds to the specified Cartesian coordinate. You probably know that the normal x, y coordinates of a point are called Cartesian coordinates. Another measurement system is the polar one. You need to specify the point’s radius (distance from the pole) and angle (theta component). The Math.atan2() method actually returns the arc tangent of x/y in the range –[pi] to [pi]. A quick way to get the value of [pi] via this method is to say:

var pi = Math.atan2(1, 1) * 4

First of all, atan2(1, 1) is equal to atan(1), which is ?[pi] in radians. So atan2(1, 1) is equal to [pi]. In general, atan2(a, a) * 4 is equal to [pi], because a/a is always 1.

Previous Table of Contents Next


With any suggestions or questions please feel free to contact us