Java Math cos()方法及实例

Java Math cos()方法及实例

java.lang.Math.cos() 返回一个角度的三角余弦。如果参数是 NaN或无穷大 ,那么返回的结果是NaN。返回的值将在 [-1, 1] 范围内

语法 :

public static double cos(double angle)

参数。
该函数有一个强制性参数angle,单位是弧度。

返回:

该函数返回一个角度的三角余弦。

例1: 展示java.lang.Math.cos()方法的工作。

// Java program to demonstrate working
// of java.lang.Math.cos() method
import java.lang.Math;
  
class Gfg {
  
    // driver code
    public static void main(String args[])
    {
        double a = 30;
          
        // converting values to radians
        double b = Math.toRadians(a);
  
        System.out.println(Math.cos(b));
  
        a = 45;
          
        // converting values to radians
        b = Math.toRadians(a);
  
        System.out.println(Math.cos(b));
  
        a = 60;
          
        // converting values to radians
        b = Math.toRadians(a);
  
        System.out.println(Math.cos(b));
  
        a = 0;
          
        // converting values to radians
        b = Math.toRadians(a);
  
        System.out.println(Math.cos(b));
    }
}

输出:

0.8660254037844387
0.7071067811865476
0.5000000000000001
1.0

例2: 展示当参数为NAN或无穷大时,java.lang.Math.cos()方法的工作。

// Java program to demonstrate working
// of java.lang.Math.cos() method infinity case
import java.lang.Math; // importing java.lang package
  
public class GFG {
    public static void main(String[] args)
    {
  
        double positiveInfinity = 
               Double.POSITIVE_INFINITY;
        double negativeInfinity = 
               Double.NEGATIVE_INFINITY;
        double nan = Double.NaN;
        double result;
  
        // Here argument is negative infinity, 
        // output will be NaN
        result = Math.cos(negativeInfinity);
        System.out.println(result);
  
        // Here argument is positive infinity, 
        // output will also be NaN
        result = Math.cos(positiveInfinity);
        System.out.println(result);
  
        // Here argument is NaN, output will be NaN
        result = Math.cos(nan);
        System.out.println(result);
    }
}

输出:

NaN
NaN
NaN

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程