为了获得Java中给定值的反余弦,我们使用java.lang.Math.acos()方法。该acos()方法接受需要计算角度的双精度值。返回的角度范围在0到pi之间。如果参数为NaN或大于1或小于-1,则结果为NaN。
声明-java.lang.Math.acos()方法的声明如下-public static double acos(double a)
在此,a是计算其反余弦的值。
让我们看一个程序来获取Java中给定值的反余弦值。
示例import java.lang.Math;
public class Example {
public static void main(String[] args) {
//得到两个双度数
double x = Math.sqrt(3)/2;
double y = 0.5;
//计算并显示这些双精度的反余弦
System.out.println("Arc cosine of " + x + " = " + Math.acos(x));
System.out.println("Arc cosine of " + y + " = " + Math.acos(y));
}
}
输出结果Arc cosine of 0.8660254037844386 = 0.5235987755982989
Arc cosine of 0.5 = 1.0471975511965979