Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use hypot in haversine calculation for more floating-point safety #266

Closed
wants to merge 1 commit into from

Conversation

asinghvi17
Copy link

hypot(x, y) is sqrt(x^2 + y^2) but implemented in an error-corrected way. It is a bit slower though, so depends on the tradeoff you want to make. Feel free to accept or reject.

hypot(x, y) is sqrt(x^2 + y^2) but implemented in an error-corrected way.  It is a bit slower though, so depends on the tradeoff you want to make.  Feel free to accept or reject.
@@ -24,10 +24,11 @@ function (dist::Haversine)(x, y)
Δφ = φ₂ - φ₁ # latitudes

# haversine formula
a = sind(Δφ/2)^2 + cosd(φ₁)*cosd(φ₂)*sind(Δλ/2)^2
# hypot(x, y) is equivalent to sqrt(x^2 + y^2)
sqrt_a = hypot(sind(Δφ/2), cosd(φ₁)*cosd(φ₂)*sind(Δλ/2))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is incorrect. The cosd coefficients are not squared, it's just the sind term.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah whoops, I missed that somehow! This is probably not very useful then :).

@asinghvi17 asinghvi17 closed this Oct 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants