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

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

在C#中, MathF.Sqrt(Single)是MathF类(在系统名称空间中存在)方法,用于计算指定的Single或float数据类型值的平方根。

返回类型:此方法返回x的平方根。如果x等于NaN, NegativeInfinityPositiveInfinity ,则返回该值。此方法的返回类型为System.Single

下面的示例说明了上述方法的使用:
范例1:

// 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));
    }
}
输出:
280.216
NaN

范例2:

// 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));
    }
}
输出:
0
0