Talk:Haversine formula

From Wikipedia, the free encyclopedia
Jump to: navigation, search

Cos or sin in the first formula?[edit]

I undid the change from cos to sin. Cos is correct when the formula is expressed in terms of latitude.

NB in the general haversine formula given below under "law of haversines" for a spherical triangle sin is indeed correct. The point is simply that latitude is measured from the equator not the pole, so sin of the great-circle distance from the pole becomes cos(latitude). — Preceding unsigned comment added by Dgwsoft (talkcontribs) 11:51, 14 September 2011 (UTC)

This point is in fact explained under "law of haversines". I just want to make it clear why it should not be changed back. Dgwsoft (talk) 14:13, 14 September 2011 (UTC)

Why not give the simplification or link to it?[edit]

Just give the square box approximation formula as an alternative since many of the uses of haversine are to answer the 'show all locations with X miles of latitude/longititude Y'. The square implementation is just a table lookup based on longititude to set the size of the square (i.e., the number of degrees in the box width and height based on latitude).

Why atan2?[edit]

In the formula for d, you can use either arcsin or arctangent:

d = 2 R asin(√h) = 2 R atan2(√h, √(1−h))

The Census Bureau FAQ recommends using the atan2 function instead of the asin function, but I have to confess that I can't see why.

The stated explanation is that the asin function requires "bulletproofing" in order to check that √h doesn't exceed unity, while "no bulletproofing is needed for an inverse tangent", but this makes no sense to me: in the atan2 version, you still need to check that h doesn't exceed 1 or or the square root will fail.

