【发布时间】:2013-08-14 13:38:34
【问题描述】:
我很难在我的 iOS 应用程序中比较两个(应该相同的)CLLocationCoordinate2D 结构。由于某种原因,无论我如何比较它们,纬度都不会说它是平等的。
代码:
double dlat = self.mapView.centerCoordinate.latitude - self.centerOnNextView.coordinate.latitude;
double dlong = self.mapView.centerCoordinate.longitude - self.centerOnNextView.coordinate.longitude;
NSLog(@"dlat: %f dlong: %f, dlat == 0.0: %d, dlat == -0.0: %d, dlong == 0.0: %d, dlong == -0.0: %d",
dlat, dlong, dlat == 0.0, dlat == -0.0, dlong == 0.0, dlong == -0.0);
if ( ( dlat == 0.0 || dlat == -0.0 ) && ( dlong == 0.0 || dlong == -0.0 ) ) {
NSLog(@"WE ARE EQUAL!");
}
我什至尝试过if ( [@(dlat) isEqualToNumber:@(0)] && [@(dlong) isEqualToNumber:@(0)] ) 作为比较检查,但也失败了。
这是输出:
dlat: -0.000000
dlong: 0.000000
dlat == 0.0: 0 (false)
dlat == -0.0: 0 (false)
dlong == 0.0: 1 (true)
dlong == -0.0: 1 (true)
出于某种原因,我只是不断地得到那个负零,没有什么可以与之相比。我该怎么做才能比较这些东西?!
【问题讨论】:
-
什么,周一晚上 9:30 没人上班? XD
-
dlat很可能不为 0。可能是您的 printf 的精度将其四舍五入以进行显示。
标签: c floating-point core-location cllocationcoordinate2d