Math函数的常用方法
Math.sqrt(a):计算a的平方根
System.out.println(Math.sqrt(16));
//输出:4.0
Math.cbrt(a):计算a的立方根
System.out.println(Math.cbrt(27));
//输出:3.0
Math.pow(a,b):计算a的b次方
System.out.println(Math.pow(2,3));
//输出:8.0
Math.max(a,b):计算最大值
System.out.println(Math.max(9,34));
//输出:34
Math.min(a,b):计算最小值
System.out.println(Math.min(9,34));
//输出:9
Math.abs(a):取a的绝对值
System.out.println(Math.abs(-28));
//输出:28
Math.ceil(a):对a向上取整
System.out.println(Math.ceil(2.09));
//输出:3.0
Math.floor(a):对a向下取整
System.out.println(Math.floor(4.98));
//输出:4.0
Math.rint(a):四舍五入,返回double值,注意小数点后面是.5的时候会取偶数
System.out.println(Math.rint(4.98));//5.0
System.out.println(Math.rint(3.5));//4.0
System.out.println(Math.rint(6.5));//6.0
System.out.println(Math.rint(1.9));//2.0
System.out.println(Math.rint(8.2));//8.0
Math.round(a):四舍五入,float时返回int值,double时返回long值
System.out.println(Math.round(2.8));//3
System.out.println(Math.round(3.5));//4
System.out.println(Math.round(6.5));//7
System.out.println(Math.round(1.16));//1
System.out.println(Math.round(9.2));//9
Math.random():取得一个[0,1)范围内的随机数
//[0,1)的double类型的数
System.out.println(Math.random());
//[0,100)的double类型的数
System.out.println(Math.random()*100);
//[10,20)的double类型的数
System.out.println(Math.random()*10+10);
Math.sin(a):返回角度的三角正弦值,角度以弧度表示(360度=2π弧度)
double pi=Math.PI;
System.out.println(Math.sin(0));//0.0
System.out.println(Math.sin(pi/2));//1.0
System.out.println(Math.sin(pi/6));//0.49999999999999994
Math.cos(a):返回角度的三角余弦值,角度以弧度表示
double pi=Math.PI;
System.out.println(Math.cos(0));//1.0
System.out.println(Math.cos(pi));//-1.0
System.out.println(Math.cos(pi/3));//0.5000000000000001
Math.tan(a):返回角度的三角正切值,角度以弧度表示
double pi=Math.PI;
System.out.println(Math.tan(0));//0.0
System.out.println(Math.tan(1));//1.5574077246549023
System.out.println(Math.tan(pi/3));//1.7320508075688767