Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8552387
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T14:24:47+00:00 2026-06-11T14:24:47+00:00

I have a long-established routine written in Visual C++ running on PC which calculates

  • 0

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).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T14:24:48+00:00Added an answer on June 11, 2026 at 2:24 pm

    In the C++ code

    cos(m_dLatRadians) * cos(dLatRadians)
    

    should be

    cos(dLatRadians) * cos(dLatRadians)
    

    to be consistent with r2 = r3 * r3; and match what you are doing in Java.

    Also, as @CaseyB points out, the dLatRadians parameters are different (as are m_dLatRadians).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have long-lasting TCP connection between two computers (second not under my control). Second
Currently I have Long Pres Gesture Recognizers on four different TableViews (two in each
One thing with which I have long had problems, within the CakePHP framework, is
Let's say I have a long established repository like this: interface IDonutRepository { public
We have a long established, greatly multiplatform codebase that is currently being ported to
I have long cycles of GC. from checks I saw there are too many
Apple have long blocked mobile Safari from playing sounds – the usual JavaScript commands
I have long HTTP request ( generating large Excel file - about 60K records
I have long file with the following list: /drivers/isdn/hardware/eicon/message.c//add_b1() /drivers/media/video/saa7134/saa7134-dvb.c//dvb_init() /sound/pci/ac97/ac97_codec.c//snd_ac97_mixer_build() /drivers/s390/char/tape_34xx.c//tape_34xx_unit_check() (PROBLEM)/drivers/video/sis/init301.c//SiS_GetCRT2Data301() /drivers/scsi/sg.c//sg_ioctl()
How do you manage versions of work flows in WF when you have long

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.