Reputation: 3
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 :
BigDecimal
to calculate distances be more accurate ?double
produce additional inaccuracies ? (to store or retrieve the value)Upvotes: 0
Views: 267
Reputation: 111219
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
Upvotes: 3
Reputation: 308001
Yes, in theory doing any calculations in BigDecimal
could lead to better precisions, but that is very unlikely to actually help for several reasons:
double
without an excessive numerical error.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.
Upvotes: 1