C# dotnet 在内存中的 double 的 NAN 和正负无穷二进制是如何存

2022-08-12 16:34:24 浏览数 (2)

我就好奇无穷大和 NAN 在 C# 的二进制是如何表示的

揭开谜底

NAN: 00,00,00,00,00,00,F8,FF

正无穷 PositiveInfinity : 00,00,00,00,00,00,F0,7F

负无穷 NegativeInfinity : 00,00,00,00,00,00,F0,FF

测试方法如下

代码语言:javascript复制
        static void Main(string[] args)
        {
            MemoryStream stream = new MemoryStream();
            BinaryWriter writer = new BinaryWriter(stream);

            double db = double.NaN;
            writer.Write(db);

            Console.WriteLine(string.Join(",", stream.ToArray().Select(b => b.ToString("X2"))));

            stream.Seek(0, SeekOrigin.Begin);
            db = double.PositiveInfinity;
            writer.Write(db);
            Console.WriteLine(string.Join(",", stream.ToArray().Select(b => b.ToString("X2"))));

            stream.Seek(0, SeekOrigin.Begin);
            db = double.NegativeInfinity;
            writer.Write(db);
            Console.WriteLine(string.Join(",", stream.ToArray().Select(b => b.ToString("X2"))));
        }

这个值不是乱存的,有标准,请看 IEEE 754 - Wikipedia

是不是还有更简单的方法拿到某个变量的内存里面的值存放方法?试试下面代码

代码语言:javascript复制
double d = double.NaN;
Span<byte> sp = new Span<byte>(&d, sizeof(double));

试试看下 sp 的值

感谢 ( ̄▽ ̄) 小伙伴提供的方法

0 人点赞