c# - 谁能用 C# 中的带符号 float 解释这种奇怪的行为?

标签 c# .net floating-point

这是带有注释的示例:

class Program
{
    // first version of structure
    public struct D1
    {
        public double d;
        public int f;
    }

    // during some changes in code then we got D2 from D1
    // Field f type became double while it was int before
    public struct D2 
    {
        public double d;
        public double f;
    }

    static void Main(string[] args)
    {
        // Scenario with the first version
        D1 a = new D1();
        D1 b = new D1();
        a.f = b.f = 1;
        a.d = 0.0;
        b.d = -0.0;
        bool r1 = a.Equals(b); // gives true, all is ok

        // The same scenario with the new one
        D2 c = new D2();
        D2 d = new D2();
        c.f = d.f = 1;
        c.d = 0.0;
        d.d = -0.0;
        bool r2 = c.Equals(d); // false! this is not the expected result        
    }
}

那么,您对此有何看法?

最佳答案

bug在System.ValueType的下面两行:(我踩到了引用源)

if (CanCompareBits(this)) 
    return FastEqualsCheck(thisObj, obj);

(两种方法都是[MethodImpl(MethodImplOptions.InternalCall)])

当所有字段都是 8 字节宽时,CanCompareBits 错误地返回 true,导致对两个不同但语义相同的值进行按位比较。

当至少一个字段不是 8 字节宽时,CanCompareBits 返回 false,代码继续使用反射循环遍历字段并为每个值调用 Equals ,它正确地将 -0.0 视为等于 0.0

这是来自 SSCLI 的 CanCompareBits 的源代码:

FCIMPL1(FC_BOOL_RET, ValueTypeHelper::CanCompareBits, Object* obj)
{
    WRAPPER_CONTRACT;
    STATIC_CONTRACT_SO_TOLERANT;

    _ASSERTE(obj != NULL);
    MethodTable* mt = obj->GetMethodTable();
    FC_RETURN_BOOL(!mt->ContainsPointers() && !mt->IsNotTightlyPacked());
}
FCIMPLEND

关于c# - 谁能用 C# 中的带符号 float 解释这种奇怪的行为?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2508945/

相关文章:

.net - Docker-compose 与 .NET Core 无法访问

C# 定时器和垃圾回收

c# - 如何从 youtube 获取我的 youtube 视频列表?

c - 访问连续内存位置时出现打印错误

c# - 通过程序逻辑添加控制台

c# - 无法通过 Visual Studio 2013 中的 EF6 连接到 MySQL

c# - 从 .txt 文件中读取特定值并将其存储在变量中

c# - 使用 C# 插入字段名称为 TA/TJ 的数据库

java - 为什么我的 Java 应用程序中的结果是 "0"?

c++ log函数使用浮点精度