📜  C#| Math.Cos()方法

📅  最后修改于: 2021-05-29 15:32:15             🧑  作者: Mango

Math.Cos()是内置的Math类方法,它返回给定双值参数(指定角度)的余弦值。

句法:

public static double Cos(double num)

范围:

返回值:返回System.Double类型的num的正弦。如果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或infinity时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