1.1 三角函数方法
System.out.println(Math.toDegrees(Math.PI/2));//弧度单位化角度
System.out.println(Math.toRadians(30));//角度单位化弧度
System.out.println(Math.sin(Math.PI/6));//求sin值
System.out.println(Math.cos(Math.PI/6));//求cos值
System.out.println(Math.asin(Math.PI/6));//求反三角函数
1.2 指数函数方法
System.out.println(Math.exp(3.5));
System.out.println(Math.log(3.5));
System.out.println(Math.log10(3.5));
System.out.println(Math.pow(2, 3));
System.out.println(Math.sqrt(10.5));
1.3 取整方法
System.out.println(Math.ceil(2.1));//2.1向上取整为它最接近的整数,该整数作为一个双精度值返回,此例返回3.0
System.out.println(Math.floor(2.1));//2.1向下取整为它最接近的整数,该整数作为一个双精度值返回,此例返回2.0
System.out.println(Math.rint(-2.1));//x取整为它最接近的整数,如果.5距离相等。则返回偶数的那个(-2.0)
System.out.println(Math.rint(-2.5));
System.out.println(Math.round(2.6f));//如果是单精度数,返回(int)Math.floor(x+0.5);如果x是双精度数,返回(long)Math.floor(x+0.5);(3)
System.out.println(Math.round(2.5));
1.4 min、max、和abs方法
System.out.println(Math.max(Math.max(2.5, 4.6), Math.min(3, 5.6)));//此例返回4.6
System.out.println(Math.abs(-2.6));//此例返回2.6
1.5 最小化数值错误
在循环中,如果使用浮点数大小比较大小作为判断条件,那么浮点数涉及的误差就是不可避免的,因为浮点数在计算机中本身就是近似表示的。所以判断条件应该尽量选择整型数。