0

I'm using an implementation of this answer to calculate the distance between two points for my GPS tracking software.

However I've read on numerous StackOverFlow that using double is bad because of the inaccurate float representation of values. Therefore, I'm asking :

  • Would using BigDecimal to calculate distances be more accurate ?
  • Would converting the result from BigDecimal back to double produce additional inaccuracies ? (to store or retrieve the value)
  • How precise does your result have to be? Geo position with nanometer precision or does meter precision suffice? – luk2302 Aug 06 '20 at 10:10
  • 4
    Calculation of distances is inaccurate anyway, because you're calculating the distance on an approximation of the shape of the earth. Are you sure the inaccuracy from doubles is relevant? – Andy Turner Aug 06 '20 at 10:10
  • 1
    `double` are basically fine for most things that you **measure** (in a nutshell, because the measurement is likely to already be less precise than what double can hold), but not ideal for anything that you can **count precisely** (in a nutshell because the small errors introduced by `double` are not tolerable when you count in exact units). In other words: for computing distances from already-imprecise GPS result `double` is more than good enough. – Joachim Sauer Aug 06 '20 at 10:17
  • "Would converting the result back to Double produce additional inaccuracies" No, `Double` is just an object wrapper around a `double`. It is no more or less precise. – Andy Turner Aug 06 '20 at 10:18
  • Indeed, the software needs to be precise to the meter. What i'm asking however is would the result be relevantly different if I would be using more precise types such as BigDecimal, given the number of calculations operated in its function – RainbowQoP Aug 06 '20 at 10:18
  • 1
    The circumference of the earth is ~40,000km, so you probably don't need to store a number bigger than 40 million. The unit of least precision for that number is `Math.ulp(40_000_000.0) == 7.45e-9` - so, 7nm. – Andy Turner Aug 06 '20 at 10:24
  • @HighPerformanceMark btw, 24k is the circumference [in miles](https://www.google.com/search?q=circumference+of+earth+in+miles). – Andy Turner Aug 06 '20 at 10:24
  • 2
    Pff, miles, kilometres, no wonder Columbus thought he'd got to China ! – High Performance Mark Aug 06 '20 at 10:34

2 Answers2

3

No, using BigDecimal instead of double will not significantly improve the accuracy of this algorithm.

The main source of error is a modeling error: you're using a formula that assumes the Earth is a perfect sphere. This means the result can be wrong by as much as 0.5%. The rounding error you get from using double is on the order of 0.000000000001%.

To get more accurate results, you could use for example Vincenty's formulae, which assumes the Earth is an ellipsoid, but it is harder to implement than the great circle distance. To get even more accurate results, you should take into account the local topography, which is even harder.

For further reading: https://en.m.wikipedia.org/wiki/Geographical_distance

Joni
  • 108,737
  • 14
  • 143
  • 193
1

Yes, in theory doing any calculations in BigDecimal could lead to better precisions, but that is very unlikely to actually help for several reasons:

  • as you found out there are non-naive algorithms to calculate the distance using only double without an excessive numerical error.
  • the precision of double values for this use case is many orders of magnitude higher than the precision and accuracy of any positional information you might start out with. That means that the measurement error will definitely be bigger than the numerical error caused by using double.

And if you did your calculations in BigDecimal anyway and then converted the result to double, then you'd lose precision, of course, since BigDecimal can be effectively arbitrarily precise, but double can't.

But again: that loss of precision is irrelevant for the use case you describe.

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614