Java - Math cos(double) 方法



描述

Java Math cos(double a) 方法返回一個角的三角餘弦。如果引數是 NaN 或無窮大,則結果為 NaN。計算結果必須在精確結果的 1 ulp 之內。結果必須是半單調的。

宣告

以下是 java.lang.Math.cos() 方法的宣告

public static double cos(double a)

引數

a − 要返回其三角餘弦的值。

返回值

此方法返回引數的三角餘弦。

異常

獲取角度的三角餘弦示例

以下示例演示了 Math cos() 方法的使用。

package com.tutorialspoint;

public class MathDemo {

   public static void main(String[] args) {

      // get a variable x which is equal to PI/2
      double x = Math.PI / 2;

      // convert x to radians
      x = Math.toRadians(x);

      // get the arc sine of x
      System.out.println("Math.cos(" + x + ")=" + Math.cos(x));
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Math.cos(0.027415567780803774)=0.9996242168594817

獲取 0° 角的三角餘弦示例

以下示例演示了 Math cos() 方法在 0° 角上的使用。

package com.tutorialspoint;

public class MathDemo {

   public static void main(String[] args) {

      // get a variable x which is equal to zero
      double x = 0.0d;

      // convert x to radians
      x = Math.toRadians(x);

      // get the arc sine of x
      System.out.println("Math.cos(" + x + ")=" + Math.cos(x));
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Math.cos(0.0)=1.0

獲取 45° 角的三角餘弦示例

以下示例演示了 Math cos() 方法在 45° 角上的使用。

package com.tutorialspoint;

public class MathDemo {

   public static void main(String[] args) {

      // get a variable x which is equal to zero
      double x = 45.0d;

      // convert x to radians
      x = Math.toRadians(x);

      // get the arc sine of x
      System.out.println("Math.cos(" + x + ")=" + Math.cos(x));
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Math.cos(0.7853981633974483)=0.7071067811865476
java_lang_math.htm
廣告