Skip to content

Commit 8bf4ced

Browse files
IlyaOrsonKristofferC
authored andcommitted
Use a more performant formula for haversine (#90)
* Use a more performant formula for haversine ```julia using BenchmarkTools Main> using BenchmarkTools Main> a = rand() 0.8809105663628993 Main> @benchmark asin(sqrt($a)) BenchmarkTools.Trial: memory estimate: 0 bytes allocs estimate: 0 -------------- minimum time: 15.790 ns (0.00% GC) median time: 16.579 ns (0.00% GC) mean time: 17.691 ns (0.00% GC) maximum time: 712.929 ns (0.00% GC) -------------- samples: 10000 evals/sample: 1000 Main> @benchmark atan2(sqrt($a), sqrt(1-$a)) BenchmarkTools.Trial: memory estimate: 0 bytes allocs estimate: 0 -------------- minimum time: 24.080 ns (0.00% GC) median time: 24.870 ns (0.00% GC) mean time: 25.987 ns (0.00% GC) maximum time: 417.257 ns (0.00% GC) -------------- samples: 10000 evals/sample: 1000 Main> asin(sqrt(a)) ≈ atan2(sqrt(a), sqrt(1-a)) true ``` * Take minimum after square root in haversine
1 parent 9c4f1bc commit 8bf4ced

File tree

1 file changed

+1
-4
lines changed

1 file changed

+1
-4
lines changed

src/haversine.jl

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,8 @@ function evaluate(dist::Haversine, x::VecOrLengthTwoTuple, y::VecOrLengthTwoTupl
2929
# haversine formula
3030
a = sin(Δφ/2)^2 + cos(φ₁)*cos(φ₂)*sin(Δλ/2)^2
3131

32-
# take care of floating point errors
33-
a = min(a, one(a))
34-
3532
# distance on the sphere
36-
2*dist.radius*atan2(a, (1-a))
33+
2 * dist.radius * asin( min(a, one(a)) ) # take care of floating point errors
3734
end
3835

3936
haversine(x::VecOrLengthTwoTuple, y::VecOrLengthTwoTuple, radius::Real) = evaluate(Haversine(radius), x, y)

0 commit comments

Comments
 (0)