📜  C#中的Double.IsNegativeInfinity()方法

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

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

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

例子:

Input  : d = -5.0 / 0.0 
Output : True

Input  : d = -1.5935e250 * 7.948e110
Output : True

代码:演示Double.IsNegativeInfinity(Double)方法

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

笔记:

  • 小于Double.MinValue(即-1.7976931348623157E + 308)的任何浮点运算的结果都被视为负无穷大。
  • 浮点运算返回Infinity (正无穷大)或-Infinity (负无穷大)以指示溢出情况。