数学类 Math
常用的方法:
- abs(参数) 绝对值
- acos(参数)、asin、atan、cos、sin、tan 三角函数
- sqrt(参数) 平方根
- pow(double a,double b) a的b的次幂
- log(参数) 自然对数
- exp e为底的指数
- max(double a,double b) a、b中的最大值
- min(double a,double b) a、b中的最小值
- random() 返回0.0到1.0的随机数
- random().nextInt(n) 返回[0,n)的随机数
- long round(double a) double型的数据a转换成long型数据(四舍五入)
- toDegrees(double angrad) 弧度 转化成 角度
- toRadians(double angdeg) 角度 转化成 弧度
代码举例:
public class MathTest {
public static void main(String[] args) {
double a=-3.14;
System.out.println(a+"的绝对值:"+Math.abs(a));
int b =4;
System.out.println(b+"的平方根:"+Math.sqrt(b));
System.out.println(b+"的2的次幂:"+Math.pow(b,2));
double c= 3.15;
double d= 4.5;
System.out.println(c+"和"+b+"之间的最大值:"+Math.max(c, d));
}
}
结果: