views:

116

answers:

2

My managed object has 2 double fields: "latitude", "longitude". I need to fetch all objects, that has certain coordinates

This code not working, fetchedObjects count = 0

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"latitude == %f AND longitude == %f", coordinate.latitude, coordinate.longitude];

But this code work fine, fetchedObjects count = 3:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"latitude == 53.012667 AND longitude == 36.113000"];
A: 

it works fine with long float, %lf

abuharsky
Accept your answer as correct, if it solves your problem
Vladimir
A: 

Oh geez...

Don't use == to compare floating point values, ever.

If you want to find objects with a "specific" value, compare against a small range. Otherwise, floating-point representation error will bite you.

So consider using: const float epsilon = 0.000001; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"latitude > %f AND latitude < %f AND longitude > %f AND longitude < %f", coordinate.latitude - epsilon, coordinate.latitude + epsilon, coordinate.longitude - epsilon, coordinate.longitude + epsilon];

jdeprez