为什么我得到不确定的结果?

时间:2019-10-18 21:34:02

标签: vb.net vb.net-2010

我有一个通过坐标计算距离的功能

Private Function CalcularDistancia(ByVal X As Double, ByVal Y As Double, ByVal X2 As Double, ByVal Y2 As Double) As Double
    Dim pi As Double = 3.1415926535897931
    Dim X_1, X_2, Y_1, Y_2, DistanciaTotal As Double

    X_1 = ((90 - X) * pi) / 180
    X_2 = ((90 - X2) * pi) / 180
    Y_1 = (Y * pi) / 180
    Y_2 = (Y2 * pi) / 180

    DistanciaTotal = Acos(Cos(X_1) * Cos(X_2) + Sin(X_1) * Sin(X_2) * Cos(Y_1 - Y_2)) * 6371
    Return DistanciaTotal
End Function

当两个坐标相同但并非总是相同时,似乎会发生错误。 除了 -1。#IND (不确定的NaN),我没有收到任何错误。 我看不到零除或函数中有违法的东西。

我该如何调试呢?

What does -1.#IND mean (double stream output)

1 个答案:

答案 0 :(得分:1)

Private Function CalcularDistancia(ByVal X As Double, ByVal Y As Double, ByVal X2 As Double, ByVal Y2 As Double) As Double
    Dim pi As Double = 3.1415926535897931
    Dim X_1, X_2, Y_1, Y_2, DistanciaTotal As Double

    X_1 = ((90 - X) * pi) / 180
    X_2 = ((90 - X2) * pi) / 180
    Y_1 = (Y * pi) / 180
    Y_2 = (Y2 * pi) / 180

    ' ArcCos(N) evaluation does a divide by Sqr(-N * N + 1) thus causing division by zero for some values, e.g. 1.
    Dim MyXY as Double, MyNN1 as Double
    MyXY = (Cos(X_1) * Cos(X_2) + Sin(X_1) * Sin(X_2) * Cos(Y_1 - Y_2))
    MyNN1 = -MyXY * MyXY + 1
    If (MyNN1 = 0) bail out

    DistanciaTotal = Acos(MyXY) * 6371

    Return DistanciaTotal
End Function