哈弗辛公式与python3数学域E

2024-10-03 21:26:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图实现哈弗辛的公式来确定给定地点的经纬度是否在指定的半径内。我用了一个公式:
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

Tags: 错误sqrtsincos公式distance经纬度地点