24

I am storing latitude and longitude at the server side (JAVA Platform).

To store those values, I am using float and double datatype at the server side. I came to know that float and double are not a recommended primitive datatypes (which is not recommended to use currencies in professional way), because float and double having rounding precision problem.

In my case, I am just comparing the stored coordinates (latitude and longitude) at the server side.

Question 1:

Comparing the coordinates with the datatypes (float or double) will make any problem in future?

Question 2:

Using big decimal is good? or Going with float or double is safer?

Cœur
  • 37,241
  • 25
  • 195
  • 267
ArunRaj
  • 1,780
  • 2
  • 26
  • 48
  • 6 decimal places (which is max precision of a float) is enough. See http://gis.stackexchange.com/a/8674 – Martin Oct 14 '16 at 17:44
  • 1
    @JarrodRoberson This question is about Java, the other question is about MySQL. How can this question be a duplicate then? – user247702 Aug 01 '17 at 08:16

3 Answers3

33

You should start with the accuracy you desire. Once you have determined that, you can choose a data type that is suitable.

If you decide that an accuracy of 5 decimal places (1.1132 m) is enough, you can easily go with float. The more accurate your calculation needs to be, the more you should lean towards using double and eventually BigDecimal.

When comparing floating point numbers, you should incorporate the necessary precision as well.

Kai Sternad
  • 22,214
  • 7
  • 47
  • 42
  • 3
    +1 for this answer. Check this link to have an idea of the precision you need http://en.wikipedia.org/wiki/Wikipedia%3AWikiProject_Geographical_coordinates#Precision – MrUpsidown Dec 18 '13 at 13:21
  • 1 degree of latitude or longitude is about 111km. Also: `double l = -179.123456789; System.out.println((float)f);` prints: `-179.15346`, then you can estimate an accuracy around 1m only if you code it with 'float' (4 bytes) – Bruno L. Jul 28 '19 at 14:35
  • Usually I do not expect (civil) GPS in a smartphone to be more precise than 1m so for most cases float should be fine. – FrankKrumnow Sep 23 '21 at 15:16
21

It's not a matter of safety, its just a matter of precision. I wouldn't consider floats, but doubles are what i think are ideal here. You just need to see what's the most precision you can get out of a double and see if it fits a regular longitude/latitude value. I think it's more then enough.

Else BigDecimal is just a simple backdoor to your problem, use it if you want more precision

ThaBomb
  • 702
  • 6
  • 11
  • 2
    For curious, Wanted to know How much precision the double can handle. As per my observation, It can handle 14 digits of precision (to my case). So I think It is good for me. If double can handle more than 20 digits, double would be the ideal for my app. Your suggestion please . – ArunRaj Dec 18 '13 at 12:17
  • depending on the java you are using, the double primitive datatype is 64 bits long with of course 1 digit for the sine, etc.. You can find the information on the precision here: http://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.3 – ThaBomb Dec 19 '13 at 00:17
  • Thank you so much for your valuable guidance. – ArunRaj Dec 19 '13 at 06:50
4

you will not have to do any rounding on your longitude or latitude values, so use whichever one you want. Double is more precise, so let that be the most important factor in your decision.

How much precision do your long/lat values need?

For pinpoint accuracy on a map, you might want to look at big decimal, but for vague values, float is good enough.

Husman
  • 6,819
  • 9
  • 29
  • 47
  • 1
    It might need 20 digit. But Not sure. As per my observation this is the case. And of course I am not doing any rounding operations. I am simply checking the following condition if((swLat <= lattitude) && (swLng <= longtitude) && (neLat >= lattitude) && (neLng >= longtitude)) ... If i have implemented with double, the condition is working fine. But fear to use it, Because I need accuracy. Using Double is a ideal option for my case ? – ArunRaj Dec 18 '13 at 11:53