0 Comments

My code is not working.

I want to calculate the distance between two points on the Earth’s surface based on their latitude and longitude using the haversine formula.

The haversine formula is a method to find the great-circle distance between two points on a sphere given their longitudes and latitudes.

from math import sin, cos, sqrt, atan2

earth_radius = 6373.0

latitude1 = 43.4395755
longitude1 = 23.2267012
latitude2 = 47.0566784
longitude2 = 14.5931164

delta_longitude = longitude2 - longitude1
delta_latitude = latitude2 - latitude1
angle_a = (sin(delta_latitude / 2))**2 + cos(latitude1) * cos(latitude2) * (sin(delta_longitude / 2))**2
angular_distance = 2 * atan2(sqrt(angle_a), sqrt(1 - angle_a))
distance = earth_radius * angular_distance

print("Result:", distance)

#Output:
#Result: 6176.896859755734

It returns the distance 6176.896859755734, but not 786 km. Why?

KoderShop Changed status to publish August 9, 2023