📜  C#中的Single.IsInfinity()方法(带示例)

📅  最后修改于: 2021-05-29 14:40:48             🧑  作者: Mango

在C#中, Single.IsInfinity(Single)是Single结构方法。此方法用于检查指定的浮点值的取值是否为正无穷大或负无穷大。在执行一些数学运算时,有可能获得正无穷大或负无穷大的结果。例如:如果任何正值除以零,则结果为正无穷大。

返回类型:如果指定值的计算结果为正无穷大或负无穷大,则此方法返回布尔值True ,否则返回False

例子:

// C# program to illustrate the
// Single.IsInfinity(Single) 
// Method
using System;
  
class GFG {
  
    // Main method
    static public void Main()
    {
  
        // Dividing a Positive number by zero
        // results in positive infinity.
        float zero = 0.0f;
        float value = 10.0f;
        float result = value / zero;
  
        // Printing result
        Console.WriteLine(result);
  
        // Check result using IsInfinity() Method
        Console.WriteLine(Single.IsInfinity(result));
  
        // Result of any operation that
        // exceeds Single.MaxValue
        // is Positive infintity
        result = Single.MaxValue * 9.25f;
  
        // Printing result
        Console.WriteLine(result);
  
        // Check result using IsInfinity() Method
        Console.WriteLine(Single.IsInfinity(result));
  
        // Result of any operation that
        // is less than Single.MinValue
        // is Negative infintity
        result = Single.MinValue * 9.565f;
  
        // Printing result
        Console.WriteLine(result);
  
        // Check result using IsInfinity() Method
        Console.WriteLine(Single.IsInfinity(result));
    }
}
输出:
Infinity
True
Infinity
True
-Infinity
True

注意:浮点操作返回PositiveInfinityNegativeInfinity表示发生溢出情况。

参考:

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