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 6181215
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T00:58:51+00:00 2026-05-24T00:58:51+00:00

I ‘m integrating a geographic coordinate class from CodePlex to my personal toolbox library.

  • 0

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:

  1. 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.

  2. 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.

  3. 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 code as GeoCoordinate { 1.2, 1.2 }. You can see where this is going: all instances would need to have the same hash code.

  4. A solution to all of this would be to make GeoCoordinate an immutable class. This would also solve the GetHashCode problem: it is based on latitude and longitude (what else), and if these are are mutable then using GeoCoordinate as a key into a dictionary is asking for trouble. However, to make the class immutable has its own drawbacks:

    • You cannot instantiate-and-configure instances of the class (the WPF paradigm), which might be a pain in some cases
    • Serialization would probably also become a pain due to the loss of a parameterless constructor (I ‘m not a .NET serialization expert so that’s as much detail as I see here)

Which approach would you suggest? It’s easy to make the class fit the requirements I have right now (just make it immutable), but is there some better way?

Edit: I added an item 3 in the list above, shifting the previous item 3 to position 4.

Solution

I ‘m going to allow some more time for feedback, but presently I ‘m going with the immutable approach. The struct (because that’s what it is now) with the relevant members can be seen here; comments and suggestions more than welcome.

  • 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-24T00:58:52+00:00Added an answer on May 24, 2026 at 12:58 am

    A “location” with longitude/latitude to me falls quite nicely into the “immutable value” slot. The position itself doesn’t change – if you change the latitude that is a different position. From there, it could be a struct; for float the struct would be the same size as an x64 reference anyway, so no real down side.

    Re equality; if the position isn’t quite the same, it isn’t “equals”, at least in the “key” perspective, so I’d be happy with == here. You could add a “is within (x) distance” method if it helped. Of course, great-arc geometry isn’t exactly totally free either ;p

    Thoughts though:

    • it should override bool Equals(object) as well as adding a bool Equals(GeoCoordinate)
    • it should override GetHashCode() and implement IEquatable<GeoCoordinate>
    • static operators are a nice-to-have optional extra
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.