I have a long-established routine written in Visual C++ running on PC which calculates the distance between two points on the earth’s surface given their lat/long coordinates.
I have converted this to Java as part of a port to Android, but I am getting different results. To investigate the reason for this, I have split a complex trig equation into bits to track what is happening at each step.
However, in Visual C++ the result of the step by step calculation is different from the result obtained from the complex statement, although they should obviously be the same. I would like to know why!
The Java versions yields the same result in both cases, as would be expected.
Here is the Visual C++ code:
double CCoord::GetDistanceRadians(double dLatRadians, double dLongRadians)
{
double dResult;
// This is the step by step code
double r1, r2, r3, r4, r5;
r1 = sin(m_dLatRadians);
r2 = sin(dLatRadians);
r3 = cos(dLatRadians);
r4 = m_dLongRadians - dLongRadians;
r5 = cos(r4);
r1 *= r2;
r2 = r3 * r3;
r2 *= r5;
r4 = r1 + r2;
// This is the original statement: r4 and dResult should be equal
// (but they aren't)
dResult = sin(m_dLatRadians) * sin(dLatRadians) +
cos(m_dLatRadians) * cos(dLatRadians) * cos(m_dLongRadians - dLongRadians);
// Protect against maths processor inaccuracies
if ((dResult > 1.0) || (dResult < -1.0))
dResult = 0.0;
else
dResult = acos(dResult);
return dResult;
}
And here is the Java code:
// Return angular distance in radians
private double getDistanceRadians(double dLatRadians, double dLongRadians)
{
double result;
// Do JIT test
checkRadianValues();
// This is the step by step code
double r1, r2, r3, r4, r5;
r1 = Math.sin(m_dLatRadians);
r2 = Math.sin(dLatRadians);
r3 = Math.cos(dLatRadians);
r4 = m_dLongRadians - dLongRadians;
r5 = Math.cos(r4);
r1 *= r2;
r2 = r3 * r3;
r2 *= r5;
r4 = r1 + r2;
// This is the original statement: r4 and result should be equal
// (and they are)
result = Math.sin(m_dLatRadians) * Math.sin(dLatRadians) +
Math.cos(dLatRadians) * Math.cos(dLatRadians) *
Math.cos(m_dLongRadians - dLongRadians);
// Protect against maths processor inaccuracies
if ((result > 1.0d) || (result < -1.0d))
result = 0.0d;
else
result = Math.acos(result);
return result;
}
These are the input and result values:
Variable VC++ value Java value
m_dLatRadians 0.896808347 0.896808378
m_dLongRadians -0.047414778(0) -0.047414778(6)
dLatRadians 0.896192633(9) 0.896192588(9)
dLongRadians -0.026897463 -0.026897463
r4 1.000218106 1.0002181433
dResult/result 0.999917766 1.0002181433
Please can anyone suggest why r4 and dResult should be different in the Visual C++ version?
P.S. I get good final distance results from the VC++ version, and bad final results from Java – nearly all distances come out as zero (the higher level code which calls these routines is not shown because there is a lot of it! However it’s the discrepancy I am interested in finding out about).
In the C++ code
should be
to be consistent with
r2 = r3 * r3;and match what you are doing in Java.Also, as @CaseyB points out, the
dLatRadiansparameters are different (as arem_dLatRadians).