如何在Java中精确计算距离?

huangapple go评论59阅读模式
英文:

How to precisely calculate distances in Java?

问题

我正在使用这个答案的实现来计算我的GPS跟踪软件中两点之间的距离。

然而,我在许多StackOverflow帖子上都看到,使用double是不好的,因为它会导致值的不准确的浮点表示。因此,我想问:

  • 使用BigDecimal来计算距离会更准确吗?

  • 将BigDecimal的结果转换回double会产生额外的不准确性吗?(用于存储或检索值)

英文:

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)

答案1

得分: 3

不,使用BigDecimal而不是double不会显著提高此算法的准确性。

主要的误差源是建模误差:您使用的公式假定地球是一个完美的球体。这意味着结果可能出现多达0.5%的错误。使用double会产生大约0.000000000001%的舍入误差。

为了获得更精确的结果,您可以使用例如Vincenty的公式,它假设地球是一个椭球,但它比大圆距离更难实现。要获得更精确的结果,您应该考虑到本地地形,这更加困难。

进一步阅读:https://en.m.wikipedia.org/wiki/Geographical_distance

英文:

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

答案2

得分: 1

是的,理论上在BigDecimal中进行任何计算可能会导致更好的精度,但出于多种原因,这种情况几乎不可能真正有所帮助:

  • 正如你所发现的,有非朴素的算法可以在仅使用double而不产生过多数值误差的情况下计算距离。
  • 对于这种用例,double值的精度要比您可能开始时拥有的任何位置信息的精度和准确性高出多个数量级。这意味着测量误差肯定会比使用double引起的数值误差大。

如果你无论如何在BigDecimal中进行计算,然后将结果转换为double,那么你当然会失去精度,因为BigDecimal可以有效地具有任意精度,而double不能。

但再次强调:对于你描述的用例来说,这种精度损失是不相关的。

英文:

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.

huangapple
  • 本文由 发表于 2020年8月6日 18:07:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/63281278.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定