📜  C#中的Single.IsNegativeInfinity()方法与示例

📅  最后修改于: 2021-05-29 13:21:37             🧑  作者: Mango

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

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

示例:演示Single.IsNegativeInfinity(Single)方法

// C# program to illustrate the
// Single.IsNegativeInfinity(Single)
// Method
using System;
  
class GFG {
  
    // Main method
    static public void Main()
    {
  
        // Dividing a negative number by zero
        // results in Negative infinity.
  
        // Dividing a number directly by 0
        // produces an error
        // So 0 is stored in a variable first
  
        float zero = 0.0f;
        float value = -5f;
        float result = value / zero;
  
        // Printing result
        Console.WriteLine(result);
  
        // Check result using IsNegativeInfinity() Method
        Console.WriteLine(Single.IsNegativeInfinity(result));
  
        // Result of floating point operation
        // that is less than Single.MinValue
        // is Negative Infinity
  
        result = Single.MinValue * 7.9f;
  
        // Printing result
        Console.WriteLine(result);
  
        // Check result using IsNegativeInfinity() Method
        Console.WriteLine(Single.IsNegativeInfinity(result));
    }
}
输出:
-Infinity
True
-Infinity
True

笔记:

  • 任何小于Single.MinValue的浮点运算的结果都被视为负无穷大。
  • 浮点运算返回PositiveInfinityNegativeInfinity以指示溢出条件。

参考:

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