(On the other hand, if your trig and square root functions operate on complex numbers, neither case will fail—you'll just get a small imaginary part, which you can ignore, if h slightly exceeds 1.)

Moreover, I just did a numerical check using the GNU C library math functions (which are usually pretty good) in double precision (~16 decimal places) on a Pentium (IV), comparing to an arbitrary-precision (50-decimal places) calculation, and both the asin and atan versions are equally (in)accurate. For h = 1-1e-13, both versions give an error of about 3.16e-7 compared to the correct result of 1.5707963267948966192313216916397514420985846996876 for d/2R.

So, I'm going to delete the recommendation to use arctangent, as a bit of "folk numerical analysis" that maybe was true for somebody's pocket calculator years ago but doesn't seem to have any relation to reality now. Please, feel free to correct me if I'm missing something.

—Steven G. Johnson 03:43, Apr 12, 2004 (UTC)

ATAN2 does not suffer from problems when the arc crosses the pole . The tangent function is discontinuous because it is defined as t = o/a. ATAN2 takes o and a as parameters and computes a result in such a way that the problematic division is never performed, incidentally mitigating floating point errors too. Consider yourself corrected. That said, haversine is a stone-age approximation for people who don't have computers and should never be used for anything that requires high precision over large distance. Yes ships use it to navigate but they make constant corrections and the close they get the smaller the absolute error.

—Peter Wone 11:02, May 5, 2011 (UTC) —Preceding unsigned comment added by Styphnate (talkcontribs)

I can't see what you're saying. Can you give a specific example? -- (talk) 01:04, 3 December 2011 (UTC)
Styphnate, it sounds like you are describing the advantages of atan2 vs. atan. Here, it is being used instead of asin, which has no division by zero. (Actually, computers with IEEE arithmetic can divide by zero just fine and get ±∞. The advantage of atan2 over atan is more that atan2 has an expanded range, from −π to π. However, that isn't relevant here since the arguments are always positive.)
Regarding the "stone age" nature of this formula, you should pause to consider the fact that spheres are used for other things in mathematics besides approximating planets. — Steven G. Johnson (talk) 21:17, 3 December 2011 (UTC)

What's the formula?[edit]

In one part of the article it is claimed that:

haversin(θ) = sin2(θ / 2)

While later on the article says:

the use of detailed printed haversine charts saved navigators from squaring sines and halving the result

which would imply that:

haversin(θ) = sin2(θ) / 2

So do you square an angle's sine and then halve the result, or halve the angle and then square its sine? --Delirium 08:51, Apr 12, 2004 (UTC)

The first formula is correct (see also the versine article for the etymology...the haversine is half the versine). I didn't write that sentence in the article, so I can only speculate that the author of that sentence meant the halving of the argument, not the result, or perhaps the doubling of the result. I've modified the article to clarify. —Steven G. Johnson 17:46, Apr 12, 2004 (UTC)
Apologies; that was truly idiotic of me. I also was the originator of the whole "haversin(d/R) = h, where h=etc." wording, which I see has been changed. I don't blame you; it was cumbersome. But I thought the replacement was rather confusing; so I've proposed yet another version. I know "right-hand-side" sounds amateurish: but I really think the goal should be to be as intelligible to all readers, even at the cost of amateurishness. Doops 05:31, 14 Apr 2004 (UTC) P.S. I also see that my 'rammify' has been altered, which I kinda expected because it's old-fashioned and eccentric: but it really is a perfectly sensible use of the word. Look it up. :)
I assume you mean 'ramify'. I'm familiar with the word, and ramify small errors would be appropriate to describe the errors branching into many different computations, but is not really the right connotation for magnifying them in a single computation. —Steven G. Johnson

Why solve for d?[edit]

OK, I've got a question: why solve for d at all? In order to calculate the distance directly by the equation given, you will have to take the square root of a number which is likely to be near one; then caculate the arcsine of that. Wouldn't it be much, much easier to have, along with your detailed, printed table of haversines, a detailed, printed table of archaversines? Doops 05:39, 14 Apr 2004 (UTC)

Of course. A detailed printed table for the haversine is a detailed printed table for the inverse just look up in the other column. As the article says, this saved navigators from squaring sines, computing square roots, etc. —Steven G. Johnson 05:56, Apr 14, 2004 (UTC)
>As the article says, this saved navigators from squaring sines, computing square roots, etc.
Exactly! It was that phrase of yours which led me to ask the question! :) Doops 06:14, 14 Apr 2004 (UTC)

RHS vs. LHS[edit]

I'm sorry to keep bringing this up; but I am really not happy with the language Let h denote haversin(d/R), given from above. &c. &c.

Since I'm obviously not the expert here, and since I don't want to be annoying, I'm not going to change it back again; but I really have to say that this phrasing seems counterintuitive to me. Although doubtless its a perfectly defensible statement mathematically, it does seem to be saying, in effect, "let h = the LHS" when what we want to say is "let h = the RHS". Now obviously RHS = LHS, so this perhaps shouldn't matter; but I'm worried that somebody might plug h=haversin(d/R) into the formulae on the next line, getting the perfectly pointless result d=dR/R.

It's not at all circular, since LHS = RHS. Because the equation relating the two was one line before the defnition of h, I find it hard to believe that the reader would not realize that they have to use the RHS to apply the equation. —Steven G. Johnson
Yes; but why? I'm sure people can figure it out; perhaps the con- argument is no big deal; but what's the pro- argument? I simply don't see the point in doing things in a non-intuitive way.
The subtly-weird statement, to me, is to explicitly define h as one "side" or the other of an equation where both sides are equal—h is just a number, not a symbolic expression. Given that number, and knowing the relevant relation h = haversin(d/R), you then want to find d. This is algebra, not a computer programming language; "=" is equality, not assignment. —Steven G. Johnson 00:03, Apr 15, 2004 (UTC)
Gosh, I have so many comments I don't know where to begin!
  • first of all, Stevenj, you haven't answered my question: what is the argument in favor of the current version?
  • secondly, perhaps you're right that explicitly defining h as the RHS is weird. But I think (see below) that the text as it stands right now implicitly "defines" "h" as the LHS.
  • right now, the text reads "let h denote sin2(θ/2)." "Denote" seems pretty much like assignment to me! That is to say, when I hear the word 'denote,' I imagine a little label pointing at the expression with the letter h written on it. Not a mathematical definition, but a mental one.
  • And there is a very real difference between the two sides of the equation here: the RHS (as you point out above) is simply a number, which we can calculate based on observable data; but the LHS isn't! Or at least we don't think of it that way — if we thought of it that way, then we'd have a useless tautology!
  • But I guess we could treat both sides as 'just a number'; I guess we could go whole hog and, after giving the equation, begin the paragraph by saying: "Call this quantity h. By solving the eqation above we can find the distance d in terms of h and R (the known radius of the earth), etc. etc." We'd refer to the equation as a value, then proceed in the next line to manipulate it again like an active equation; but at least this would make the definition of h RHS/LHS-neutral.
Anyway, sorry for worrying at this so much. I'm not trying to be obnoxious! I just really believe in making things as straightforward as possible. Doops 03:14, 15 Apr 2004 (UTC)

The only reason (correct me if I'm wrong) for using h at all is to make the formula less messy-looking; if we were willing to have a long, confusing formula we could just write (for the 3rd large-font equation) d=R archaversin (haversin Δ&phi etc. etc.) And perhaps we should just go with that! But if we really do want to use h for the sake of simplicity, I think things will simply be much more simple if we get people thinking of h as another name for the RHS. The present language may mean that to some people; but not, I think, to all.

(Also, regarding the second version of the solved-for-d equation: don't you think we should make more explicit how it was derived? I think the mention (now deleted) that the def'n of haversine was applied before the arcsine taken was useful. Isn't it better to show than to tell?)

At a certain point, adding verbiage to belabor the obvious makes things less clear. —Steven G. Johnson 19:45, Apr 14, 2004 (UTC)
That's certainly true (see my new comment below). Doops 03:14, 15 Apr 2004 (UTC)

Again, I'm not an expert: which means (on the one hand) that I should be humble in altering the article; but on the other, that I might have useful suggestions regarding how it will sound to the layman. Doops 18:35, 14 Apr 2004 (UTC)

Possible rearrangement[edit]

Hi. I've put up a possible rearrangement of the whole article at Talk:Haversine_formula/Alt

Some points about it:

  • The main goal was to arrange the article so that the novice outsider, upon clicking to it, could get a quick summary of the haversine formula as quickly and painlessly as possible.
  • All discussions about the technical aspects and rationale are moved to the second section, the first paragraph of which is intended for the general reader. The rest of the section (which I haven't presumed to write) would be more technical in nature, and could perhaps be in smaller type to make this distinction clear.
  • I've left the remainder of the article unchanged.

Comments? If you like this ordering, please work on that second section (beyond the first paragraph) so that there's a logical progression! Thanks— Doops 03:14, 15 Apr 2004 (UTC)

Suggestion to add the precise formula for the earth[edit]

The article states, that haversine is not 100% accurate for the planet earth due to the fact that it's an ellipsoid. couldn't the article link to an entry with the correct formula or at least the name of the formula to be used for the earth? —Preceding unsigned comment added by (talk) 00:29, 19 August 2008 (UTC)

Useful to add mention of Kahan's formula?[edit]

Just in case anybody is tempted to do a straight computer implementation of the haversine formula, with its large numerical errors near the antipodal point, maybe it could be useful to add a mention of another algorithm by William Kahan that addresses that issue? His somewhat roundabout description is at Terry0051 (talk) 23:29, 2 March 2009 (UTC)

Discussion of alternative formulas for great-circle distances belongs on Great-circle distance, not here. —Steven G. Johnson (talk) 02:45, 3 March 2009 (UTC)
It's certainly surprising if a discussion of the material in this very article belongs only somewhere else: the formula previously mentioned is effectively a recasting of the formula that is the subject of this article, providing the same result while avoiding the numerical drawback.Terry0051 (talk) 16:47, 4 March 2009 (UTC)
All great-circle formulas are a recasting of the haversine formula, because all of them are equivalent in exact arithmetic. We have a whole article on great-circle formulas; we shouldn't be duplicating that material here when we can just link to that article. —Steven G. Johnson (talk) 17:30, 4 March 2009 (UTC)

Geometric mean?[edit]

The article suggests using the geometric mean of the earth's equatorial and polar radii. There are two problems with this: First, it matters very little which mean is used for two numbers so close in value. The difference between the geometric and arithmetic means is only about 9 meters. But using either unweighted mean ignores the fact that much of the earth's surface is closer to the equator than to the poles. The average over the entire surface is about 2/3 a + 1/3 b, where a is the equatorial radius and b the polar radius, or a(1-1/3f), where f is the flattening. Using the WGS84 numbers, this is 6371008.771 meters, which for all practical purposes can be rounded to 6371 kilometers. —Preceding unsigned comment added by (talk) 13:25, 6 May 2009 (UTC)

At the very least the topic should link to the Earth radius topic, where common Earth radii values are explained. KAW 24 (talk) 16:29, 8 February 2010 (UTC)

Why not delete the "(in radians)" part[edit]

There are no factors of Pi in the equation so it should work perfectly well if your latitude and longitude are in degrees. Just don't mix radians and degrees in the same sin or cos.

Probably the "in radians" was because the formula also includes haversine(d/R), which is only correct if the argument to the haversine function is taken to be in radians. — Steven G. Johnson (talk) 04:02, 5 August 2009 (UTC)

How to use Haversine to compute distance and angle between two points?[edit]

If I have two points defined as Lat1, Long1 and Lat2 and Long2, where these numbers are in terms of degrees of latitude and longitude (in terms of decimal degrees, not DMS) then how would I use the Haversine forumula to compute the distance between the two points, and the angle (or heading) of point 2 relative to point 1. For example, if the first point was the location of my TV antenna, and the second point was the location of a TV transmission tower (50 to 150 miles away), then I want to know the distance and also the direction to point my antenna. (I know that TV does this, but I want to compute the heading or direction manually). — Preceding unsigned comment added by (talk) 16:58, 25 May 2013 (UTC)