简体   繁体   English

Java double vs BigDecimal 纬度/经度

[英]Java double vs BigDecimal for latitude/longitude

When storing latitudes/longitudes which are typically of the format: 44.087585 (ie max 2 numbers before the dot and 6dp) do I need to bother with bigdecimals?当存储通常采用以下格式的纬度/经度时:44.087585(即在点和 6dp 之前最多 2 个数字)我需要使用 bigdecimals 吗?

Using double has enough precision for accurate lat/lon down to inches for 6-7 decimal places.使用double具有足够的精度,可以将 lat/lon 精确到小数点后 6-7 位的英寸。 In aviation, if decimal degrees are used, they typically go to at least 7 decimal places.在航空领域,如果使用十进制度,它们通常 go 至少保留 7 个小数位。 In our NASA simulations, lat/lon data are doubles while all other attitude and altitude are floats.在我们的 NASA 模拟中,纬度/经度数据是doubles的,而所有其他姿态和高度都是浮动的。 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.换句话说,高度的第 6 位小数并不重要,而 lat/lon 的第 6 位是亚英尺精度。 You should not use float for lat/lon if you need precision down to sub-feet.如果您需要精确到亚英尺,则不应将float用于纬度/经度。

You can play around in Google Earth to see what accuracy you get by placing markers and manipulating the last decimal digit.您可以在 Google 地球中玩耍,通过放置标记和操作最后一位小数来查看您获得的准确度。

double should be fine for latitudes/longitudes. double对于纬度/经度应该没问题。 BigDecimal becomes more important once you start manipulating currency or need to have more control over precision.一旦您开始操纵货币或需要对精度进行更多控制, BigDecimal就变得更加重要。

A double has enough precision for storing that. double具有足够的精度来存储它。 However, when displaying to users, use a format string like %.6f so that you don't print garbage digits out.但是,在向用户显示时,请使用%.6f之类的格式字符串,这样您就不会打印出垃圾数字。

If you're concerned about precision, you should probably go with BigDecimal for lots of reasons covered in this post .如果您担心精度,您可能应该使用BigDecimal的 go , 因为这篇文章中涵盖了很多原因 That said, double or float should be just fine to store what you need in terms of being able to satisfy the range of values that you need.也就是说,就能够满足您需要的值范围而言, double精度或float应该可以很好地存储您需要的内容。

At just six decimal places of precision I don't think double or float will trip you up, but if you start aggregating the values in any way, the minor imprecisions can start to add up.精确到小数点后六位,我认为doublefloat不会让你失望,但如果你开始以任何方式聚合这些值,那么次要的不精确就会开始累加。

The worst accuracy of storing as double is about 3 nm.存储为 double 的最差精度约为 3 nm。

If you'd like to calculate it in Java:如果你想在 Java 中计算它:

Math.ulp((double) 180) * 60 * 1852

3.1582203519064933E-9

As a bonus;作为奖励; the worst accuracy you get from storing in floats is 1.7 meters.存储在花车中的最差精度是 1.7 米。

if you want to control nano-robots in their nano-travels using nano-GPS - go for BigDecimal.如果您想使用纳米 GPS 控制纳米机器人的纳米旅行 - go 用于 BigDecimal。 otherwise - float/double is enough.否则 - float/double 就足够了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM