- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
这一切都始于有人向我提出的一个棘手问题..(在书中提到 - C# 简而言之)这是它的要点。
Double a = Double.NaN;
Console.WriteLine(a == a); // => false
Console.WriteLine(a.Equals(a)); // => true
上面的好像不太对。 a 应该始终 == 自身(引用相等)并且两者应该一致。
似乎 Double 重载了 == 运算符。经反射器确认如下:
[__DynamicallyInvokable]
public static bool operator ==(double left, double right)
{
return (left == right);
}
奇怪的是,它看起来是递归的,而且没有提到 NaN 的特定行为。那么为什么它会返回 false 呢?
所以我又加了一些代码来区分
var x = "abc";
var y = "xyz";
Console.WriteLine(x == y); // => false
现在我明白了
L_0001: ldc.r8 NaN
L_000a: stloc.0
L_000b: ldloc.0
L_000c: ldloc.0
L_000d: ceq
L_000f: call void [mscorlib]System.Console::WriteLine(bool)
L_0014: nop
L_0015: ldloca.s a
L_0017: ldloc.0
L_0018: call instance bool [mscorlib]System.Double::Equals(float64)
L_001d: call void [mscorlib]System.Console::WriteLine(bool)
L_0022: nop
L_0023: ldstr "abc"
L_0028: stloc.1
L_0029: ldstr "xyz"
L_002e: stloc.2
L_002f: ldloc.1
L_0030: ldloc.2
L_0031: call bool [mscorlib]System.String::op_Equality(string, string)
L_0036: call void [mscorlib]System.Console::WriteLine(bool)
ceq
IL 操作码果然documentation for ceq
指定它是 float 和 NaN 的特例。这解释了观察结果。
问题:
最佳答案
您从 Reflector 看到的反编译实际上是 Reflector 中的一个错误。 Reflector 需要能够反编译比较两个 double 的函数;在这些函数中,您会发现 ceq
直接发送到代码中。因此,Reflector 将 ceq
指令解释为两个 double 值之间的 == 以帮助反编译比较两个 double 值的函数。
默认情况下,值类型不带有 == 实现。 ( Don't user-defined structs inherit an overloaded == operator? ) 但是,所有内置标量类型都有一个显式重载运算符,编译器将其翻译 为适当的 CIL。重载还包含一个简单的基于 ceq
的比较,因此 == 运算符重载的基于动态/后期绑定(bind)/反射的调用不会失败。
For predefined value types, the equality operator (==) returns true if the values of its operands are equal, false otherwise. For reference types other than string, == returns true if its two operands refer to the same object. For the string type, == compares the values of the strings.
-- http://msdn.microsoft.com/en-us/library/53k8ybth.aspx
您所说的意味着 == 使用引用类型语义来比较 double
。但是,由于 double
是值类型,因此它使用值语义。这就是为什么 3 == 3
为真,即使它们是不同的堆栈对象。
您几乎可以将此编译器转换视为 LINQ 的 Queryable 对象如何包含扩展方法和其中的代码,但编译器将这些调用转换为表达式树,然后传递给 LINQ 提供程序。在这两种情况下,底层函数都不会真正被调用。
Double 的文档确实提到了 ceq
CIL 指令的工作原理:
If two Double.NaN values are tested for equality by calling the Equals method, the method returns true. However, if two NaN values are tested for equality by using the equality operator, the operator returns false. When you want to determine whether the value of a Double is not a number (NaN), an alternative is to call the IsNaN method.
-- http://msdn.microsoft.com/en-us/library/ya2zha7s.aspx
如果您查看反编译的 C# 编译器源代码,您会发现以下代码将双重比较直接转换为 ceq
:
private void EmitBinaryCondOperator(BoundBinaryOperator binOp, bool sense)
{
int num;
ConstantValue constantValue;
bool flag = sense;
BinaryOperatorKind kind = binOp.OperatorKind.OperatorWithLogical();
if (kind <= BinaryOperatorKind.GreaterThanOrEqual)
{
switch (kind)
{
...
case BinaryOperatorKind.Equal:
goto Label_0127;
...
}
}
...
Label_0127:
constantValue = binOp.Left.ConstantValue;
if (((constantValue != null) && constantValue.IsPrimitiveZeroOrNull) && !constantValue.IsFloating)
{
...
return;
}
constantValue = binOp.Right.ConstantValue;
if (((constantValue != null) && constantValue.IsPrimitiveZeroOrNull) && !constantValue.IsFloating)
{
...
return;
}
this.EmitBinaryCondOperatorHelper(ILOpCode.Ceq, binOp.Left, binOp.Right, sense);
return;
}
以上代码来自于Roslyn.Compilers.CSharp.CodeGen.CodeGenerator.EmitBinaryCondOperator(...)
,我添加了“...”是为了让代码更为此目的可读。
关于c# - 何时调用 Double 的 == 运算符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14958015/
Or 运算符 对两个表达式进行逻辑“或”运算。 result = expression1 Or expression2 参数 result 任意数值变量。 expression1 任意
Not 运算符 对表达式执行逻辑非运算。 result = Not expression 参数 result 任意数值变量。 expression 任意表达式。 说明 下表显示如何
Is 运算符 比较两个对象引用变量。 result = object1 Is object2 参数 result 任意数值变量。 object1 任意对象名。 object2 任意
\ 运算符 两个数相除并返回以整数形式表示的结果。 result = number1\number2 参数 result 任意数值变量。 number1 任意数值表达式。 numbe
And 运算符 对两个表达式进行逻辑“与”运算。 result = expression1 And expression2 参数 result 任意数值变量。 expression1
运算符(+) 计算两个数之和。 result = expression1 + expression2 参数 result 任意数值变量。 expression1 任意表达式。 exp
我对此感到困惑snippet : var n1 = 5-"4"; var n2 = 5+"4"; alert(n1); alert(n2); 我知道 n1 是 1。那是因为减号运算符会将字符串“4”转
我想我会得到 12,而不是 7。 w++,那么w就是4,也就是100,而w++, w 将是 8,1000;所以 w++|z++ 将是 100|1000 = 1100 将是 12。 我怎么了? int
Xor 运算符 对两个表达式进行逻辑“异或”运算。 result = expression1 Xor expression2 参数 result 任意数值变量。 expression1
Mod 运算符 两个数值相除并返回其余数。 result = number1 Mod number2 参数 result 任意数值变量。 number1 任意数值表达式。 numbe
Imp 运算符 对两个表达式进行逻辑蕴涵运算。 result = expression1 Imp expression2 参数 result 任意数值变量。 expression1 任
Eqv 运算符 执行两个表达式的逻辑等价运算。 result = expression1 Eqv expression2 参数 result 任意数值变量。 expression1 任
我有一个运算符重载的简单数学 vector 类。我想为我的运算符(operator)获取一些计时结果。我可以通过计时以下代码轻松计时我的 +=、-=、*= 和/=: Vector sum; for(s
我是用户定义比较运算符的新手。我正在读一本书,其中提到了以下示例: struct P { int x, y; bool operator、运算符<等),我们
在 SQL 的维基百科页面上,有一些关于 SQL 中 bool 逻辑的真值表。 [1] 维基百科页面似乎来源于 SQL:2003 标准。 等号运算符 (=) 的真值表与 SQL:2003 草案中的 I
我遇到了一个奇怪的 C++ 运算符。 http://www.terralib.org/html/v410/classoracle_1_1occi_1_1_number.html#a0f2780081f
我正在阅读关于 SO 和 answers 中的一个问题,它被提到为: If no unambiguous matching deallocation function can be found, pr
我偶然发现了这个解决方案,但我无法理解其中到底发生了什么。谁能解释一下! 据我了解,它试图通过计算一半的单元格然后将其加倍来计算 a*b 网格中的单元格数量。但是我无法理解递归调用。 请不要建议其他解
Go的基本类型 布尔类型bool 长度:1字节 取值:布尔类型的取值只能是true或者false,不能用数字来表示 整型 通用整型 int / uint(有符号 / 无符号,下面也类似) 长度:根据运
在本教程中,您将学习JavaScript中可用的不同运算符,以及在示例的帮助下如何使用它们。 什么是运算符? 在JavaScript中,运算符是一种特殊符号,用于对运算数(值和变量)执行操作。例如,
我是一名优秀的程序员,十分优秀!