NaN 和 Infinity

Posted 远方V3

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了NaN 和 Infinity相关的知识,希望对你有一定的参考价值。

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("\nNaN > 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();
        }
    }
}

 

以上是关于NaN 和 Infinity的主要内容,如果未能解决你的问题,请参考以下文章

NaN 和 Infinity

你如何让 VB6 用 +infinity、-infinity 和 NaN 初始化双精度数?

使用 Infinity 和 NaN 禁用异常

何时使用 NaN 或 +/-Infinity?

在 JavaScript 中,为啥零除以零返回 NaN,而任何其他除以零返回 Infinity?

JSON 省略了 Infinity 和 NaN; ECMAScript 中的 JSON 状态?