Single.IsInfinity() 方法在 C# 中的用法示例


C# 中的 Single.IsInfinity() 方法用于返回一个值,指示指定的数字计算得到的是负无穷还是正无穷。

语法

语法如下所示 −

public static bool IsInfinity (float val);

此处,val 是一个单精度浮点数。

示例

现在让我们看一个示例 −

 在线演示

using System;
public class Demo {
   public static void Main(){
      float f1 = 19.3f;
      float f2 = Single.MaxValue;
      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("
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));    } }

输出

将产生以下输出 −

Value1 = 19.3
Hashcode for Value1 = 1100637798
TypeCode for Value1 = Single
Is Value1 value is positive or negative infinity? = False
Value2 = 3.402823E+38
Hashcode for Value2 = 2139095039
TypeCode for Value2 = Single
Is Value2 value is positive or negative infinity? = False

示例

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

 在线演示

using System;
public class Demo {
   public static void Main(){
      float f1 = 5.0f/0.0f;
      float f2 = Single.MinValue;
      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("
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));    } }

输出

将产生以下输出 −

Value1 = ∞
Hashcode for Value1 = 2139095040
TypeCode for Value1 = Single
Is Value1 value is positive or negative infinity? = True
Value2 = -3.402823E+38
Hashcode for Value2 = -8388609
TypeCode for Value2 = Single
Is Value2 value is positive or negative infinity? = False

更新于: 03-Dec-2019

88 次浏览

提升你的 职业生涯

完成课程,获得证书

立即开始
广告