Single.IsNaN() 方法在 C# 中的使用案例


C# 中的 Single.IsNan() 方法用于返回指示指定值是否为非数字 (NaN) 的值。

语法

语法如下 −

public static bool IsNaN (float f);

上述参数 val 是单精度浮点数。

示例

我们现在来看一个示例 −

 在线演示

using System;
public class Demo {
   public static void Main() {
      float f1 = 5.0f/0.0f;
      float f2 = 45.5f;
      Console.WriteLine("Value1 = "+f1);
      Console.WriteLine("Hashcode for Value1 = "+f1.GetHashCode());
      Console.WriteLine("TypeCode for Value1 = "+f1.GetTypeCode());
      Console.WriteLine("Is Value1 value is positive or negative infinity? = "+Single.IsInfinity(f1));
      Console.WriteLine("Is Value1 NaN? = "+Single.IsNaN(f1));
      Console.WriteLine("
Value2 = "+f2);       Console.WriteLine("Hashcode for Value2 = "+f2.GetHashCode());       Console.WriteLine("TypeCode for Value2 = "+f2.GetTypeCode());       Console.WriteLine("Is Value2 value is positive or negative infinity? = "+Single.IsInfinity(f2));       Console.WriteLine("Is Value2 NaN? = "+Single.IsNaN(f2));    } }

输出

这将产生以下输出 −

Value1 = ∞
Hashcode for Value1 = 2139095040
TypeCode for Value1 = Single
Is Value1 value is positive or negative infinity? = True
Is Value1 NaN? = False

Value2 = 45.5
Hashcode for Value2 = 1110835200
TypeCode for Value2 = Single
Is Value2 value is positive or negative infinity? = False
Is Value2 NaN? = False

示例

我们现在来看另一个示例 −

 在线演示

using System;
public class Demo {
   public static void Main() {
      float f1 = 5.0f/0.0f;
      float f2 = 0.0f / 0.0f;
      Console.WriteLine("Value1 = "+f1);
      Console.WriteLine("Hashcode for Value1 = "+f1.GetHashCode());
      Console.WriteLine("TypeCode for Value1 = "+f1.GetTypeCode());
      Console.WriteLine("Is Value1 value is positive or negative infinity? = "+Single.IsInfinity(f1));
      Console.WriteLine("Is Value1 NaN? = "+Single.IsNaN(f1));
      Console.WriteLine("
Value2 = "+f2);       Console.WriteLine("Hashcode for Value2 = "+f2.GetHashCode());       Console.WriteLine("TypeCode for Value2 = "+f2.GetTypeCode());       Console.WriteLine("Is Value2 value is positive or negative infinity? = "+Single.IsInfinity(f2));       Console.WriteLine("Is Value2 NaN? = "+Single.IsNaN(f2));    } }

输出

这将产生以下输出 −

Value1 = ∞
Hashcode for Value1 = 2139095040
TypeCode for Value1 = Single
Is Value1 value is positive or negative infinity? = True
Is Value1 NaN? = False

Value2 = NaN
Hashcode for Value2 = -4194304
TypeCode for Value2 = Single
Is Value2 value is positive or negative infinity? = False
Is Value2 NaN? = True

更新于: 04-Dec-2019

107 次浏览

开启您的 职业

通过完成课程取得认证

开始
广告