The dirty little secret about ACOS is that it's inherently inaccurate. In
certain regions, the arc-cosine function is *extremely* sensitive to *tiny*
variations in the numbers, variations such as the ones inherent in
representing floating-point numbers inside computers.
AFAIK, MapInfo merely passes their ACOS function on to the acos() that comes
with the C compiler they build MapInfo with. It's unreasonable to expect
anything more accurate without going to (non-standard) higher precision
numbers, and that would make your programs run a LOT slower.
Since it's possible to algebraically derive a formula for the sine function
from the cosine function, you can use the ATAN2 function to get the azimuth.
That way, you're balancing out the inaccuracy in the sine function against
the inaccuracy in the cosine.
(ATAN2 calculates the direction of a 2-D vector given its two scalar
components, and the sine and cosine are the components of the great circle's
direction vector at the starting point).
Yes, the C compiler's builtin ATAN2 function really should be exposed in
MapInfo/MapBasic, since it's table-based and likely to be more accurate than
anything you can whip up in MapBasic. You can use your own C compiler to
make a DLL that exposes that compiler's ATAN2, and then reference the DLL in
your MapBasic program. However, you still get more accuracy by creating a
MapBasic ATAN2 from ATN than you'd get with just the cosine formula.
IF you use
LAT0 = start latitude
LON0 = start longitude
LAT1 = stop latitude
LON1 = stop longitude
DIST = angular distance
DLON = LON1-LON0
Sin AZIMUTH = sin (DLON) * cos LAT0 / sin DIST
Cos AZIMUTH = ( cos LAT1*sin LAT0 - sin LAT1*cos LAT0*cos DLON ) / sin DIST
which would mean
AZIMUTH = ATAN2 (
sin DLON*cos LAT0,
cos LAT1*sin LAT0 - sin LAT1*cos LAT0*cos DLON
)
Of course, this only works on the sphere: Things become a LOT more
complicated on an ellipsoid. Some of you may remember that I converted some
FORTRAN code for ellipsoidal navigation to C++ (downloaded from NIST IIRC).
There was a paper (written by the Air Force for targeting ICBMs) that
accompanied it. Unfortunately, the azimuth function was much simplified from
the formulae in the paper, and was correspondingly less accurate.
The old USGS bulletin 1532 by the late John P. Snyder explains the math a
lot better than his later publication Map Projections - a Working Manual"
(USGS Professional Paper 1395)
If you want higher-precision (or even arbitrary-precision) floating-point
math, there is certainly software out there to do it. But then the
calculations will be done in software, and not the processor, so it's going
to be slow.
Spencer
-----Original Message-----
From: mapi...@googlegroups.com [mailto:mapi...@googlegroups.com] On
Behalf Of Glen
Sent: Monday, February 04, 2008 5:45 PM
To: MapInfo-L