Mathオブジェクト

pi  |   ceil()/floor()/round()  |   max()/min()  |   power()  |   random()  |   sqrt()  |   sin()/cos()/tan()  |  


円周率

Math.PI

円周率π(3.141592.............)の値を格納したプロパティ。


切上げ・切捨て・四捨五入

Math.ceil(n)
 n の小数点第1位を切上げた整数値を計算。

Math.floor(n)
 n の小数点第1位を切捨てた整数値を計算。

Math.round(n)
 n の小数点第1位を四捨五入した整数値を計算。


最大値、最小値)

Math.max(x, y)
Math.min(x, y)

max() は x と y の大きい方、min() は x と y の小さい方を返す。


階乗

Math.pow(n, m)

n の m 乗を計算。


乱数

Math.random()

0以上、1未満の乱数を生成する。
次のようにすると、0~5の乱数を生成することができる。
xx = Math.floor(Math.random() * 6);


平方根

Math.sqrt(n)

n の平方根を計算。


三角関数

Math.sin(x)
Math.cos(x)
Math.tan(x)

x のsin(-1~1)、cos(-1~1)、tan(-∞~∞)を計算する。
x はラジアン単位で指定する。180度はπラジアン、1度はπ/180ラジアン。 sin30度の値は次のように計算できる。
a = 30.0;  // 度単位の角度
kotae = Math.sin(a * (Math.PI / 180) );

arigat アットマーク acm.org
Last modified: Oct. 2014