KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I 'm integrating a geographic coordinate class from CodePlex to my personal "toolbox" library. This class uses float fields to store latitude and longitude. Since the class GeoCoordinate implements IEquatable<GeoCoordinate> , I habitually wrote the Equals method like so: public bool Equals(GeoCoordinate other) { if (other == null) { return false; } return this.latitude == other.latitude && this.longitude == other.longitude; } At this point I stopped and considered that I 'm comparing floating point variables for equality, which is generally a no-no. My thought process then went roughly as follows: I can only imagine setting the Latitude and Longitude properties once, which means that there will be no errors being accumulated to mess up my comparisons. On the other hand, it's possible (albeit pointless) to write var geo1 = new GeoCoordinate(1.2, 1.2); var geo2 = new GeoCoordinate(1.2, 1.2); // geo1.Equals(geo2) will definitely be true, BUT: geo2.Latitude *= 10; geo2.Latitude /= 10; // I would think that now all bets are off Of course this is not something I can imagine doing, but if the public interface of the class allows it then Equals should be able to handle it. Comparing for equality using a difference < epsilon test would solve the problem of comparing two instances, but create more problems: How to make equality transitive? It sounds impossible. How to produce the same hash code for all values that would compare equal? Let's say that epsilon = 0.11 (random example). It follows that GeoCoordinate { 1, 1 } would need the same hash code as GeoCoordinate { 1.1, 1.1 } . But the latter would need the same hash
Tags (comma-separated)
Save Edits
Cancel