Math类的几个用法:
var r = 2;
var s1 = Math.PI * Math.pow(r,r); //圆周率 * r 的平方 ,面积运算公式
var s2 = Math.floor(s1); //得到不大于s1的最大整数
var s3 = Math.ceil(s1); //得到不小于s1的最大整数
var s4 = Math.round(s1); //得到不小于s1的最大整数
var s5 = parseInt(s1); //得到s1整数部分
cc.log("面积:" + s1 ); //面积:12.566370614359172
cc.log("s2: " + s2); //s2: 12
cc.log("s3: " + s3); //s3:13
cc.log("s4: " + s4); //s4: 13
cc.log("s5: " + s5); //s5: 12
cc.log(s4 == s3); //true
cc.log(s5 == s2); //true
var arr = [1,9,5,5,6,7,1,5,2,1];
for(var j = 0 ; j+1 < arr.length;j++){
for(var i = 0 ; i+1 < arr.length;i++){
var temp = arr[i];
var temp2 = arr[i+1];
var a = this.sort(temp,temp2);
if(a){
arr[i] = temp;
}
else{
arr[i] = temp2;
arr[i+1] = temp;
}
}
}
cc.log("按元素最小顺序排序: " + arr); //arr = [1,1,1,2,5,5,5,6,7,9];
sort:function(a,b){
var minnum = Math.min (a,b); //取a,b最小值
//var maxnum = Math.max (a,b); //取a,b最大值
if(minnum == a)
return true;
else
return false;
},
输出:
1.Math.PI 记录的圆周率
2.Math.E记录e的常量3.Math.abs 求绝对值
4.Math.sin 正弦函数 Math.asin 反正弦函数
5.Math.cos 余弦函数 Math.acos 反余弦函数
6.Math.tan 正切函数 Math.atan 反正切函数 Math.atan2 商的反正切函数
7.Math.toDegrees 弧度转化为角度
8.Math.toRadians 角度转化为弧度
9.Math.ceil 得到不小于某数的最大整数 Math.ceil(12.7) =13.010.Math.floor 得到不大于某数的最大整数 Math.floor(12.7) =12.0
11.Math.remainder 求余
12.Math.max 求两数中最大
13.Math.min 求两数中最小
14.Math.sqrt 求开方
15.Math.pow 求某数的任意次方, 抛出ArithmeticException处理溢出异常
16.Math.sqrt(x):平方根
17.Math.pow(x,y):x的y次方
18.Math.exp 求e的任意次方
19.Math.log10 以10为底的对数
20.Math.log 自然对数
21.Math.rint 求距离某数最近的整数(可能比某数大,也可能比它小)
22.Math.round 同上,返回int型或者long型(上一个函数返回double型)
23.Math.random 返回0,1之间的一个随机数