不能比较两个CLLocationCoordinate2Ds(负零?) [英] Can't compare two CLLocationCoordinate2Ds (negative zero??)
问题描述
我有比较两个最困难的时候(我应该是相同的) CLLocationCoordinate2D
结构在我的iOS应用。出于某种原因,不管我怎么对它们进行比较,纬度就不会说这是平等的。
I'm having the hardest time comparing two (what should be identical) CLLocationCoordinate2D
structs in my iOS app. For some reason no matter how I compare them, the latitude just won't say it's equal.
在code:
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!");
}
我甚至尝试如果(@(DLAT)isEqualToNumber:@(0)]放大器;&安培; [@(dlong)isEqualToNumber:@(0)])
作为比较检查,但是失败了。
I even tried if ( [@(dlat) isEqualToNumber:@(0)] && [@(dlong) isEqualToNumber:@(0)] )
as the comparison check, but it failed too.
下面是输出:
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)
出于某种原因,我不断得到负零在那里并没有什么会比较反对。我到底能做些什么来得到这些东西来比较?!
For some reason I just keep getting that negative zero in there and nothing will compare against it. What the heck can I do to get these things to compare?!
推荐答案
DLAT
不具有0.0的值。 使用%E
打印,而不是%F
时,看到其中的差别。
dlat
does not have the value of 0.0. Use %e
when printing instead of %f
to see the difference.
在 DLAT ==一个数字来比较0.0 0.0
得到相同的结果,为 DLAT == -0.0
。没有必要做两个比较。
Comparing a number to 0.0 as in dlat == 0.0
gets the same result as dlat == -0.0
. No need to do both compares.
更多关于-0结果
0.0和-0.0具有相同的数值的值,并在六大运营商比较> =,>,==,=比较就跟对方,&LT中,<!=。
More on -0
0.0 and -0.0 have the same numeric value and compare just like each other in the six compare operators >=, >, ==, !=, <, <=.
如果一个人的必须的零点之间的区别,人们可以使用 INT signbit()
或 memcmp()
。有许多方法。
If one must distinguish between the zeros, one could use int signbit()
or memcmp()
. Many methods exist.
这篇关于不能比较两个CLLocationCoordinate2Ds(负零?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!