经纬度计算球面距离,ValueError: math domain error错误

def distance(lat1,lon1,lat2,lon2):
    lat1=lat1*math.pi/180
    lon1=lon1*math.pi/180
    lat2 = lat2 * math.pi / 180
    lon2=lon2*math.pi/180
    dis=math.acos(round((math.sin(lat1)*math.sin(lat2))+(math.cos(lat1)*math.cos(lat2)* math.cos(lon2-lon1)),10))*6371.004
    #dis = math.acos((math.sin(lat1) * math.sin(lat2)) + (math.cos(lat1) * math.cos(lat2) * math.cos(lon2 - lon1))) * 6371.004
    #由于acos的定义域为【-1,1】,当两个点为相同的点时,acos()内的计算结果理论上应为1,但实际保存的结果可能大于1,引发ValueError: math domain error错误
    #这里使用round()对acos()内的结果进行小数截留
    return dis
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值