在C#中, Single.IsNaN(Single)是Single结构方法。此方法用于检查指定的浮点值是否不是数字(NaN)。
Syntax: public static bool IsNaN (float f);
Parameter:
f: It is a single-precision floating-point number of type System.Single.
返回类型:如果指定的值不是数字(NaN),则此函数返回一个布尔值,即True ,否则返回False 。
例子:
// C# program to demonstrate
// Single.IsNaN(Single) method
using System;
class GFG {
// Main Method
public static void Main(String[] args)
{
// first example
float f1 = 1.0f / 0.0f;
bool res = Single.IsNaN(f1);
// printing the output
if (res)
Console.WriteLine(f1 + " is NaN");
else
Console.WriteLine(f1 + " is not NaN");
// second example
// it will give result
// NaN
float f2 = 0.0f / 0.0f;
bool res1 = Single.IsNaN(f2);
// printing the output
if (res1)
Console.WriteLine(f2 + " is NaN");
else
Console.WriteLine(f2 + " is not NaN");
}
}
输出:
Infinity is not NaN
NaN is NaN
笔记:
- 如果Single值是PositiveInfinity或NegativeInfinity,则此方法返回false 。
- 浮点运算将返回NaN,以表明运算结果未定义。
参考:
- https://docs.microsoft.com/zh-cn/dotnet/api/system.single.isnan?view=netstandard-2.1