Open In App

MathF.Sqrt() Method in C# with Examples

Last Updated : 04 Apr, 2019
Comments
Improve
Suggest changes
Like Article
Like
Report
In C#, MathF.Sqrt(Single) is a MathF class(present in system namespace) method which is used to calculate the square root of the specified Single or float data type value.
Syntax: public static float Sqrt (float x); Here, x is the number whose square root is to be calculated and type of this parameter is System.Single.
Return Type: This method returns the square root of x. If x is equal to NaN, NegativeInfinity, or PositiveInfinity, that value is returned. The return type of this method is System.Single. Below are the examples to illustrate the use of the above-discussed method: Example 1: csharp
// C# program to demonstrate the
// MathF.Sqrt(Single) Method
using System;

class GFG {

    // Main Method
    public static void Main()
    {

        // taking positive value
        float x = 78521F;

        // taking negative value
        float x1 = -7824F;

        // Input positive value
        // Output will be square
        // root of x
        Console.WriteLine(MathF.Sqrt(x));

        // Input negative value,
        // Output will be NaN
        Console.Write(MathF.Sqrt(x1));
    }
}
Output:
280.216
NaN
Example 2: csharp
// C# program to demonstrate the MathF.Sqrt()
// method when the argument is positive
// or negative Zero
using System;

class GFG {

    // Main Method
    public static void Main()
    {

        // taking positive zero 
        float x = 0f;
        Console.WriteLine(MathF.Sqrt(x));

        // taking negative zero
        float y = -0f;
        Console.Write(MathF.Sqrt(y));
    }
}
Output:
0
0

Next Article

Similar Reads