English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Java Math mathematical methods
Java Math cos()方法返回指定角度的三角余弦。
cos()方法的语法为:
Math.cos(double angle)
angle - 要返回其三角余弦的角度
Note:angle的值以弧度为单位。
返回指定角度的三角余弦
如果指定的角度为NaN或无穷大,则返回NaN
import java.lang.Math; class Main { public static void main(String[] args) { //创建度数变量 double a = 30; double b = 45; // 转换为弧度 a = Math.toRadians(a); b = Math.toRadians(b); //Print the cosine value System.out.println(Math.cos(a)); // 0.8660254037844387 System.out.println(Math.cos(b)); // 0.7071067811865476 } }
在上面的示例中,我们已导入java.lang.Math包。如果我们要使用Math类的方法,这一点很重要。注意表达式
Math.cos(a)
在这里,我们直接使用了类名来调用方法。这是因为cos()是静态方法。
Note:我们已经使用Math Radians()方法将所有值转换为弧度。 这是因为根据官方文档,cos()方法将角度作为弧度。
import java.lang.Math; class Main { public static void main(String[] args) { //Create variables. //The square root of a negative number. //The result is not a number (NaN) double a = Math.sqrt(-5); //Implement infinity with Double double infinity = Double.POSITIVE_INFINITY; //Print the cosine value System.out.println(Math.cos(a)); // NaN System.out.println(Math.cos(infinity)); // NaN } }
In this case, we created a variable named a.
Math.cos(a) -Returns NaN because the negative number (-5) is not a number
Double.POSITIVE_INFINITY is a field of the Double class. It is used to implement infinity in Java.
Note: We have usedJava Math sqrt()Method to calculate the square root of a number.