JScript の Math オブジェクト
更新 : 2007 年 11 月
Math オブジェクトには、組み込みのプロパティとメソッドが多数用意されています。プロパティは特定の数値です。
Math オブジェクトの使用
これらの特定の数値の 1 つに、π (約 3.14159...) があります。これは Math.PI プロパティの値で、次のように使用されます。
// A radius variable is declared and assigned a numeric value.
var radius = 4;
var area = Math.PI * radius * radius;
// Note capitalization of Math and PI.
Math オブジェクトの組み込みメソッドに、指数演算メソッドの pow があります。これは、数値を指定した回数だけ累乗します。次の例では、π と指数演算の両方を使用しています。
// This formula calculates the volume of a sphere with the given radius.
var volume = (4/3)*(Math.PI*Math.pow(radius,3));
別の例を示します。
var x = Math.floor( Math.random()*10 ) + 1;
Math オブジェクトは明示的に作成できませんが、常にプログラムで利用できます。