Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Dollhopf · May 02, 2018 at 12:26 PM · dictionaryhash

(-0f).GetHashCode() not equals 0f.GetHashCode(), how to fix this fast?

Following situation (code is reduced to make it clearer):

 public class Hex {
     public float x;
     public float y;
     public float z;
     
     public Hex(float newX = 0f, float newY = 0f) {
         x = newX;
         y = newY;
         Flatten();
     }
     public void Flatten() {
         z = -x - y;
     }
     public override int GetHashCode() {
             return x.GetHashCode() ^ y.GetHashCode() << 2 ^ z.GetHashCode() >> 2;
                 // Calculate hash based on Vector3 calculation
     }
 }
 Hex hex = new Hex(); // all variables equals 0f

So naturally:

 Debug.Log(hex.x == hex.y); //true
 Debug.Log(hex.x == hex.z); //true
 Debug.Log(hex.y == hex.z); //true

But:

 Debug.Log(hex.x.GetHashCode()); //0
 Debug.Log(hex.y.GetHashCode()); //0
 Debug.Log(hex.z.GetHashCode()); //-2147483648

Further investigation concluded:

 Debug.Log((-0f).GetHashCode()); //-2147483648

So this is a Problem:

 Hex hex2 = new Hex(-0f, -0f);
 Debug.Log(hex.GetHashCode() == hex2.GetHashCode()); //false

Because I want to use the Hex-class as a dictionary key, the Hash of both hex and hex2 should be identical. Performance is very important, because the key is called a lot. So what is the quickest function/calculation to fix this?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Bunny83 · May 02, 2018 at 01:21 PM

Technically -0f and 0f are two completely different values. The IEEE 754 floating point format has the concept of a "signed zero". Logically the two zeros are treated equally in "most" cases but not all. An equal check between the two zeros will return true. However certain calculations will yield a different result. For example 1f / 0f will yield +infinity while 1f / (-0f) will yield -infinity.


The only way to solve this issue is to "normalize" the zero with a seemingly pointless check:

 if (val == 0f)
     val = 0f;

This will ensure if "val" is zero it will always be a positive zero. So just do this in your "Flatten" method or inside your constructor after you called the Flatten method. Note that you probably want to do the check for all 3 values.


Note that GetHashCode does not calculate any special hash for float values. It just returns the same 32 bit pattern as int. So a negative 0 looks like this in hex 0x80000000. This is equal to "-2147483648" as signed integer. Have a look at this IEEE754 converter.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · May 02, 2018 at 01:26 PM 0
Share

Note that since you said performance is important you may want to use a struct ins$$anonymous$$d of a class. Also if you override GetHashCode you also should override Equals. Especially since two class instances are never considered equal by default as only their references are compared and never their content (unlike structs).

avatar image Dollhopf Bunny83 · May 02, 2018 at 02:15 PM 0
Share

Thanks for the tip with struct. I have lot of functions in Hex (including equals, toString and most operators) to be able to compare/add/subtract them, but good to mention.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

82 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

What are the properties of a good key hash? 1 Answer

OpenSSL key hash problems 1 Answer

Does Game objects names affect anything in build ?,does Game objects names affect anything? 1 Answer

Putting dictionary items in GUI ScrollView 1 Answer

From two lists into a dictionary... and back? 5 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges