Double.GetHashCode 算法或覆盖
我有一个托管代码和非托管代码都运行的应用程序项目,我需要使用相同的算法在两个系统中对双值进行哈希处理。所以我要么重写 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是我所看到的
Double.GetHashCode()
:This is what I see for
Double.GetHashCode()
:这是第一个问题。您不能重新定义预定义类型(除非......)。
That is the first issue. You cannot redefine a predefined type (unless ...).
您的自定义双精度类型有两个问题:
double
相互转换。第一个问题只需在结构中添加一个 double 类型的私有变量即可解决。
第二个问题是通过使用隐式转换器来解决的。
例子:
There is two problems with your custom double type:
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.
Example:
使用扩展方法,卢克。
Use extension methods, Luke.