using Fasterflect; using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication2 { class Program_NaNAndInfinity { static void Main(string[] args) { /* C#语言中,对于 int,long 和 decimal类型的数,任何数除以 0 所得的结果是无穷大,不在int,long 和 decimal 类型的范围之内,所以计算 6/0 之类的表达式会出错。 但是,double 和 float 类型实际上有一个可以表示无穷大的特殊值:5.0/0.0 = Infinity (无穷大),这个规则唯一的例外是0.0/0.0 = NaN (Not a Number)。 */ // 表示不是数字 (NaN) 的值 Console.WriteLine("NaN == NaN: {0}", Double.NaN == Double.NaN); Console.WriteLine("NaN != NaN: {0}", Double.NaN != Double.NaN); Console.WriteLine("NaN.Equals(NaN): {0}", Double.NaN.Equals(Double.NaN)); Console.WriteLine("! NaN.Equals(NaN): {0}", !Double.NaN.Equals(Double.NaN)); Console.WriteLine("IsNaN: {0}", Double.IsNaN(Double.NaN)); Console.WriteLine(" NaN > NaN: {0}", Double.NaN > Double.NaN); Console.WriteLine("NaN >= NaN: {0}", Double.NaN >= Double.NaN); Console.WriteLine("NaN < NaN: {0}", Double.NaN < Double.NaN); Console.WriteLine("NaN < 100.0: {0}", Double.NaN < 100.0); Console.WriteLine("NaN <= 100.0: {0}", Double.NaN <= 100.0); Console.WriteLine("NaN >= 100.0: {0}", Double.NaN > 100.0); Console.WriteLine("NaN.CompareTo(NaN): {0}", Double.NaN.CompareTo(Double.NaN)); Console.WriteLine("NaN.CompareTo(100.0): {0}", Double.NaN.CompareTo(100.0)); Console.WriteLine("(100.0).CompareTo(Double.NaN): {0}", (100.0).CompareTo(Double.NaN)); // double.PositiveInfinity, float.PositiveInfinity 此常数的值是正数被零除所得到的结果。当运算结果大于 MaxValue 时,返回此常数。 Console.WriteLine("5 / 2 = {0}", 5 / 2); // 2 Console.WriteLine("5.0 / 2.0 = {0}", 5.0 / 2.0); // 2.5 Console.WriteLine("5.0 / 2 = {0}", 5.0 / 2); // 2.5 Console.WriteLine("5 / 2.0 = {0}", 5 / 2.0); // 2.5 Console.WriteLine("5.0 / 0.0 = {0}", 5.0 / 0.0); // Infinity Console.WriteLine("5.0 / 0 = {0}", 5.0 / 0); // Infinity Console.WriteLine("0.0 / 0.0 = {0}", 0.0 / 0.0); // NaN Console.WriteLine("5 / 0.0 = {0}", 5 / 0.0); // Infinity Console.WriteLine("0.0 / 0 = {0}", 0.0 / 0); // NaN Console.WriteLine("double.MinValue < double.PositiveInfinity : {0}", double.MinValue <= double.PositiveInfinity); Console.WriteLine("double.MinValue > double.PositiveInfinity : {0}", double.MinValue >= double.PositiveInfinity); Console.WriteLine("double.MaxValue < double.PositiveInfinity : {0}", double.MaxValue <= double.PositiveInfinity); Console.WriteLine("double.MaxValue > double.PositiveInfinity : {0}", double.MaxValue >= double.PositiveInfinity); // double.NegativeInfinity, float.NegativeInfinity 此常数的值是负数被零除所得到的结果。当运算结果小于 MinValue 时,返回此常数。 Console.WriteLine("-5 / 2 = {0}", -5 / 2); // 2 Console.WriteLine("-5.0 / 2.0 = {0}", -5.0 / 2.0); // -2.5 Console.WriteLine("-5.0 / 2 = {0}", -5.0 / 2); // -2.5 Console.WriteLine("-5 / 2.0 = {0}", -5 / 2.0); // -2.5 Console.WriteLine("-5.0 / 0.0 = {0}", -5.0 / 0.0); // Infinity Console.WriteLine("-5.0 / 0 = {0}", -5.0 / 0); // Infinity Console.WriteLine("0.0 / 0.0 = {0}", 0.0 / 0.0); // NaN Console.WriteLine("-5 / 0.0 = {0}", -5 / 0.0); // Infinity Console.WriteLine("0.0 / 0 = {0}", 0.0 / 0); // NaN Console.WriteLine("double.MinValue < double.NegativeInfinity : {0}", double.MinValue <= double.NegativeInfinity); Console.WriteLine("double.MinValue > double.NegativeInfinity : {0}", double.MinValue >= double.NegativeInfinity); Console.WriteLine("double.MaxValue < double.NegativeInfinity : {0}", double.MaxValue <= double.NegativeInfinity); Console.WriteLine("double.MaxValue > double.NegativeInfinity : {0}", double.MaxValue >= double.NegativeInfinity); // 判断是否为无穷大 Console.WriteLine("float.IsInfinity(0.5F) = {0}", float.IsInfinity(0.5F)); // false Console.WriteLine("float.IsInfinity(float.NegativeInfinity) = {0}", float.IsInfinity(float.NegativeInfinity)); // true Console.WriteLine("float.IsInfinity(float.PositiveInfinity) = {0}", float.IsInfinity(float.PositiveInfinity)); // true // 判断是否为NaN 或者 Infinity只能使用 double.IsNaN()及float.IsInfinity() // 参考资料: https://msdn.microsoft.com/zh-cn/library/system.double.nan(v=vs.110).aspx Console.Read(); } } }