c - Can't compare two CLLocationCoordinate2Ds (negative zero??) -


i'm having hardest time comparing 2 (what should identical) cllocationcoordinate2d structs in ios app. reason no matter how compare them, latitude won't it's equal.

the 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 equal!"); } 

i tried if ( [@(dlat) isequaltonumber:@(0)] && [@(dlong) isequaltonumber:@(0)] ) comparison check, failed too.

here's output:

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 reason keep getting negative 0 in there , nothing compare against it. heck can these things compare?!

dlat not have value of 0.0. use %e when printing instead of %f see difference.

comparing number 0.0 in dlat == 0.0 gets same result dlat == -0.0. no need both compares.


more on -0
0.0 , -0.0 have same numeric value , compare each other in 6 compare operators >=, >, ==, !=, <, <=.

if 1 must distinguish between zeros, 1 use int signbit() or memcmp(). many methods exist.


Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -