📜  C#中的Single.IsNaN()方法及示例

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

在C#中, Single.IsNaN(Single)是Single结构方法。此方法用于检查指定的浮点值是否不是数字(NaN)。

返回类型:如果指定的值不是数字(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值是PositiveInfinityNegativeInfinity,则此方法返回false
  • 浮点运算将返回NaN,以表明运算结果未定义。

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.single.isnan?view=netstandard-2.1