0

I am trying to do the great circle distance calculation. As you should be able to glean, the Location class has the properties listed in the calculation.

- (NSNumber *)greatCircleDistanceFrom:(Location *)other
{
    // Unpack all the NSNumbers into doubles so we can manipulate them
    double selfCosRadLat = [self.cosRadLat doubleValue];
    double otherCosRadLat = [other.cosRadLat doubleValue];
    double selfRadLng = [self.radLng doubleValue];
    double otherRadLng = [other.radLng doubleValue];
    double selfSinRadLat = [self.sinRadLat doubleValue];
    double otherSinRadLat = [other.sinRadLat doubleValue];

    // Multiplying by 3959 calculates the distance in miles.
    double d = acos(selfCosRadLat
                    * otherCosRadLat
                    * cos(selfRadLng - otherRadLng)
                    + selfSinRadLat
                    * otherSinRadLat
                    ) * 3959.0;

    return [NSNumber numberWithDouble:d];
}

Half the time I run my unit test, I get the right value. The other half, I get 6218.78265778.

Community
  • 1
  • 1
john
  • 3,043
  • 5
  • 27
  • 48

1 Answers1

3

Make sure that your incoming Location value isn't nil or 0,0. It seems like the reason you'd get a constant like that is because it is doing the math as if it were at 0°,0°. Are you 6218 miles from approximately west Africa? If so, your function is working great, but the method calling it isn't providing real values some of the time.

Tim
  • 14,447
  • 6
  • 40
  • 63
  • Actually, thank you. I see now that the calculation fails because the values were 0,0. However, they shouldn't be 0,0. I now have a new problem with Core Data... – john Jan 11 '12 at 21:49
  • At least you no longer have this problem :) – Tim Jan 11 '12 at 22:03
  • Yes, [new problem](http://stackoverflow.com/questions/8827495/core-data-cascading-calculation-not-sticking). – john Jan 11 '12 at 22:19