📜  C#中的MathF.Cos()方法与示例

📅  最后修改于: 2021-05-29 19:18:20             🧑  作者: Mango

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

返回值:该方法将返回System.Single类型的x的余弦值。如果x等于NegativeInfinity,PositiveInfinity或NaN ,则此方法返回NaN

下面是说明上述方法的用法的程序:

范例1:

// C# program to illustrate the
// MathF.Cos(Single) Method
using System;
  
class GFG{
  
    // Main Method
    public static void Main(String[] args)
    {
        float a = 45f;
  
        // converting value to radians
        float b = (a * (MathF.PI)) / 180;
  
        // using method and displaying result
        Console.WriteLine(MathF.Cos(b));
  
        a = 54f;
  
        // converting value to radians
        b = (a * (MathF.PI)) / 180;
  
        // using method and displaying result
        Console.WriteLine(MathF.Cos(b));
        a = 70f;
  
        // converting value to radians
        b = (a * (MathF.PI)) / 180;
  
        // using method and displaying result
        Console.WriteLine(MathF.Cos(b));
    }
}
输出:
0.7071068
0.5877852
0.34202

示例2:显示当参数为NaN或Infinity时MathF.Cos()方法的工作方式。

// C# program to illustrate the
// MathF.Cos(Single) method 
using System;
  
class GFG {
  
    // Main Method
    public static void Main(String[] args)
    {
  
         // taking infinity values
        float positiveInfinity = float.PositiveInfinity;
        float negativeInfinity = float.NegativeInfinity;
  
        // taking NaN 
        float nan = float.NaN;
  
        float result;
  
        // Here argument is negative infinity,
        // so the output will be NaN
        result = MathF.Cos(negativeInfinity);
        Console.WriteLine(result);
  
        // Here argument is positive infinity,
        // so the output will also be NaN
        result = MathF.Cos(positiveInfinity);
        Console.WriteLine(result);
  
        // Here argument is NaN, output will be NaN
        result = MathF.Cos(nan);
        Console.WriteLine(result);
    }
}
输出:
NaN
NaN
NaN