Rails 3 Float or decimal for GPS coordinates

前端 未结 4 1148
走了就别回头了
走了就别回头了 2020-12-31 09:00

I need to store GPS coordinates in a database. I\'ve heard that floats are less accurate than decimals. Is that true? If so, what reason is there to ever use floats?

4条回答
  •  时光说笑
    2020-12-31 09:54

    Since Earth equatorial circumference is around 40000 km and longitude spans 360 degrees, it follows that near the equator 1 degrees is equivalent to about 110 km.

    Thus:

    • 0 digit precision: ±110 km
    • 1 digit precision: ±11 km
    • 2 digit precision: ±1.1 km
    • 3 digit precision: ±110 m
    • 4 digit precision: ±11 m
    • 5 digit precision: ±1.1 m
    • 6 digit precision: ±11 cm
    • 7 digit precision: ±1.1 cm

    However, the closer you move to the Poles, the smaller the lenghts of the parallels becomes. For example near Paris, which has a latitude of 48 degrees, 1 degree is equivalent to cos(48) * 110 = 74 km.

    Thus:

    • 0 digit precision: ±74 km
    • 1 digit precision: ±7.4 km
    • 2 digit precision: ±0.74 km
    • 3 digit precision: ±74 m
    • 4 digit precision: ±7.4 m
    • 5 digit precision: ±0.74 m
    • 6 digit precision: ±7.4 cm
    • 7 digit precision: ±0.74 cm

    So, field of type DECIMAL(10,7) would be enough to satisfy you rather precise.

提交回复
热议问题