我在计算lat lng距离时遇到了一个奇怪的问题。 如果我将此查询输入mysql
SELECT (3959 * acos(cos(radians(30.50492800)) * cos(radians(30.50492800)) * cos(radians(-90.49670600) - radians(-90.496706)) + sin(radians(30.50492800)) * sin(radians(30.50492800))))
它导致null。但是,输入
SELECT (3959 * acos(cos(radians(30.50492800)) * cos(radians(30.50492800)) * cos(radians(-90.49670600) - radians(-90.496706)) + sin(radians(30.50492800)) * sin(radians(30.50492700))))
或
SELECT (3959 * acos(cos(radians(30.50492800)) * cos(radians(30.50492800)) * cos(radians(-90.49670600) - radians(-90.49670)) + sin(radians(30.50492800)) * sin(radians(30.50492800))))
给出结果。
导致这种情况的原因是什么?
答案 0 :(得分:2)
这根本不奇怪,
3个表达式中没有一个计算相同的东西。
差异
SELECT (3959 * acos(cos(radians(30.50492800)) * cos(radians(30.50492800)) *
cos(radians(-90.49670600) - radians(-90.496706)) + sin(radians(30.50492800)) *
sin(radians(30.50492800))))
SELECT (3959 * acos(cos(radians(30.50492800)) * cos(radians(30.50492800)) *
cos(radians(-90.49670600) - radians(-90.496706)) + sin(radians(30.50492800)) *
sin(radians(
30.50492700
))))
SELECT (3959 * acos(cos(radians(30.50492800)) * cos(radians(30.50492800)) * cos(radians(-90.49670600) - radians(
的 -90.49670
强> )) + sin(radians(30.50492800)) * sin(radians(30.50492800))))
<强>评价强>
cos(radians(30.50492800))
等于0.861585503968495
cos(radians(30.50492800))
等于0.861585503968495
cos(radians(-90.49670600) - radians(-90.496706))
等于1 sin(radians(30.50492800)) * sin(radians(30.50492800))
等于0.257670419351355
0.861585503968495 * 0.861585503968495 * 1 + 0.257670419351355
1.000000000000000513393952565025
acos(1.000000000000000513393952565025)
返回null。
cos(radians(30.50492800))
等于0.861585503968495
cos(radians(30.50492800))
等于0.861585503968495
cos(radians(-90.49670600) - radians(-90.496706))
等于1 sin(radians(30.50492800)) * sin(radians(30.50492700))
等于0.257670411718131
0.861585503968495 * 0.861585503968495 * 1 + 0.257670411718131
等于0.999999992366776513393952565025
acos(0.999999992366776513393952565025)
= 0.000123557464423953
3959 * 0.000123557464423953
= 0.489164001654431
cos(radians(30.50492800))
等于0.861585503968495
cos(radians(30.50492800))
等于0.861585503968495
cos(radians(-90.49670600) - radians(-90.49670))
等于0.999999999999995
sin(radians(30.50492800)) * sin(radians(30.50492800))
等于0.257670419351355
0.861585503968495 * 0.861585503968495 * 0.999999999999995 + 0.257670419351355
0.999999999999996801746049321797
acos(0.999999999999996801746049321797)
等于8.16170211889098e-08
3959
,我们得到0.000323121786886894
为什么acos(1.000000000000000513393952565025)会返回null?
返回X的反余弦值,即余弦为X的值。 如果X不在-1到1的范围内,则返回NULL。
如您所见,1.000000000000000513393952565025
高于1。
对于不给出null的其他两个表达式,acos
的给定参数是
两者都在[-1, 1]
范围内,所以我们得不到空。