This section provides a quick description and a tutorial example script on the 'Math' built-in object type. There is only one default 'Math' object called 'Math' that holds all math properties and functions.
The "Math" object type is a special built-in object type, that is used by the host environment
to create a single "Math" object as a container for many mathematics related properties and functions.
It has the following main features:
There is no way you can create your own "Math" objects, because the "Math()" constructor is not accessible.
The host environment creates the default "Math" object.
The default "Math" object has several value properties: E, LN10, LN2, PI, SQRT2, etc.
The default "Math" object has many function properties (methods): abs(), floor(), ceil(), round(), sin(), cos(), random(), etc.
Here is a tutorial example script showing some of the those features:
// Math_Object_Type.html
// Copyright (c) 2008 by Dr. Herong Yang, http://www.herongyang.com/
// Creating your own "Math" objects is not allowed
// var myObject = new Math();
// Checking this object
println("\nAbout this \"Math\":");
println(" Type = "+(typeof Math));
println(" Instance Of Object: "+(Math instanceof Object));
println(" Instance Of Function: "+(Math instanceof Function));
println(" Instance Of Math: "+(Math instanceof Math));
println(" toString(): "+Math.toString());
println("\n\"Math\" value properties:");
println(" Math.E = "+Math.E);
println(" Math.PI = "+Math.PI);
println(" Math.SQRT2 = "+Math.SQRT2);
println("\nCalling \"Math\" function properties - methods:");
println(" Math.sqrt(2) = "+Math.sqrt(2));
println(" Math.sin(Math.PI) = "+Math.sin(Math.PI));
println(" Math.abs(-17.78) = "+Math.abs(-17.78));
println(" Math.ceil(-17.78) = "+Math.ceil(-17.78));
println(" Math.floor(-17.78) = "+Math.floor(-17.78));
println(" Math.round(-17.78) = "+Math.round(-17.78));
println(" Math.random() = "+Math.random());
If you run this script with "jrunscript", you will get:
About this "Math":
Type = object
Instance Of Object: true
Instance Of Function: false
Instance Of Math: false
toString(): [object Math]
"Math" value properties:
Math.E = 2.718281828459045
Math.PI = 3.141592653589793
Math.SQRT2 = 1.4142135623730951
Calling "Math" function properties - methods:
Math.sqrt(2) = 1.4142135623730951
Math.sin(Math.PI) = 1.2246467991473532e-16
Math.abs(-17.78) = 17.78
Math.ceil(-17.78) = -17
Math.floor(-17.78) = -18
Math.round(-17.78) = -18
Math.random() = 0.4715524330340368
Now we know how to use the "Math" object.
Read other "Math" related chapters to see more tutorial examples.