在C#中, Single.IsPositiveInfinity(Single)是Single结构方法。此方法用于检查指定的浮点值是否为正无穷大。在某些浮点运算中,可以获得正无穷大的结果。例如:如果任何正值除以零,则结果为正无穷大。
Syntax: public static bool IsPositiveInfinity (float f);
Parameter:
f: It is a single-precision floating-point number of type System.Single.
返回类型:如果指定值的计算结果为正无穷大,则此函数返回布尔值True ,否则返回False 。
示例:演示Single.IsPositiveInfinity(Single)方法
// C# program to illustrate the
// Single.IsPositiveInfinity(Single)
// 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
float zero = 0.0f;
float value = 10.0f;
float result = value / zero;
// Printing result
Console.WriteLine(result);
// Check result using IsPositiveInfinity() Method
Console.WriteLine(Single.IsPositiveInfinity(result));
// Floating point operation that exceeds
// Single.MaxValue is Positive Infinity
result = (1.402823E+38f + ((float)9.985e307));
// Printing result
Console.WriteLine(result);
// Check result using IsPositiveInfinity() Method
Console.WriteLine(Single.IsPositiveInfinity(result));
}
}
输出:
Infinity
True
Infinity
True
注意:浮点操作返回PositiveInfinity以指示溢出条件。
参考:
- https://docs.microsoft.com/zh-cn/dotnet/api/system.single.ispositiveinfinity?view=netstandard-2.1