如果是System.Double不是双? [英] When is a System.Double not a double?

查看:135
本文介绍了如果是System.Double不是双?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看到如何 double.Nan == double.NaN 始终在C#假后,我开始好奇如何平等是引擎盖下实现的。所以我用ReSharper的反编译的双重结构,这里是我发现了什么:

After seeing how double.Nan == double.NaN is always false in C#, I became curious how the equality was implemented under the hood. So I used Resharper to decompile the Double struct, and here is what I found:

public struct Double : IComparable, IFormattable, IConvertible, IComparable<double>, IEquatable<double>
{
    // stuff removed...

    public const double NaN = double.NaN;

    // more stuff removed...
}



这似乎预示着这个结构双击声明是在这个特殊的小写形式定义的常量双击,虽然我一直认为,这两个是完全的代名词。更重要的是,如果我去的小写双落实,ReSharper的简单滚动我在文件顶部的声明。同样,跃居实施小写的 NaN的的只是带我到常量声明早些时候在就行了!

This seems to indicate the the struct Double declares a constant that is defined in terms of this special lower case double, though I'd always thought that the two were completely synonymous. What's more, if I Go To Implementation on the lowercase double, Resharper simply scrolls me to the declaration at the top of the file. Similarly, jumping to implementation of the lowercase's NaN just takes me to the constant declaration earlier in the line!

所以,我试图理解这种看似递归定义。这只是反编译的假象?也许在ReSharper的限制?还是其实这小写双不同的野兽完全 - 代表在从CLR / CTS较低水平的东西吗?

So I'm trying to understand this seemingly recursive definition. Is this just an artefact of the decompiler? Perhaps a limitation in Resharper? Or is this lowercase double actually a different beast altogether - representing something at a lower level from the CLR/CTS?

哪里 NaN的真的是从哪里来的?

Where does NaN really come from?

推荐答案

目前为止的最佳来源,你可以得到.NET程序集是用于构建它们实际的源代码。击败的准确性任何反编译,评论可以说是相当有用的。下载参考源

By far the best source you can get for .NET assemblies is the actual source code that was used to build them. Beats any decompiler for accuracy, the comments can be quite useful as well. Download the Reference Source.

您会随后也看到 Double.NaN 中IL没有被定义为马克认为,它实际上在C#源代码文件。在网/ CLR / BCL /系统/ double.cs 源代码文件显示的真正的声明:

You'll then also see that Double.NaN isn't defined in IL as Marc assumed, it's actually in a C# source code file. The net/clr/bcl/system/double.cs source code file shows the real declaration:

  public const double NaN = (double)0.0 / (double)0.0;



这需要C#编译器在编译时评估常量表达式的优势。或者把它的舌头在脸颊,NaN被由C ++编译器定义,因为这是被用来编写C#编译器的语言;)

Which takes advantage of the C# compiler evaluating constant expressions at compile time. Or to put it tongue-in-cheek, NaN is defined by the C++ compiler since that's the language that was used to write the C# compiler ;)

这篇关于如果是System.Double不是双?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