📜  C#中的Single.IsFinite()方法及示例

📅  最后修改于: 2021-05-30 00:10:34             🧑  作者: Mango

Single.IsFinite()方法用于检查float值是否超出范围。

下面的程序说明了Single.IsFinite()方法的用法:

范例1:

// C# program to demonstrate the
// Single.IsFinite() Method
using System;
using System.Globalization;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        // Declaring and initializing value1
        float value1 = 1654.268416f;
  
        // using IsFinite() method
        bool value = Single.IsFinite(value1);
  
         // Displaying the result
        if (value)
            Console.WriteLine("{0} is finite", value1);
        else
            Console.WriteLine("{0} is not finite", value1);
    }
}
输出:
1654.268 is finite

范例2:

// C# program to demonstrate the
// Single.IsFinite() Method
using System;
using System.Globalization;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        // Declaring and initializing value1
        float value1 = (float)Math.Pow(2, 10000000);
  
        // using IsFinite() method
        bool value = Single.IsFinite(value1);
  
         // Displaying the result
        if (value)
            Console.WriteLine("{0} is finite", value1);
        else
            Console.WriteLine("{0} is not finite", value1);
    }
}
输出:
Infinity is not finite

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.single.isfinite?view=netstandard-2.1