Skip to content

Haversine distance may be using the wrong phi1 and phi2 angle #11308

Closed

Description

@lucasalavapena

Repository commit

2e405f3

Python version (python --version)

NA

Dependencies version (pip freeze)

NA

Expected behavior

First of thanks for all the algorithms and the work!

The current (and initial) implementation of the haversine distance is using reduced latitudes (as one does for Lambert's formula see https://en.wikipedia.org/wiki/Geographical_distance#Lambert's_formula_for_long_lines), however, from my understanding the haversine distance should be using the raw latitudes (in radians like the longitudes are currently being handled). At least that is how it is written in https://en.wikipedia.org/wiki/Haversine_formula#Formulation .

The haversine distance from my understanding is supposed to work on a sphere, however, the notion on reduced latitude and flattening factors exist to deal with the non-spherical nature of the Earth (see https://en.wikipedia.org/wiki/Flattening). I am by no means an expert, I could very well be wrong.

Moreover, reading the wikipage and other implementations the transformation to a reduced latitude is not needed for the haversine distance, the latitude is used directly. Here are some references:

additional implementations:

I think most likely there was some confusion with Lambert's formula.

I am happy to make a small PR, if there is an agreement with my observation.

Actual behavior

Use the right angles and the results should slightly be different. I did some test, it is not that significant.

Activity

  1. Maanit491 commented on Oct 1, 2024

    @Maanit491

    The issue is resolved by me, please review my PR :)

  2. Maanit491 commented on Oct 9, 2024

    @Maanit491

    Kindly review my PR @lucasalavapena

  3. Asukasuk commented on Dec 14, 2025

    @Asukasuk

    Haversine Distance Calculation Issue
    ​The second image describes an issue where the implementation of the Haversine distance seems to be using "reduced latitudes" or may be confusing the Haversine formula with the Lambert formula.
    ​The user's main concern is: Haversine distance may be using the wrong phi1 and phi2 angle.
    ​Understanding the Formulas
    ​The Haversine formula is used to calculate the great-circle distance between two points on a sphere (like the Earth) given their longitudes and latitudes.
    ​The standard Haversine formula for distance d between two points (\phi_1, \lambda_1) and (\phi_2, \lambda_2) on a sphere of radius R is:
    a = \sin^2\left(\frac{\phi_2 - \phi_1}{2}\right) + \cos(\phi_1) \cos(\phi_2) \sin^2\left(\frac{\lambda_2 - \lambda_1}{2}\right)
    c = 2 \arctan2(\sqrt{a}, \sqrt{1-a})
    d = R \cdot c
    Conclusion and Next Steps
    ​If the code is implementing the Haversine distance based on the assumption of a perfect sphere (which is what the Haversine formula is for), reduced latitudes are not needed. The use of reduced latitudes would only be necessary if the calculation were accounting for the Earth's flattening (an ellipsoidal shape).
    ​If you are the developer or are responding to the user, here is the suggested action:
    ​Verify the Formula: Check the source code to see if it is using \phi (geodetic latitude) or \beta (reduced latitude) in the trigonometric calculations.
    ​Confirm the Model: If the formula is intended for an ellipsoid (flattened Earth), then using a method like Vincenty's or calculating the reduced latitude might be appropriate, but that would mean the formula is not a pure Haversine implementation.
    ​Correct the Latitudes: If the goal is a standard Haversine calculation on a sphere, ensure the input latitudes (\phi_1 and \phi_2) are used directly.

  4. added 2 commits that reference this issue on Mar 8, 2026
    9fae7d9
    635e9ca
  5. cclauss commented on Sep 10, 2026

    @cclauss
    Member

    @priya-sundaram-dev wrote:

    Legitimate: The current code converts to reduced/geocentric latitude via atan((AXIS_B/AXIS_A)·tan(lat)), but standard haversine uses geographic latitude directly (per the reporter's cited references).

    Two competing open PRs, and the sticking point is the Earth-radius constant:

    I'd settle the radius convention first (mean radius is the usual haversine choice), then take whichever PR is rebased to it.

  6. added a commit that references this issue on Sep 12, 2026
    12d0648
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions