當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


C# MathF.Sqrt()用法及代碼示例


在C#中,MathF.Sqrt(Single)是MathF類(在係統名稱空間中存在)方法,用於計算指定的Single或float數據類型值的平方根。

用法: 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.

返回類型:此方法返回x的平方根。如果x等於NaN,NegativeInfinity或PositiveInfinity,則返回該值。此方法的返回類型為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


相關用法


注:本文由純淨天空篩選整理自Kirti_Mangal大神的英文原創作品 MathF.Sqrt() Method in C# with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。