哈弗森';s公式和Python 3-数学域错误
我试图实现哈弗森公式,以确定给定位置的纬度和经度是否在指定半径内。我在这里使用了一个公式:哈弗森';s公式和Python 3-数学域错误,python,python-3.x,haversine,Python,Python 3.x,Haversine,我试图实现哈弗森公式,以确定给定位置的纬度和经度是否在指定半径内。我在这里使用了一个公式: 我遇到以下可重复输入的数学域错误,这种情况并非总是发生,但经常会让我认为我使用了正确的代码: from math import atan2, sqrt, sin, cos # All long / lat values are in radians and of type float centerLongitude = -0.0391412861306467 centerLatitude = 0.93
我遇到以下可重复输入的数学域错误,这种情况并非总是发生,但经常会让我认为我使用了正确的代码:
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
不能对负数使用
sqrt
:
>>> sqrt(-1)
ValueError: math domain error
使用cmath.srt
:
>>> import cmath
>>> cmath.sqrt(-1)
1j
就你而言:
>>> a = 1.0139858858386017
>>> sqrt(1-a)
ValueError: math domain error