Generate identical hashcodes for approximately the same numbers

I am creating a C # 3.5 application that uses the AutoCAD API to read an AutoCAD 2D drawing, makes changes to the drawing using specific business logic, and then adjusts it in AutoCAD. Due to the nature of the logic, the shape of the drawing must be rebuilt - for example, a rectangle consists of 4 straight lines.

I create these forms using the start and end coordinates of each line from AutoCAD, but some of the coordinates do not exactly match. For example, one point can be 0.69912839 (on one axis), but a line starting at the same point can be 0.69990821. They are in mm, so the distance is a minute (0,00078 mm!)

I created my own class (call it MyPoint, similar to PointF) because I need to add extra logic to it. In this class, I created a method that takes two doubles and returns true or false depending on whether the two points are within 0.001 mm from each other. Then I redefined the Equals method, == and! = So that I can do (point1 == point2 or point1.Equals (point2)), which checks if all axes are within 0.001 mm from each other - if there are any, I classify it as the same point.

It is great and works brilliantly. Now I need to check the collection of these point classes to get rid of all duplicates, so I use the LINQ Distinct () method in my collection. However, this method uses GetHashcode (), not Equals (), to determine if the instances are equal. So, I exceeded GetHashcode (), which uses GetHashcode for a double class.

But the above example fails because it is obvious that they have different values ​​and therefore generate different hash codes. Is there a way so that two numbers that are within 0.001 of each other can generate the same hash code? (Note that the numbers do not know each other, since GetHashcode is called separately in different instances of the class.) I have tried many ways that work for some examples, but not for others.

- 3dp ( 10 ^ 3, ) - - (699 == 699.) 0.69990821 0.70000120 (699!= 700.) , (0.700 == 0.700), (0.699!= 0.700.) 3dp, , , 12.9809 12.9818 (12980!= 12982.)

, Equals, ==,!= GetHashcode MyPoint.IsEqualTo() MyPointCollection.Distinct()?

+3
7

Distinct. System.Collections.IComparer( ) , ​​ . , , , .

+2

-. : 2 . var a = point1.GetHashCode(); var b = point2.GetHashCode();

a!= b, 1 2. ..

, - , - . , - point1 point2 .

, :

public override int GetHashCode()
{
    return 0;
}

.

+3

, Equals(), ==, != GetHashCode()

, , . .

, == , P1 0,001 P2, P2 0,001 P3, P1 0,002 P3, P1 == P2, P2 == P3 P1 == P3, , . , , .

, .

== :

if(P1 == P2 && P2 == P3 && P1 != P3)
{
    // Code here gets executed
}
+3

, ( 0), LinQ equals. , , , .

, (BSP) ().

+1

Equals, ==,!= GetHashcode MyPoint.IsEqualTo() MyPointCollection.Distinct()?

.

- . -, . (x + 0,001, y), (x, y-0,001) .. , , . ( , .)

: , . "" , . x1 x2 , abs(x1 - x2) < .001. , x {x_0,..., x_n}. X_i -, hash(x) = h(floor(1000*x)) h(), . , x , hash(x-.001), hash(x) hash(x+.001), , x x_i . x_i .

9 ( ); 3-, 27.

+1

, .

, GetHashCode , .

a b, , a.GetHashCode() == b.GetHashCode().

: a < b. a b 0,001. a0 = a, a1 = a0 + 0.0005, a2 = a1 + 0.0005, .., b.

a.GetHashCode() == a1.GetHashCode() == a2.GetHashCode() == ... == b.GetHashCode().

+1

, , . "" .

int tolerance = 3;
double[] original = new double[] {
0.69912839,
0.69990821,

0.69990821,
0.70000120,

12.980984087,
12.981808908
};
double[] modified = new double[original.Length];

for (int i = 0; i < original.Length; i++)
{
modified[i] = original[i];

/* Begin number adjustment logic */
modified[i] *= Math.Pow(10, tolerance);
modified[i] = Math.Truncate(modified[i]);

if (modified[i] % 2 != 0)
{
modified[i]++;
}
/* End number adjustment logic */

Console.WriteLine(modified[i]);

if (i % 2 != 0)
{
Console.WriteLine(string.Empty);
}
}

- " 3dp, " . - ( ):

/* Begin number adjustment logic */
modified[i] *= Math.Pow(10, tolerance);
modified[i] = Math.Truncate(modified[i]);
/* End number adjustment logic */

:

/* Begin number adjustment logic */
modified[i] = Math.Round(modified[i], tolerance);
/* End number adjustment logic */
0
source

Source: https://habr.com/ru/post/1732498/


All Articles