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

  • Home
  • SEARCH
  • 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 7610871
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T01:31:25+00:00 2026-05-31T01:31:25+00:00

I have a strange problem using the Cosine formula in my PHP application.. function

  • 0

I have a strange problem using the Cosine formula in my PHP application..

function CalculateDistanceCosine($decA, $decB)
{
    $lon1 = $decA[0]; //This would be equal to point A's longitude, and so on..
    $lat1 = $decA[1]; 
    $lon2 = $decB[0];   
    $lat2 = $decB[1];

    //echo $lon1." ".$lat1."<br/>";
    //echo $lon2." ".$lat2."<br/>";

    $distance  = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($lon2-$lon1));
    $distance  = acos($distance);
    $distance  = rad2deg($distance);
    $distance  = $distance * 60 * 1.1515;
    $distance  = round($distance, 4);

    return $distance;   
}

My input into this would be something like this:

45.468055555556 -73.741388888889 //- The coordinates for Montreal International Airport
28.428888888889 -81.315833333333 //- Orlando International Airport

However, upon using it, I get wild mistakes.. i.e, “The distance from Montreal to Orlando is 576 KM — very wrong.”

What’s interesting is that it is very accurate along the longitude axis. For example, if I gave an input of:

50 -73.741388888889 
50 -81.315833333333 

The error is now only about 50KM, very acceptable.

In other words, why is it neglecting latitudinal differences?

I’ve tried the Harvesine formula with similar results unfortunately.

  • 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-05-31T01:31:27+00:00Added an answer on May 31, 2026 at 1:31 am

    I believe your main problem is that you’re getting latitude confused with longitude. You’ve not shown us the code that’s wrong, but bear in mind that in your arrays, you would need the longitude first, followed by the latitude, for your function to work. In the example you’ve given, you’ve listed the latitude first, followed by the longitude.

    Your code would certainly work out the distance between those two points as written as 576 (in units I’ll mention in a minute), but those points aren’t actually where you think they are 🙂 Try changing your function as follows:

    $lat1 = $decA[0]; //This would be equal to point A's *latitude*, and so on..
    $lon1 = $decA[1]; 
    $lat2 = $decB[0];   
    $lon2 = $decB[1];
    

    …or just pass in the values in the expected order.

    Also, I don’t recognise your distance multiplier, but it looks like you might be calculating your result in miles, not kilometres. For kilometres, try:

    $distance  = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($lon2-$lon1));
    $distance  = acos($distance);
    $distance  = $distance * 6372.8;
    $distance  = round($distance, 4);
    

    (It’s only skipping the conversion back to degrees and the multiplier that’s changed, the basic formula remains the same.)

    Given the above changes, your distance works out at around 2,008 kilometres. Is that about right? I’m afraid I’ve never visited Montreal, Orlando, or anywhere in between…

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

Sidebar

Related Questions

I've got this strange problem using py-amqp and the Flopsy module. I have written
I have very strange problem. I'm using IIS 7.0 Integrated mode for my application.
I seem to have a strange problem: I coded an application in C++ (using
I have strange problem with the delay function here using the HTML function with
I have a strange problem with a calculator made using boost::spirit. This calculator is
I have a strange problem with lxml when using the deployed version of my
I have a strange problem in Visual Studio 2010 (C#) using the Report Viewer
I have a strange problem with a DataGridView I'm using which is bound to
I have a strange problem: I am getting the html source from url using
I have a strange problem using a MapView in Android. It works fine until

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.