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

📅  最后修改于: 2021-05-29 22:08:46             🧑  作者: Mango

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

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

例子:

Input  : d = 10 / 0.0 
Output : True

Input  : d =  7.997e307 + 9.985e307; 
Output : True

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

// C# program to illustrate the
// Double.IsPositiveInfinity() Method
using System;
  
class GFG {
  
    // Main method
    static public void Main()
    {
  
        // Dividing a Positive number by zero
        // results in positive infinity.
  
        // Dividing a number directly by 0
        // produces an error
        // So 0 is stored in a variable first
  
        double zero = 0.0;
        double value = 10.0;
        double result = value / zero;
  
        // Printing result
        Console.WriteLine(result);
  
        // Check result using IsPositiveInfinity() Method
        Console.WriteLine(Double.IsPositiveInfinity(result));
  
        // Floating point operation that exceeds
        // Double.MaxValue (i.e 1.7976931348623157E+308)
        // is Positive Infinity
  
        result = 7.997e307 + 9.985e307;
  
        // Printing result
        Console.WriteLine(result);
  
        // Check result using IsPositiveInfinity() Method
        Console.WriteLine(Double.IsPositiveInfinity(result));
    }
}

笔记:

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