在C#中, Double.IsInfinity()是Double结构方法。此方法用于检查指定值的计算结果是正无穷大还是负无穷大。在执行一些数学运算时,有可能获得正无穷大或负无穷大的结果。例如:如果任何正值除以零,则结果为正无穷大。
Syntax: public static bool IsInfinity (double d);
Parameter:
d: It is a double-precision floating-point number of type System.Double
返回类型:如果指定值的计算结果为正无穷大或负无穷大,则此函数返回布尔值True ,否则返回False 。
例子:
Input : d = 10 / 0.0
Output : True
Input : d = 7.997e307 + 9.985e307
Output : True
笔记:
- 如果任何浮点运算的结果超过Double.MaxValue(即1.79769313486232E + 308),则视为正无穷大;如果结果小于Double.MinValue(即-1.79769313486232E + 308),则视为负无穷大。
- 浮点运算返回Infinity (正无穷大)或-Infinity (负无穷大)以指示溢出情况。
代码:演示Double.IsInfinity(Double)方法
// C# program to illustrate the
// Double.IsInfinity() Method
using System;
class GFG {
// Main method
static public void Main()
{
// Dividing a Positive number by zero
// results in positive infinity.
double zero = 0.0;
double value = 10.0;
double result = value / zero;
// Printing result
Console.WriteLine(result);
// Check result using IsInfinity() Method
Console.WriteLine(Double.IsInfinity(result));
// Result of any operation that
// exceeds Double.MaxValue
// (i.e 1.79769313486232E+308)
// is Positive infintity
result = Double.MaxValue + 9.985e307;
// Printing result
Console.WriteLine(result);
// Check result using IsInfinity() Method
Console.WriteLine(Double.IsInfinity(result));
// Result of any operation that
// is less than Double.MinValue
// (i.e -1.79769313486232E+308)
// is Negative infintity
result = Double.MinValue - 9.985e307;
// Printing result
Console.WriteLine(result);
// Check result using IsInfinity() Method
Console.WriteLine(Double.IsInfinity(result));
}
}
输出:
Infinity
True
Infinity
True
-Infinity
True