Lat/Long Distance Calculation

I amtrying to write a script that would compute the distance between an "x" number of points. This is what I have come up with so far and it is not working. Can anyone modify it to make it work?

A=34.16597 B=-84.33244
C=34.2344 D=-84.29189

   test "$A" -eq "$C" -o "$B" -eq "$D"
then
echo "The distance is zero"
else
if
   "sin$Asin$C + cos$Acos$Ccos[$B - $D]" -gt 1
then
echo "The distance is"
3963.1 "*" ARCOS[1]
else
echo "The distance is"
3963.1 "*" ARCOS[sin$Asin$C+cos$Acos$Ccos[$B-$D]
fi
fi

Thanks!

nawk on solaris has the following arithmetic functions.

Arithmetic Functions
The arithmetic functions, except for int, are based on the
ISO C standard. The behavior is undefined in cases where the
ISO C standard specifies that an error be returned or that
the behavior is undefined. Although the grammar permits
built-in functions to appear with no arguments or
parentheses, unless the argument or parentheses are indi-
cated as optional in the following list (by displaying them
within the [ ] brackets), such use is undefined.

 atan2\(y,x\)      Return arctangent of y/x.

 cos\(x\)          Return cosine of x, where x is in radians.

 sin\(x\)          Return sine of x, where x is in radians.

 exp\(x\)          Return the exponential function of x.

 log\(x\)          Return the natural logarithm of x.

 sqrt\(x\)         Return the square root of x.

 int\(x\)          Truncate its argument to an integer. It will
                 be truncated toward 0 when x > 0.

 rand\(\)          Return a random number n, such that 0 < n  <
                 1.

 srand\([expr]\)   Set the seed value for rand to expr  or  use
                 the time of day if expr is omitted. The pre-
                 vious seed value will be returned.

It has been too long since I took trig to figure out the manipulation to do acos, but rewrite your formulats using only the above numeric functions.

Also remember, it does its calculations in radians, not degrees, so you have to adjust for that too.

Computing the inverse trig functions would be quite a chore. This really should be written in C. If you must use a script, maybe you can use a little C program to give you access to the C library? Here is a quick C program...

$ cat mathtool.c
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <stdio.h>
int main(int argc, char *argv[])  {
        double arg2,result;
        arg2=strtod(argv[2],(char **) NULL);
        if (!strcmp(argv[1], "asin")) {
                result=asin(arg2);
        } else if (!strcmp(argv[1], "asind")) {
                result=asin(arg2)*57.29577951308232087679815481410517033240547246656432154916024386120;
        } else if (!strcmp(argv[1], "acos")) {
                result=acos(arg2);
        } else if (!strcmp(argv[1], "acosd")) {
                result=acos(arg2)*57.29577951308232087679815481410517033240547246656432154916024386120;
        } else if (!strcmp(argv[1], "atan")) {
                result=atan(arg2);
        } else if (!strcmp(argv[1], "atand")) {
                result=atan(arg2)*57.29577951308232087679815481410517033240547246656432154916024386120;
        } else {
                printf("error \n");
        }
        printf("%-20.6f \n", result);
        exit(0);
}
$
I-search:
$ gcc mathtool.c -l m -o mathtool
$ ./mathtool asin 1.0
1.570796
$ ./mathtool asind 1.0
90.000000

I only put in asin, acos, and atan, but you could add in the regular trig functions too. I also put in asind, acosd, and atand which converts the answer to degrees (not degrees, minutes, seconds).

If you have Python, here's an alternative:

from math import sin , cos, acos
A = 34.16597 
B = -84.33244
C = 34.2344 
D = -84.29189
result = ( sin(A) * sin(C) )+  ( cos(A) * cos(C) * cos(B-D) )
if A == C or B == D:
    print "The distance is zero"
elif result > 1:
    print "The distance is " , 3963.1 * acos(1) 
else:
    print "The distance is " , acos(result)

output:

# ./test.py
The distance is  0.0782622048633