北京注册公司核名网站,网站制作结算确认函,四川住房和城乡建设厅网站不能进入,上海网站优化公司Java Math 类
Java 的 Math 包含了用于执行基本数学运算的属性和方法#xff0c;如初等指数、对数、平方根和三角函数。
Math 的方法都被定义为 static 形式#xff0c;通过 Math 类可以在主函数中直接调用。 public class Test { public static void main (String []args…Java Math 类
Java 的 Math 包含了用于执行基本数学运算的属性和方法如初等指数、对数、平方根和三角函数。
Math 的方法都被定义为 static 形式通过 Math 类可以在主函数中直接调用。 public class Test { public static void main (String []args) { System.out.println(90 度的正弦值 Math.sin(Math.PI/2)); System.out.println(0度的余弦值 Math.cos(0)); System.out.println(60度的正切值 Math.tan(Math.PI/3)); System.out.println(1的反正切值 Math.atan(1)); System.out.println(π/2的角度值 Math.toDegrees(Math.PI/2)); System.out.println(Math.PI); }
}
以上实例编译运行结果如下
90 度的正弦值1.0
0度的余弦值1.0
60度的正切值1.7320508075688767
1的反正切值 0.7853981633974483
π/2的角度值90.0
3.141592653589793floor,round 和 ceil 实例 public class Main { public static void main(String[] args) { double[] nums { 1.4, 1.5, 1.6, -1.4, -1.5, -1.6 }; for (double num : nums) { test(num); } } private static void test(double num) { System.out.println(Math.floor( num ) Math.floor(num)); System.out.println(Math.round( num ) Math.round(num)); System.out.println(Math.ceil( num ) Math.ceil(num)); }
}
以上实例执行输出结果为
Math.floor(1.4)1.0
Math.round(1.4)1
Math.ceil(1.4)2.0
Math.floor(1.5)1.0
Math.round(1.5)2
Math.ceil(1.5)2.0
Math.floor(1.6)1.0
Math.round(1.6)2
Math.ceil(1.6)2.0
Math.floor(-1.4)-2.0
Math.round(-1.4)-1
Math.ceil(-1.4)-1.0
Math.floor(-1.5)-2.0
Math.round(-1.5)-1
Math.ceil(-1.5)-1.0
Math.floor(-1.6)-2.0
Math.round(-1.6)-2
Math.ceil(-1.6)-1.0