0

I have a function to calculate distance by coordinates

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

The error seems to happen when both coordinates are the same but no always. I don't receive any error but a -1.#IND(Indeterminate NaN) instead. I don't see any division by zero or something illegal in my function.

How I can debug this?

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

E_Blue
  • 1,021
  • 1
  • 20
  • 45
  • I can’t reproduce this. What exact parameter values are you using to get the bad result? – Dai Oct 18 '19 at 21:38
  • To troubleshoot this, break up the last line (with the trigonometric functions) into different statements then use your step-through debugger to find out which line contains a bad value. – Dai Oct 18 '19 at 21:39
  • Side note: You can use [Math.PI](https://learn.microsoft.com/en-us/dotnet/api/system.math.pi?view=netframework-4.8) – LarsTech Oct 18 '19 at 21:43
  • @Dai That's the problem I have more than 2.000(two thousand) coordinates; those coordinates are the path followed by a GPS so I send those coordinates to this function to calculate the whole distance; first the coordinates 1 and 2, then the coordinates 2 and 3, 3 and 4, and so on. If I put a Try Catch seems like ain't going to catch which coordinates are the problematic. – E_Blue Oct 18 '19 at 21:57
  • 1
    @E_Blue Do `If IsNaN( DistanciaTotal ) Throw New Exception`. – Dai Oct 18 '19 at 22:22
  • @Dai IsNaN is not defined. Should I import some library? – E_Blue Oct 18 '19 at 22:42
  • 1
    @E_Blue - `If Double.NaN = DistanciaTotal Then Throw New Exception("DistanciaTotal is not a valid number: " & DistanciaTotal)` – David Oct 18 '19 at 23:02
  • @Dai Ok, sorry, I never used that function before; I understand now. – E_Blue Oct 18 '19 at 23:05
  • @David a direct comparison with NaN won't work, as NaN compares unequal to everything including another NaN. Use `Double.IsNaN` to check for a NaN value. – Craig Oct 21 '19 at 13:17
  • @Craig I just do it that, by mistake. LOL – E_Blue Oct 21 '19 at 13:48

1 Answers1

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
donPablo
  • 1,937
  • 1
  • 13
  • 18
  • I get lost at bail out line. Should I return 0? – E_Blue Oct 18 '19 at 22:44
  • Take a look at your original X/Y/X2/Y2 and manually calculate what that distance should be. Put a breakpoint there and look at input values to see what is causing it. – donPablo Oct 18 '19 at 22:47
  • I get MyXY=1.0000000000000002 so I put that number on the Windows Calculator and get "Invalid input". – E_Blue Oct 18 '19 at 22:52
  • For that MyXY, what are the original X/Y/X2/Y2? – donPablo Oct 18 '19 at 22:53
  • CalcularDistancia(-34.95245, -57.9187, -34.95245, -57.9187) – E_Blue Oct 18 '19 at 22:54
  • So for that X/Y... what do you want the Distancia to be? They look like the same poiints. – donPablo Oct 18 '19 at 22:56
  • Yes but if I use CalcularDistancia(-34.98245, -57.9187, -34.98245, -57.9187) I get 0.0; Why I can't get 0.0 for the first one? I think I'm going to return 0 every time the points are the same. Do you see any other way that I can get an undefined result or an infinite result? – E_Blue Oct 18 '19 at 23:00
  • @E_Blue Windows Calculator does not use IEEE-754 floats for its calculations, so it will not give you the same results as .NET's `Single` and `Double` types. – Dai Oct 19 '19 at 00:10