Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing coordinate from Core Location in Core Data. Float or Double?

I saw both variants people use... Just don't know is float not enough for iOS5?

like image 601
Shmidt Avatar asked Oct 21 '11 15:10

Shmidt


4 Answers

A 32-bit float holds 7 digits of precision, a 64-bit double holds 15 digits of precision.

With 7 digits of precision the maximum longitude of 179.9999 is only accurate down to 0.0001 degrees. A degree is about 60 nautical miles so 0.0001 degrees is about 10 meters (33 feet). If you want to store more precise lat/lon values, then you need to use a double (or a fixed point integer).

like image 104
progrmr Avatar answered Jan 02 '23 08:01

progrmr


These days it doesn't matter much.

A double is just a float value slot that can hold twice as much precision as a standard float. Unless you force it to use a float, the Objective-c compiler will actually store all float values as a double.

Floats and Doubles are largely hold overs from the old days when memory was super tight. These days, you usually only see them different when it comes to string formatting. It's the same with various sizes of int. All sizes are stored in the largest available size unless you force the compiler to do otherwise.

like image 27
TechZen Avatar answered Jan 02 '23 07:01

TechZen


For answering which accuracy you have when using floats or double I have come up with this answer:

Worst case accuracy when storing as decimal numbers for E/W and N/S:

  • float: 1.6955566 meters
  • double: 3.1582203519064933E-9 meters. (That is 3 nm)

You can do the calculations yourself by running this in Java:

public class Main {
  public static void main(String[] args) {
    System.out.println(Math.ulp((float) 180) * 60 * 1852);
    System.out.println(Math.ulp((double) 180) * 60 * 1852);
  }
}

I know this is an old thread but I found it while searching for the problem. Float is most probably enough for most applications. The GPS of the Apple device in question does probably not have better accuracy than that. But it is easier to store doubles as long as data is not a concern.

like image 30
Gussoh Avatar answered Jan 02 '23 08:01

Gussoh


Recently, I think it's just easier to store coordinate in Core Data as NSValue in transformable attribute:

[NSValue valueWithMKCoordinate:coordinate]

https://developer.apple.com/documentation/foundation/nsvalue/1452193-valuewithmkcoordinate

like image 21
Shmidt Avatar answered Jan 02 '23 07:01

Shmidt