Skip to content
Related Articles

Related Articles

Improve Article

MathF.Cos() Method in C# with Examples

  • Last Updated : 04 Apr, 2019

MathF.Cos(Single) is an inbuilt MathF class method which returns the cosine of a given float value argument(specified angle).

Syntax: public static float Cos (float x);
Here, x is the angle(measured in radian) whose cosine is to be returned and the type of this parameter is System.Single.

Return Value: This method will return the cosine of x of type System.Single. If x is equal to NegativeInfinity, PositiveInfinity, or NaN, then this method returns NaN.

Below are the programs to illustrate the use of the above-discussed method:

Example 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));
    }
}
Output:
0.7071068
0.5877852
0.34202

Example 2: To show the working of MathF.Cos() method when the argument is NaN or Infinity.




// 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);
    }
}
Output:
NaN
NaN
NaN



My Personal Notes arrow_drop_up
Recommended Articles
Page :