相关疑难解决方法(0)

实现地理坐标类:相等比较

我正在将CodePlex的地理坐标类集成到我的个人"工具箱"库中.此类使用float字段来存储纬度和经度.

自从类GeoCoordinate实现以来IEquatable<GeoCoordinate>,我习惯性地编写了Equals这样的方法:

public bool Equals(GeoCoordinate other)
{
    if (other == null) {
        return false;
    }

    return this.latitude == other.latitude && this.longitude == other.longitude;
}
Run Code Online (Sandbox Code Playgroud)

在这一点上,我停下来,并认为我正在比较浮点变量的相等性,这通常是禁忌.我的思考过程大致如下:

  1. 我只能设想一次设置Latitude和Longitude属性,这意味着不会累积任何错误来搞砸我的比较.

  2. 另一方面,写(尽管没有意义)是可能的

    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
    
    Run Code Online (Sandbox Code Playgroud)

    当然,这不是我能想象到的,但如果类的公共接口允许它,那么它Equals应该能够处理它.

  3. 使用difference < …

c# geocoding equality

11
推荐指数
2
解决办法
2912
查看次数

标签 统计

c# ×1

equality ×1

geocoding ×1