3

I am kinda new on this. I am about to convert math equation to code lately. So I started practicing on simple formulas such as Fibonacci numbers and haversine formula.

But I am kinda confused with the haversine code on the following references;

Generally, what they do is,

var R = 6371e3; // metres
var φ1 = lat1.toRadians();
var φ2 = lat2.toRadians();
var Δφ = (lat2-lat1).toRadians();
var Δλ = (lon2-lon1).toRadians();

var a = Math.sin(Δφ/2) * Math.sin(Δφ/2) +
        Math.cos(φ1) * Math.cos(φ2) *
        Math.sin(Δλ/2) * Math.sin(Δλ/2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));

var d = R * c;

And I have the reference of original haversine formula from here.

Then, notice of this line,

var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));

Which part of it that comes from original haversine formula that is in wikipedia? I haven't found any Tangent at all on those graphs. Am I missing something here? Any suggestion is appreciated.

Community
  • 1
  • 1
choz
  • 17,242
  • 4
  • 53
  • 73

2 Answers2

5

By definition

hav(θ)=sin2(θ/2)

If hav(θ)=a, then

θ = 2 arcsin(sqrt(a))

After expressing arcsine with arctangent, this becomes:

θ = 2 arctan(sqrt(a)/(sqrt(1-a)))


UPDATE Clarification in response to OP's comment:

The haversine function hav(θ) for some angle θ is a shorthand for sin2(θ/2). Given two points on a sphere and θ being the flat angle between radii connecting those points with the center of the sphere, the haversine formula expresses the haversine function with the lattitude (φ) and longitude (λ) values of those points. Thus, you can calculate the value of the haversine function (which is represented in your code by the variable a) through known values φ and λ. Then you can find from a the value of θ (which is represented in your code by the variable c). My explanation above focuses exactly on that part. Then with a known value of θ (and the radius R) you can compute the great-circle distance d.

Leon
  • 31,443
  • 4
  • 72
  • 97
  • Thanks for your answer. But I believe that `hav(θ)=sin2(θ/2)` is acting as haversine function which is in `Math.sin(Δφ/2) * Math.sin(Δφ/2)` or `Math.sin(Δλ/2) * Math.sin(Δλ/2)` in the sample when assigning variable to `a`. And it's not where the `atan` comes from. – choz Jul 11 '16 at 09:14
  • @choz I added a clarification to my answer – Leon Jul 11 '16 at 09:42
  • I get it now. Why would everything have to be complicated when you can just say 'Oh.. the doc says to solve for d by applying the inverse haversine (if available) or by using the arcsine (inverse sine) function and by the way `Math.asin(Math.sqrt(a)) = Math.atan2(Math.sqrt(a), Math.sqrt(1-a));`'... Thanks anyway :) – choz Jul 11 '16 at 10:00
  • Do programming solutions use atan2 for better numerical accuracy, or is it just cargo-culting? – OrangeDog Jun 13 '23 at 15:31
2

It's just another formulation of the Haversine formula.

From https://en.wikipedia.org/wiki/Inverse_trigonometric_functions#Relationships_among_the_inverse_trigonometric_functions:

arctan(x) = arcsin(x / sqrt(x^2 + 1))

With X = x / sqrt(x^2 + 1), you end up with:

x = X / sqrt(1 - X^2)

Hence:

arcsin(X) = arctan(X / sqrt(1 - X^2))

Then in the Haversine formula from Wikipedia:

d = 2R * arcsin(sqrt(a))
  = 2R * arctan(sqrt(a) / sqrt(1 - a))
maxdec
  • 5,707
  • 2
  • 30
  • 35