當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


C# Math.Cos()用法及代碼示例


Math.Cos()是內置的Math類方法,該方法返回給定雙值參數(指定角度)的餘弦值。

用法:

public static double Cos(double num)

參數:


  • num:它是要返回餘弦的角度(以弧度為單位),並且此參數的類型為System.Double。

返回值:返回類型為System.Double的num的csoine。如果num等於NegativeInfinity,PositiveInfinity或NaN,則此方法返回NaN。

下麵的程序來說明Math.Cos()方法。

示例1:演示Math.Cos()方法的用法。

// C# program to demonstrate working 
// Math.Cos() method 
using System; 
   
class Geeks { 
   
    // Main Method 
    public static void Main(String []args) 
    { 
        double a = 70; 
           
        // converting value to radians 
        double b = (a * (Math.PI)) / 180; 
   
        // using method and displaying result 
        Console.WriteLine(Math.Cos(b)); 
        a = 50; 
           
        // converting value to radians 
        b = (a * (Math.PI)) / 180; 
   
        // using method and displaying result 
        Console.WriteLine(Math.Cos(b)); 
        a = 73; 
           
        // converting value to radians 
        b = (a * (Math.PI)) / 180; 
  
        // using method and displaying result 
        Console.WriteLine(Math.Cos(b)); 
        a = 77; 
           
        // converting value to radians 
        b = (a * (Math.PI)) / 180; 
   
        // using method and displaying result 
        Console.WriteLine(Math.Cos(b)); 
    } 
}
輸出:
0.342020143325669
0.642787609686539
0.292371704722737
0.224951054343865

示例2:當參數為NaN或無窮大時,顯示Math.Cos()方法的用法。

// C# program to demonstrate working 
// Math.Cos() method in infinity case 
using System; 
  
class Geeks { 
      
    // Main Method 
    public static void Main(String []args) 
    { 
  
        double positiveInfinity = Double.PositiveInfinity;      
        double negativeInfinity = Double.NegativeInfinity; 
                 
                 
        double nan = Double.NaN; 
        double result; 
  
        // Here argument is negative infinity, 
        // output will be NaN 
         result = Math.Cos(negativeInfinity); 
         Console.WriteLine(result); 
  
        // Here argument is positive infinity, 
        // output will also be NaN 
        result = Math.Cos(positiveInfinity); 
        Console.WriteLine(result); 
  
        // Here argument is NaN, output will be NaN 
        result = Math.Cos(nan); 
        Console.WriteLine(result); 
    } 
}
輸出:
NaN
NaN
NaN


相關用法


注:本文由純淨天空篩選整理自Kirti_Mangal大神的英文原創作品 C# | Math.Cos() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。