Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 543215
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:31:41+00:00 2026-05-13T10:31:41+00:00

This question is similar to the one here . We all know what PointF

  • 0

This question is similar to the one here.

We all know what PointF is, don’t we? This is the data structure:

public struct PointF
{
  public float X;
  public float Y;
}

How to implement IEqualityComparer<PointF> with tolerance? Let’s say my Equals code is like this

public const float Epsilon = 0.01; //say
public bool Equals(PointF pt1, PointF pt2)
{
   return Math.Abs(pt1.X-pt2.X)<Epsilon && Math.Abs(pt1.Y-pt2.Y)<Epsilon;
}

Question: How to implement the correct GetHashCode so that for a dictionary of PointF, I will access the element correctly?

I crack my head a few days but still can’t find a satisfactory solution.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T10:31:41+00:00Added an answer on May 13, 2026 at 10:31 am

    Instead of defining the tolerance by the distance, you could place the points in a grid.
    If two points are in the same cell, they’re considered equal and have the same hash code.

    public bool Equals(PointF pt1, PointF pt2)
    {
       return GetCell(pt1.X) == GetCell(pt2.X)
           && GetCell(pt1.Y) == GetCell(pt2.Y);
    }
    
    public int GetHashCode(PointF pt)
    {
       return GetCell(pt.X) ^ GetCell(pt.Y);
    }
    
    private static int GetCell(float f)
    {
        return (int)(f / 10); // cell size is 10 pixels
    }
    

    Thesis: There is no implementation of Equals and GetHashCode that meets your requirements.

    Proof: Consider the following three points, A, B, and C:

    Illustration

    As per your requirements,

    Equals(A, B) == true              // (i)
    Equals(B, C) == true              // (ii)
    Equals(A, C) == false             // (iii)
    GetHashCode(A) == GetHashCode(B)  // (iv)
    GetHashCode(B) == GetHashCode(C)  // (v)
    GetHashCode(A) != GetHashCode(C)  // (vi)
    

    But from (iv) and (v) follows

    GetHashCode(A) == GetHashCode(C)
    

    and thereby

    Equals(A, C) == true
    

    which contradicts (iii) and (vi).

    Since Equals and GetHashCode cannot return different values for the same arguments, there is no implementation that meets your requirements.
    q.e.d.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 272k
  • Answers 272k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer SQL Server can't do this (at least as of 2005).… May 13, 2026 at 1:58 pm
  • Editorial Team
    Editorial Team added an answer If you don't want any fancy animation effects with sliding… May 13, 2026 at 1:58 pm
  • Editorial Team
    Editorial Team added an answer You could use a regular expression. I modified this example… May 13, 2026 at 1:58 pm

Related Questions

I am attempting to allow my web designers to use the metadata we have
I have a question that is similar, but not identical, to the one answered
Is this efficient? How could it be improved? I was trying to get data
This is admittedly similar to (but not a duplicate of) Comparison of full text
I found a few questions similar to this one here on SO, but none

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.