MySQL trig数学计算距离结果为null

时间:2015-03-11 15:47:40

标签: mysql

我在计算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))))

给出结果。

导致这种情况的原因是什么?

1 个答案:

答案 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))))


<强>评价

  • 第一个表达
    1. cos(radians(30.50492800))等于0.861585503968495
    2. cos(radians(30.50492800))等于0.861585503968495
    3. cos(radians(-90.49670600) - radians(-90.496706))等于1
    4. sin(radians(30.50492800)) * sin(radians(30.50492800))等于0.257670419351355
    5. 所以我们评估了0.861585503968495 * 0.861585503968495 * 1 + 0.257670419351355
    6. 1.000000000000000513393952565025
    7. acos(1.000000000000000513393952565025)返回null。
  • 第二个表达
    1. cos(radians(30.50492800))等于0.861585503968495
    2. cos(radians(30.50492800))等于0.861585503968495
    3. cos(radians(-90.49670600) - radians(-90.496706))等于1
    4. sin(radians(30.50492800)) * sin(radians(30.50492700))等于0.257670411718131
    5. 所以我们评估0.861585503968495 * 0.861585503968495 * 1 + 0.257670411718131等于0.999999992366776513393952565025
    6. acos(0.999999992366776513393952565025) = 0.000123557464423953
    7. 3959 * 0.000123557464423953 = 0.489164001654431
  • 第三个表达
    1. cos(radians(30.50492800))等于0.861585503968495
    2. cos(radians(30.50492800))等于0.861585503968495
    3. cos(radians(-90.49670600) - radians(-90.49670))等于0.999999999999995
    4. sin(radians(30.50492800)) * sin(radians(30.50492800))等于0.257670419351355
    5. 因此,我们评估等于0.861585503968495 * 0.861585503968495 * 0.999999999999995 + 0.257670419351355
    6. 0.999999999999996801746049321797
    7. acos(0.999999999999996801746049321797)等于8.16170211889098e-08
    8. 乘以3959,我们得到0.000323121786886894

为什么acos(1.000000000000000513393952565025)会返回null?

来自documentation

  

返回X的反余弦值,即余弦为X的值。   如果X不在-1到1的范围内,则返回NULL。

如您所见,1.000000000000000513393952565025高于1。

对于不给出null的其他两个表达式,acos的给定参数是

  • 0.999999992366776513393952565025
  • 0.000323121786886894

两者都在[-1, 1]范围内,所以我们得不到空。