StrictMath类cos()方法cos()方法在java.lang包中可用。
cos()方法用于返回该方法中给定参数的角度的三角余弦值。在这里,cos代表一个角度的余弦。
cos()方法是一个静态方法,因此可以使用类名进行访问,如果尝试使用类对象访问该方法,则不会出现任何错误。
在此方法中,我们仅传递弧度类型的参数(即,首先,我们使用StrictMath类的toRadians()方法将给定参数转换为弧度,然后在cos()方法中传递相同的变量)。
语法:public static double cos(double d);
参数:double d –表示要找到其三角余弦值的double类型值。
返回值:
此方法的返回类型为double-它返回给定参数的三角余弦值。
注意:如果我们将NaN作为参数传递,则方法将返回相同的值(NaN)。
如果传递无穷大,则方法返回NaN。
示例//Java程序演示示例
//StrictMath类的cos(double d)方法的说明。
public class Cos {
public static void main(String[] args) {
//变量声明
double d1 = 7.0 / 0.0;
double d2 = -7.0 / 0.0;
double d3 = 60.0;
//显示d1,d2和d3的先前值
System.out.println("d1: " + d1);
System.out.println("d2: " + d2);
System.out.println("d3: " + d3);
//通过使用toRadians()方法将绝对值转换为弧度。
d1 = StrictMath.toRadians(d1);
d2 = StrictMath.toRadians(d2);
d3 = StrictMath.toRadians(d3);
//在这里,我们将得到(NaN),因为我们
//传递参数,其值为(infinity)
System.out.println("StrictMath.cos(d1): " + StrictMath.cos(d1));
//在这里,我们将得到(NaN),因为我们
//传递参数,其值为(-infinity)
System.out.println("StrictMath.cos(d2): " + StrictMath.cos(d2));
//找到d3的余弦值
System.out.println("StrictMath.cos(d3): " + StrictMath.cos(d3));
}
}
输出结果d1: Infinity
d2: -Infinity
d3: 60.0
StrictMath.cos(d1): NaN
StrictMath.cos(d2): NaN
StrictMath.cos(d3): 0.5000000000000001