Note that there are some explanatory texts on larger screens.

plurals
  1. POInconsistent result with VC++ versus Java
    text
    copied!<p>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.</p> <p>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.</p> <p>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!</p> <p>The Java versions yields the same result in both cases, as would be expected.</p> <p>Here is the Visual C++ code:</p> <pre><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 &gt; 1.0) || (dResult &lt; -1.0)) dResult = 0.0; else dResult = acos(dResult); return dResult; } </code></pre> <p>And here is the Java code:</p> <pre><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 &gt; 1.0d) || (result &lt; -1.0d)) result = 0.0d; else result = Math.acos(result); return result; } </code></pre> <p>These are the input and result values:</p> <pre><code>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 </code></pre> <p>Please can anyone suggest why r4 and dResult should be different in the Visual C++ version?</p> <p>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).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload