相关疑难解决方法(0)

Hashcode实现双精度

我之前已经问了一个关于这个课程的问题,但这里又是一个问题.

我创建了一个Complex类:

 public class Complex
 {
        public double Real { get; set; }
        public double Imaginary { get; set; }
 }
Run Code Online (Sandbox Code Playgroud)

我正在实现EqualsHashcode函数,而Equal函数考虑了一定的精度.我使用以下逻辑:

    public override bool Equals(object obj)
    {
        //Some default null checkint etc here, the next code is all that matters.
        return Math.Abs(complex.Imaginary - Imaginary) <= 0.00001 &&
            Math.Abs(complex.Real - Real)  <= 0.00001;
    }
Run Code Online (Sandbox Code Playgroud)

这是有效的,当虚构和真实部分彼此非常接近时,它说它们是相同的.

现在我正在尝试实现HashCode函数,我已经使用了一些John skeet在这里使用的示例,目前我有以下内容.

    public override int GetHashCode()
    {
        var hash = 17;
        hash = hash*23 + Real.GetHashCode();
        hash = hash*23 + Imaginary.GetHashCode(); …
Run Code Online (Sandbox Code Playgroud)

c# floating-point precision hashcode

5
推荐指数
1
解决办法
2348
查看次数

标签 统计

c# ×1

floating-point ×1

hashcode ×1

precision ×1