TheAlgorithms / Python

All Algorithms implemented in Python
https://the-algorithms.com/
MIT License
190.38k stars 45.04k forks source link

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

Open lucasalavapena opened 7 months ago

lucasalavapena commented 7 months ago

Repository commit

2e405f397bbcefccc470f215c7ff024875ef16c5

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.

Maanit491 commented 6 days ago

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