Double.GetHashCode 算法或覆盖

发布于 2024-09-28 19:20:28 字数 1104 浏览 4 评论 0原文

我有一个托管代码和非托管代码都运行的应用程序项目,我需要使用相同的算法在两个系统中对双值进行哈希处理。所以我要么重写 System.Double.GetHashCode() 要么在 C++ 代码中使用它的算法。我找不到 double.gethashcode 算法并决定重写该函数。但我遇到了一个奇怪的错误。

无法隐式转换 double 类型 到系统.Double

这里是代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace System
{
  public struct Double
  {
    unsafe public override int GetHashCode()
    {
      fixed (Double* dd = &this)
      {
        int* xx = (int*)dd;
        return xx[0] ^ xx[1] ;
      }

    }
  }
}

namespace ConsoleApplication1
{
  class Program
  {
    static void Main(string[] args)
    {
      double dd = 123.3444; // line 1
      // Double dd = 123.3444; // line 2
      // Cannot implicitly convert type double to System.Double
      Console.WriteLine(dd.GetHashCode());

      Console.ReadLine();
    }
  }
}

如果我取消注释第 2 行,我会得到 Cannot implicitly conversion type double to System.Double 错误。如果我运行第 1 行,则不会发生错误,但覆盖的代码永远不会工作。

也许我正在尝试的是非常糟糕的事情。所以任何人都知道 double.gethashcode 算法,所以我可以编写等效的 C++ 代码来获取准确的 int 值?

i have an application project that both managed and unmanaged code runs and i need to use the same algorithm for hashing double values in both systems. so either i will override System.Double.GetHashCode() or use its algorithm in c++ code. i could not find the double.gethashcode algorithm and decided to override the function. but i got a strange error.

Cannot implicitly convert type double
to System.Double

here is the code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace System
{
  public struct Double
  {
    unsafe public override int GetHashCode()
    {
      fixed (Double* dd = &this)
      {
        int* xx = (int*)dd;
        return xx[0] ^ xx[1] ;
      }

    }
  }
}

namespace ConsoleApplication1
{
  class Program
  {
    static void Main(string[] args)
    {
      double dd = 123.3444; // line 1
      // Double dd = 123.3444; // line 2
      // Cannot implicitly convert type double to System.Double
      Console.WriteLine(dd.GetHashCode());

      Console.ReadLine();
    }
  }
}

if I uncomment line 2 i get Cannot implicitly convert type double to System.Double
error. if i run line 1 then no error occurs but the overriden code never works.

maybe it is very bad thing i am trying. so any one knows about the double.gethashcode algorithm, so i can write equivalent c++ code to get the exact int value?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

还不是爱你 2024-10-05 19:20:28

这是我所看到的 Double.GetHashCode()

//The hashcode for a double is the absolute value of the integer representation
//of that double.
// 
[System.Security.SecuritySafeCritical]  // auto-generated
public unsafe override int GetHashCode() { 
    double d = m_value; 
    if (d == 0) {
        // Ensure that 0 and -0 have the same hash code 
        return 0;
    }
    long value = *(long*)(&d);
    return unchecked((int)value) ^ ((int)(value >> 32)); 
}

This is what I see for Double.GetHashCode():

//The hashcode for a double is the absolute value of the integer representation
//of that double.
// 
[System.Security.SecuritySafeCritical]  // auto-generated
public unsafe override int GetHashCode() { 
    double d = m_value; 
    if (d == 0) {
        // Ensure that 0 and -0 have the same hash code 
        return 0;
    }
    long value = *(long*)(&d);
    return unchecked((int)value) ^ ((int)(value >> 32)); 
}
神妖 2024-10-05 19:20:28
public struct Double

这是第一个问题。您不能重新定义预定义类型(除非......)。

public struct Double

That is the first issue. You cannot redefine a predefined type (unless ...).

全部不再 2024-10-05 19:20:28

您的自定义双精度类型有两个问题:

  • 它不包含数据。
  • 它不会与 double 相互转换。

第一个问题只需在结构中添加一个 double 类型的私有变量即可解决。

第二个问题是通过使用隐式转换器来解决的。

public struct CustomDouble {

  private double _value;

  public override int GetHashCode() {
    byte[] data = BitConverter.GetBytes(_value);
    int x = BitConverter.ToInt32(data, 0);
    int y = BitConverter.ToInt32(data, 4);
    return x ^ y;
  }

  public static implicit operator double(CustomDouble d) {
    return d._value;
  }

  public static implicit operator CustomDouble(double d) {
    return new CustomDouble() { _value = d };
  }

}

例子:

// Use the conversion from double to CustomDouble
CustomDouble d = 3.14;

// Use the CustomDouble.GetHashCode method:
Console.WriteLine(d.GetHashCode()); // 300063655

// Use the conversion from CustomDouble to double:
Console.WriteLine(d); // 3.14

There is two problems with your custom double type:

  • It contains no data.
  • It doesn't convert to and from double.

The first is solved by simply having a private variable of the type double in the structure.

The second is solved by using implicit converters.

public struct CustomDouble {

  private double _value;

  public override int GetHashCode() {
    byte[] data = BitConverter.GetBytes(_value);
    int x = BitConverter.ToInt32(data, 0);
    int y = BitConverter.ToInt32(data, 4);
    return x ^ y;
  }

  public static implicit operator double(CustomDouble d) {
    return d._value;
  }

  public static implicit operator CustomDouble(double d) {
    return new CustomDouble() { _value = d };
  }

}

Example:

// Use the conversion from double to CustomDouble
CustomDouble d = 3.14;

// Use the CustomDouble.GetHashCode method:
Console.WriteLine(d.GetHashCode()); // 300063655

// Use the conversion from CustomDouble to double:
Console.WriteLine(d); // 3.14
哑剧 2024-10-05 19:20:28

使用扩展方法,卢克。

Use extension methods, Luke.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文