c# - Implementing a geographic coordinate class: equality comparison -


i 'm integrating geographic coordinate class codeplex personal "toolbox" library. class uses float fields store latitude , longitude.

since class geocoordinate implements iequatable<geocoordinate>, habitually wrote equals method so:

public bool equals(geocoordinate other) {     if (other == null) {         return false;     }      return this.latitude == other.latitude && this.longitude == other.longitude; } 

at point stopped , considered 'm comparing floating point variables equality, no-no. thought process went follows:

  1. i can imagine setting latitude , longitude properties once, means there no errors being accumulated mess comparisons.

  2. on other hand, it's possible (albeit pointless) write

    var geo1 = new geocoordinate(1.2, 1.2); var geo2 = new geocoordinate(1.2, 1.2);  // geo1.equals(geo2) true, but:  geo2.latitude *= 10; geo2.latitude /= 10;  // think bets off 

    of course not can imagine doing, if public interface of class allows equals should able handle it.

  3. comparing equality using difference < epsilon test solve problem of comparing 2 instances, create more problems:

    • how make equality transitive? sounds impossible.
    • how produce same hash code all values compare equal?

      let's epsilon = 0.11 (random example). follows geocoordinate { 1, 1 } need same hash code geocoordinate { 1.1, 1.1 }. latter need same hash code geocoordinate { 1.2, 1.2 }. can see going: all instances need have same hash code.

  4. a solution of make geocoordinate immutable class. solve gethashcode problem: based on latitude , longitude (what else), , if these are mutable using geocoordinate key dictionary asking trouble. however, make class immutable has own drawbacks:

    • you cannot instantiate-and-configure instances of class (the wpf paradigm), might pain in cases
    • serialization become pain due loss of parameterless constructor (i 'm not .net serialization expert that's detail see here)

which approach suggest? it's easy make class fit requirements have right (just make immutable), there better way?

edit: added item 3 in list above, shifting previous item 3 position 4.

solution

i 'm going allow more time feedback, presently 'm going immutable approach. struct (because that's now) relevant members can seen here; comments , suggestions more welcome.

a "location" longitude/latitude me falls quite nicely "immutable value" slot. position itself doesn't change - if change latitude different position. there, could struct; float struct same size x64 reference anyway, no real down side.

re equality; if position isn't quite same, isn't "equals", @ least in "key" perspective, i'd happy == here. add "is within (x) distance" method if helped. of course, great-arc geometry isn't totally free either ;p

thoughts though:

  • it should override bool equals(object) adding bool equals(geocoordinate)
  • it should override gethashcode() , implement iequatable<geocoordinate>
  • static operators nice-to-have optional extra

Comments

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

php - How can I edit my code to echo the data of child's element where my search term was found in, in XMLReader? -