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

  • SEARCH
  • Home
  • 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 132079
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T06:12:22+00:00 2026-05-11T06:12:22+00:00

It is well known that comparing floats by == is usually a mistake. In

  • 0

It is well known that comparing floats by == is usually a mistake. In a 3D-vector class (with float components X, Y, Z) i wrote, two vectors are considered equal if their distance is considered zero.

public override bool Equals(object obj) {     if (obj == null) {         return false;     }      if (GetType () != obj.GetType ()) {         return false;     }      float d = DistSq ((Vec) obj);      return IsConsideredZero (d); }  public float DistSq(Vec p) {     Vec d = this - p;     return d.LengthSq (); }  public float LengthSq() {     return X * X + Y * Y + Z * Z; }  private const float VEC_COMPARE_EPSILON_ABS = 1E-05f; public static bool IsConsideredZero(float f) {     return Math.Abs (f) < VEC_COMPARE_EPSILON_ABS; } 

So far, everything worked fine. However, now i’d like to get a hashcode of the vector. I can see that something like hash = (int)X^(int)Y^(int)Z is bound to fail.

The best i could come up with was:

public override int GetHashCode() {     return 0; } 

This, of course, kind of sucks. Is there any way to get a reasonable hashcode? NaNs and other special values are possible, but unlikely, in case that is important.

  • 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. 2026-05-11T06:12:23+00:00Added an answer on May 11, 2026 at 6:12 am

    It’s impossible assuming you want to have the normal hashcode/equality properties:

    • If X = Y and Y = Z then X = Z (transitivity)
    • If X = Y then Y = X (commutivity)
    • X = X for all X (reflexivity)

    The first rule is the problem – because if each value is deemed ‘equal’ to the next greater representable number, you end up with all numbers being equal. For instance, suppose a number is deemed equal to another they’re within 0.1:

    0 equals 0.08 0.08 equals 0.16 0.16 equals 0.24

    => 0 equals 0.16 by the transitivity rule => 0 equals 0.24 by the transitivity rule

    (etc)

    If you ignore the transitivity rule, then you still (presumably) want ‘equal’ values to have equal hashcodes. This effectively enforces the transitivity rule – in the above example, 0 and 0.08 have to have equal hashcodes, as do 0 and 0.16. Therefore 0 and 0.16 have to have equal hashcodes, and so on. Therefore you can have no useful hashcode – it has to be a constant.

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

Sidebar

Related Questions

I am wondering if there are any well-known algorithms that I should be aware
As well-known, C++ has steeper learning curve than most of the mainstream languages, which
LAMP is a well-known acronym for the software/technology bundle/stack representing Linux , Apache ,
It is a well known problem to every web developer. As far as I
First, I understand the how of iteratees, well enough that I could probably write
I know that in the next version of ASP.NET we'll finally be able to
Well I know it's not evil just not as pretty in semantics as <strong>
I know Java well. Which caveats and resources will help me cross to the
Maybe I just don't know .NET well enough yet, but I have yet to
I know, I would have thought the answer was obviously no as well, but

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.