【发布时间】:2018-03-03 15:20:21
【问题描述】:
我正在尝试实现 Haversine 的公式来确定给定位置的纬度和经度是否在指定半径内。我在这里使用了一个详细的公式:
Calculate distance between two latitude-longitude points? (Haversine formula)
我遇到了以下可重现输入的数学域错误,它不会一直发生,但经常足以让我认为我编写了不正确的代码:
from math import atan2, sqrt, sin, cos
# All long / lat values are in radians and of type float
centerLongitude = -0.0391412861306467
centerLatitude = 0.9334153362515779
inputLatitudeValue = -0.6096173085842176
inputLongitudeValue = 2.4190393564390438
longitudeDelta = inputLongitudeValue - centerLongitude # 2.4581806425696904
latitudeDelta = inputLatitudeValue - centerLatitude # -1.5430326448357956
a = (sin(latitudeDelta / 2) ** 2 + cos(centerLatitude) * cos(centerLongitude)
* sin(longitudeDelta / 2) ** 2)
# a = 1.0139858858386017
c = 2 * atan2(sqrt(a), sqrt(1 - a)) # Error occurs on this line
# Check whether distance is within our specified radius below
【问题讨论】:
标签: python python-3.x haversine