|
PIProbably 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_2The 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
SQRT2The 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 MethodsConstant 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:
|
||||||||||||||||||||
With any suggestions or questions please feel free to contact us |