Open the context
Choose the project folder and name the files or behavior involved.
Return the central angle in radians between two unit-sphere positions with latitudes within ±90 and longitudes within ±180 degrees. Test coincident and antipodal points, rejecting nonfinite coordinates rather than assuming an Earth radius.
Choose the project folder and name the files or behavior involved.
Describe the input, expected output and boundaries that must stay intact.
Review the proposed edits and any commands before relying on their result.
Compare actual behavior with the expected result. If a check fails, investigate it before applying more changes.
from math import asin, cos, isfinite, radians, sin, sqrt
def central_angle(lat1, lon1, lat2, lon2):
values = (lat1, lon1, lat2, lon2)
if any(type(x) not in (int, float) or not -limit <= x <= limit
or not isfinite(x) for x, limit in zip(values, (90, 180, 90, 180))):
raise ValueError("bounded finite coordinates")
p, q = radians(lat1), radians(lat2)
a = sin((q-p)/2)**2 + cos(p)*cos(q)*sin(radians(lon2-lon1)/2)**2
return 2 * asin(sqrt(min(1.0, max(0.0, a))))The example is a starting point for a real project, not a claim that generated code is already tested. Adjust paths, dependencies and expected behavior to match your application.