反余弦函数是余弦函数的反函数。
acos()
方法的语法是
Math.acos(double num)
在这里,acos()
是一个静态方法。因此,我们通过类名 Math
来访问该方法。
acos() 参数
acos()
方法接受一个参数。
- num - 要返回其反余弦的数字。它应该始终小于 1。
acos() 返回值
- 返回指定数字的反余弦
- 如果指定数字为
NaN
或大于 1,则返回NaN
。
注意:返回值是一个介于 0.0 到 pi 之间的角度。
示例 1:Java Math acos()
import java.lang.Math;
class Main {
public static void main(String[] args) {
// create variable
double a = 0.5;
double b = 0.79;
double c = 0.0;
// print the arc cosine value
System.out.println(Math.acos(a)); // 1.0471975511965979
System.out.println(Math.acos(b)); // 0.6599873293874984
System.out.println(Math.acos(c)); // 1.5707963267948966
}
}
在上面的示例中,我们导入了 java.lang.Math
包。如果我们想使用 Math
类的方法,这一点很重要。请注意表达式,
Math.acos(a)
在这里,我们直接使用了类名来调用该方法。这是因为 acos()
是一个静态方法。
示例 2:Math acos() 返回 NaN
import java.lang.Math;
class Main {
public static void main(String[] args) {
// create variable
double a = 2;
// square root of negative number
// results in not a number (NaN)
double NaN = Math.sqrt(-5);
// print the arc cosine value
System.out.println(Math.acos(a)); // NaN
System.out.println(Math.acos(NaN)); // NaN
}
}
在这里,我们创建了两个名为 a 和 b 的变量。
- Math.acos(a) - 返回 NaN,因为 a 的值大于 1。
- Math.acos(b) - 返回 NaN,因为负数 (-5) 的平方根不是一个数字。
注意:我们使用了 Java Math.sqrt() 方法来计算数字的平方根。
另请阅读