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

PI

Probably the most known value among all constants featured by JavaScript is pi. Its approximate value, as enabled by the precision limits of real numbers in JavaScript, is 3.141592653589793. As before, here is an equation to demonstrate this value:

[pi] = 4(1 – 1/3 + 1/5 – 1/7 + 1/9 – ...)

As you could expect, you refer to pi in JavaScript as Math.PI. For example, to obtain the circumference of a circle you can use the following function:

function circumference(diameter) {
  if (typeof diameter == "number" && diameter >= 0)
 return Math.PI * diameter
}

Note that the function does not return a value if the diameter is not a number or is not positive.

SQRT1_2

The square root of 0.5, as stored in JavaScript’s object model, is 0.7071067811865476. You can also reach this value by calculating the square root of 0.5 (using the sqrt() method of the Math object), but accessing an existing property is somewhat faster and more obvious than calculating it via an execution of a method.

You refer to this property as Math.SQRT1_2. The reason an underscore is used for the property name is that the name of a property must follow the identifier rules which allow only letters, numbers, and underscores in the middle of a name.

Here is an obvious statement to confirm that the value is correct:

document.write(Math.pow(Math.SQRT1_2, 2))

The not-so-obvious output is:

0.5000000000000001

SQRT2

The square root of 2 is also a well-known constant. Its approximate value is 1.4142135623730951. You refer to it as Math.SQRT2. You can use the following statement to ensure the value:

document.write(Math.pow(Math.SQRT2, 2))

As you could expect, the result is not an exact one:

2.0000000000000004

Math Methods

Constant values make up only a fraction of the entire strength of mathematical implementation in JavaScript. To harness the power of the Math object you have to familiarize yourself with the set of methods available.

JavaScript tends to organize functions and values in object structures to enable easy reference and simple understanding. For this reason, all functions related to math are implemented as methods of the Math object.

The methods of the Math object can be divided into two categories, each one related to a different branch of mathematics:

  • Arithmetic methods
  • Trigonometric methods

    All methods of the Math object are specified in lowercase letters, as opposed to constants which are properties of this object, and are specified in uppercase letters.

    Arithmetic Methods

    We use the term “arithmetic methods” to describe all methods that do not relate in any way to trigonometric math.

    abs()

    You can calculate the absolute value of any number, integer or floating point via this method. The absolute value of a number is its corresponding positive number; that is, if the number is positive, its absolute value is the number itself, whereas if it is negative, its absolute value is the same number with a + sign instead of a –. You can simulate this method easily:

    function abs(num) {
      if (num < 0)
    return –num
      return num
    }
    

    For example, the absolute value of –5 is 5. The absolute value of 5 is also 5. In JavaScript you can calculate the absolute value of a number via the method Math.abs(). This method returns the absolute value of its argument. You can use this method to compute the absolute value of only one argument. If you call the method with more than one, only the absolute value of the first is returned.

    If you want the corresponding negative value of a number, as opposed to the positive value, you can negate the returned value. For example:

    var neg1 = –Math.abs(–3.7)
    var neg2 = –Math.abs(3.7)
    

    You can use the Math.abs() for many purposes, not only in mathematical algorithms.

    ceil()

    The Math.ceil() method accepts a single numeric argument and returns the next integer greater than or equal to the argument (rounding up). Therefore, the returned value is never less than the argument. Here are a few examples:

    Math.ceil(16) == 16
    Math.ceil(16.01) == 17
    Math.ceil(–15.01) == –15
    

    Let’s say you need to fit num1 cars into parking lots, where each parking lot has space for num2 cars. You can use the Math.ceil() method along with a function to calculate the minimum number of parking lots:

    function getNumLots(num1, num2) {
      return Math.ceil(num1 / num2)
    }
    

    The reason why we need to use this method in the above function is that you can only use a whole parking lot, not a fractional part; that is, we are trying to calculate the minimum number of parking lots needed, not the exact space.

    exp()

    This method returns Euler’s constant to the power of the specified argument (eargument). It is approximately equivalent to the following function:

    function exp(num) {
      return Math.pow(Math.E, num)
    }
    

    You refer to this function as Math.exp(). Here is an example:

    document.write(Math.exp(4))
    

    Its output is:

    54.598150033144236
    

    floor()

    The Math.floor() method returns the greatest integer less than or equal to the value passed to it. This is equivalent to integral division when dealing with non-negative numbers. It is also equivalent to rounding down to the nearest integer. Here are a few expressions, each evaluating to true:

    Math.floor(16) == 16
    Math.floor(16.01) == 16
    Math.floor(–15.01) == –16
    

    log()

    This method returns the natural logarithm of the argument passed to it. For example, the natural log (base e) of e (Euler’s constant) is 1. You can confirm it via the following statement:

    document.write(Math.log(Math.E))
    

    And indeed, the output is 1.

    max(), min()

    Both of these methods accept two numeric arguments. max() returns the greater of two numbers, whereas min() returns the lesser of the two. Here is a function that prints the lesser of two numbers followed by the greater:

    function printInOrder(num1, num2) {
    document.write(Math.min(num1, num2)+", "+Math.max(num1, num2))
    }
    

    The following function call prints the string “–5, 1” to the document:

    printInOrder(1, –5)
    




With any suggestions or questions please feel free to contact us