gpt4 book ai didi

c# - 何时调用 Double 的 == 运算符?

转载 作者:IT王子 更新时间:2023-10-29 04:31:24 26 4
gpt4 key购买 nike

这一切都始于有人向我提出的一个棘手问题..(在书中提到 - 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)
  • 对于 double ,== 运算符调用转换为 ceq IL 操作码
  • 对于字符串,它转换为 System.String::op_Equality(string, string)。

果然documentation for ceq指定它是 float 和 NaN 的特例。这解释了观察结果。

问题:

  • 为什么在 Double 上定义 op_Equality? (并且实现不考虑 NaN 特定行为)
  • 什么时候调用?

最佳答案

Reflector的错误解释

您从 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 的比较语义

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com