java.lang.Math类包含用于执行基本数字运算的方法,例如三角函数,对数等。
以下是一些方法。
序号 | 方法与说明 |
---|---|
1 | static double abs(double a) 此方法返回double值的绝对值。 |
2 | static float abs(float a) 此方法返回一个float值的绝对值。 |
3 | static int abs(int a) 此方法返回int值的绝对值。 |
4 | static long abs(long a) 此方法返回long值的绝对值。 |
5 | static double acos(double a) 此方法返回一个值的反余弦值;返回的角度在0.0到pi之间。 |
6 | static double asin(double a) 此方法返回一个值的反正弦值;返回的角度在-pi / 2至pi / 2的范围内。 |
让我们看一个acos()
方法的例子。
public class Demo { public static void main(String args[]) { double val = Math.PI / 2; val = Math.toRadians(val); System.out.println("Math.acos(" + val + ") = " + Math.acos(val)); } }
输出结果
Math.acos(0.027415567780803774) = 1.5433773235341761
让我们看一个asin()
方法的例子。
public class Demo { public static void main(String args[]) { double val = Math.PI / 2; val = Math.toRadians(val); System.out.println("Math.asin(" + val + ") = " + Math.asin(val)); } }
输出结果
Math.asin(0.027415567780803774) = 0.02741900326072046
让我们看一个log()
方法的例子。
public class Demo { public static void main(String args[]) { double val1 = 39564.9; double val2 = 1; System.out.println("Math.log(" + val1 + ") = " + Math.log(val1)); System.out.println("Math.log(" + val2 + ") = " + Math.log(val2)); } }
输出结果
Math.log(39564.9) = 10.585697640553684 Math.log(1.0) = 0.0