tags:

views:

836

answers:

3

Hi,

I'm trying to calculate a distance between two sets of coordinate points in an iPhone application on the fly using

 - (CLLocationDistance)getDistanceFrom:(const CLLocation *)location

I saw that I started getting NaN's in strange places, thus investigated the matter up close, with the following hardcoded values.

CLLocationDistance testDistance;
placeLocation = [[CLLocation alloc] initWithLatitude:12.236533 longitude:11.011419];
userLocation = [[CLLocation alloc] initWithLatitude:12.236533 longitude:11.011419]; 
testDistance = [userLocation getDistanceFrom:placeLocation];
if(isnan(testDistance))
 NSLog(@"ISNaN!");

[placeLocation release];
[userLocation release];

The above gets called multiple times, and in some situations the testDistance is NaN.

I fear I'm missing something very simple here. Anyone have any idea?

UPDATE 1:

Ok, so I've moved the above code into a new project (put everything in the app delegate), looped it for 100 times and all is fine. This thus suggests that the problem is project related, but this helps very little... since all the variables are in the scope of 1 function.

THE SOLUTION:

OK, this seems to be a simulator bug. The same code build runs perfectly fine on the device. Case solved.

+1  A: 

You are saying that you actually get NaN when the two locations have the exact same lat/long? Wow...

One thing you might do is test the reverse -- if you DO get NaN, try reversing the two CLLocations:

testDistance = [placeLocation getDistanceFrom: userLocation];

and see if the result always matches; i.e., always either NaN for both ways, or NOT NaN for both ways.

Another thing to check would be what happens when you test

testDistance = [userLocation getDistanceFrom: userLocation];

I'm sorry this isn't an answer, but maybe we can get some more clues.

Amagrammer
It's just a coincidence that these two locations have the same lat/long values. It doesn't matter. Anything I put in, yelds the same NaN result from time to time.Tried reversing them before, though it makes little sense.
Kaspa
Have you tried something like a loop? While <computed value> == NaN, try again. (up to say, 10 tries) Again, this is just troubleshooting, it's obviously not something you'd want to leave in production code.
Amagrammer
A: 

THE SOLUTION:

OK, this seems to be a simulator bug. The same code build runs perfectly fine on the device. Case solved, how do you file a bug with Apple?

Kaspa
This is not valid to click your own answer if someone had helped you figure out how to get it.. The above posting (by Amagrammer) was really the answer...not "simulator bug".
Jann
It's perfectly valid. This is not about who was the most badges etc. but about the next person who comes here looking for the answer (with all due respect for Amagrammer for helping out). Did you run into the same problem? I often find that things tend to work differently on the sim / device, this is caused by different processor architectures.
Kaspa
A: 

use distanceFromLocation: instead

Germo