Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java double vs BigDecimal for latitude/longitude

When storing latitudes/longitudes which are typically of the format: 44.087585 (i.e. max 2 numbers before the dot and 6dp) do I need to bother with bigdecimals?

like image 458
DD. Avatar asked Jul 19 '11 22:07

DD.


People also ask

Which is more accurate BigDecimal or double?

A BigDecimal is an exact way of representing numbers. A Double has a certain precision. Working with doubles of various magnitudes (say d1=1000.0 and d2=0.001 ) could result in the 0.001 being dropped alltogether when summing as the difference in magnitude is so large. With BigDecimal this would not happen.

Should latitude be float or double?

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.

What is the advantage of BigDecimal over double?

Explanation: BigDecimal provides more precision as compared to double. Double is faster in terms of performance as compared to BigDecimal.

Why should you use BigDecimal?

BigDecimal provides full control over the precision and rounding of the number value. Virtually, it's possible to calculate the value of pi to 2 billion decimal places using BigDecimal, with available physical memory being the only limit.


1 Answers

Using double has enough precision for accurate lat/lon down to inches for 6-7 decimal places. In aviation, if decimal degrees are used, they typically go to at least 7 decimal places. In our NASA simulations, lat/lon data are doubles while all other attitude and altitude are floats. In other words, the 6th decimal place for altitude isn't significant while the 6th decimal place for lat/lon is for sub-foot accuracy. You should not use float for lat/lon if you need precision down to sub-feet.

You can play around in Google Earth to see what accuracy you get by placing markers and manipulating the last decimal digit.

like image 91
TreyA Avatar answered Sep 22 '22 17:09

TreyA