• ray10k (unregistered)

Fairly obvious indeed: Lat/long are not coordinates on a plane, but on a sphere. In short, the closer you get to the poles, the shorter the distance between two points on the same... I want to say longitude.

• domin (unregistered)

polar coordinates, anyone?

• BOM (unregistered)

If memory serves, they want the Haversine formula for a fairly good approximation across the world. I've spent too long working with map APIs :-(

• Quite (unregistered) in reply to ray10k

... and besides, what are the units in? If lat/long are in degrees, then ... never mind.

We had an interesting case of a complete inability to undertstand the problem domain here once, where we had to implement a currency converter. The specific exchange rate you were to use depended on whether you were buying or selling the currency. But the clown implementing it thought that the conversion factor (from e.g. Euros to Dollars) to sell e.g. the Euros was the reciprocal of the conversion factor (from the same e.g. Euros to Dollars) to buy the Euros. Looked at me completely blankly when I questioned him, and asked him why he had not sought out the specification before starting the job.

• denziloe (unregistered)

okay, it's an (understandable) maths whoopsy, but not really a major code wtf and it was apparently easy enough to track down the bug and fix it

• Blerg (unregistered) in reply to denziloe

The major wtf is that the dev didnt take 5 minutes to look up how to implement (or just find a working example of) the Haversine formula for distance between latLngs.

• PJH (nodebb)

Another vote for TRWTF being: not knowing about haversines.

• CarrieVS (nodebb)

Back when I did web-dev, I had to implement the redesign of some pages which could calculate the distance from the visitor's location (if permitted) to a branch location specified by latitude and longitude. Luckily for me, I was supposed to re-implement the existing functionality on the shiny new design, not implement properly working functionality.

I never actually thought about whether we were doing this or not. Turns out we are doing it properly though since anyone who actually wants to get there is going to be starting from within the UK and almost certainly within a few miles, the difference is probably not very significant. Especially not compared to the difference between our as-the-crow-flies calculation and the actual distance by road which the potential customer will care about.

• Cartographer (unregistered)

Wow. As others have mentioned, the real WTF is not using some form of ellipsoid-based algorithm. Flat-earth Pythag equations are going to be very wrong in many circumstances. Haversine is a fine approximation of reality for things not needing a huge degree of accuracy. If this app is simply telling users how far they are from a weather station, haversine will be good enough (you'll be within tens of meters of reality in most cases). If you need more accuracy - there are (ever) more complex algorithms that take into account the true(r) non-spheroidal nature of the earth.

• Steve_The_Cynic (nodebb) in reply to ray10k

@ray10k You might want to say longitude, but if you do, you'll be wrong.

Two points five degrees (of latitude) apart on the same longitude (e.g. 3°03′30″E) will be (subject to questions of the Earth being not-really-a-true-ellipsoid) be the same distance apart. You should say "same latitude", since lines of latitude have the same number of degrees of longitude on them, but progressively fewer miles as you go north.

@CarrieVS In the UK, the error of magnitude of the east/west component by using Pythag instead of Haversine (etc.) is in excess of 40%. (That is, the calculated distance is more than 40% bigger than the true distance, in the strictly east/west direction.)

Addendum 2016-12-06 08:17: That is, the points on the same longitude - separated by 5 degrees of latitude - will be the same distance apart no matter where they are.

• ray10k (unregistered) in reply to Steve_The_Cynic

Yeah, looked it up afterwards and I did indeed mix up longitude and latitude.

• CarrieVS (nodebb) in reply to Steve_The_Cynic

That's bigger than I had guessed, but our distances were very often shorter than the road distance by at least that proportion. In fact an over-estimate of 40% would probably have improved the accuracy at least as often as not.

• Helix (nodebb)

and not a single mention of great circle (orthodromic..?) distance

• Anonymous (unregistered)

Dunning-Kruger effect, I think. Who-ever wrote the Pythagoras solution wasn't merely ignorant of how to calculate distances on a round Earth, they were ignorant of their own ignorance. Maybe the lesson to take is that you should always learn more about the problem domain, even if you think you know it.

@Steve_The_Cynic and @CarrieVS , if your co-ordinates are latitude and longitude then indeed naively calculating the Pythagorean distance between them will be a big overestimate. If however they are British National Grid co-ordinates then the Pythagorean distance will be pretty accurate. Provided the input co-ordinates are accurate - taking a GPS latitude and longitude and naively converting that to national grid will be in error of tens of metres because GPS and national grid use different shapes for the Earth implying different lat-long co-ordinates for the same point.

Yeah, map projections are a rabbit hole.

• Khudzlin (nodebb) in reply to Helix

Not by name, but that's the one you get with the haversine formula.

• bvs23bkv33 (unregistered)

harvesine? L = R * arcos(sin(lat1) * sin(lat2) + cos(lat1) * cos(lat2) * cos(lon1 - lon2))

• Kashim (unregistered) in reply to Cartographer

As someone who works with GPS and similar things every day, I can tell you that if you hear the term "Geoid Undulation" while at work, you are about to have a very bad day.

However, I don't really view this as a true WTF. The programmer thought he saw the problem, and just didn't know a lot about... the earth, or physics, or cartography, or geometry...

Honestly, I could see a LOT of people making that same mistake, and in fact I am pretty sure that people around my work have in the past. We all spent so much of our childhoods working in nice flat coordinate systems that it is really annoying to realize that the universe so often doesn't work that way.

• Guest (unregistered) in reply to Kashim

"However, I don't really view this as a true WTF."

It is a true WTF... it is just not a programming WTF.

• Mark (unregistered)

If I don't see some Trig functions in the calculation, I assume it isn't right.

• Piohen (unregistered)

https://upload.wikimedia.org/wikipedia/commons/thumb/8/80/Bearing_and_azimuth_along_the_geodesic.png/220px-Bearing_and_azimuth_along_the_geodesic.png Or why some still live on flat Earth. ;-)

• Jeff Jefferson (unregistered) in reply to Quite

The problem there was the lack of specific requirements. If you don't know how currency conversion works then it is entirely reasonable to assume it works like any other conversions.

Here's my thing, though- this would be trivial to catch in testing. The fact that this was in the code and was in use implies that it was never actually tested.

• fyl2xp1 (unregistered)

Computing accurate distances on an ellipsoid's surface is quite hard: https://en.wikipedia.org/wiki/Geodesics_on_an_ellipsoid

A few years ago I tried to convert lan/lat-coordinates to x/y/z-cartesian ones and back. I was unable to find a non-iterative solution. Anyway, the geometric approximation converged quickly and seemed to be stable.

The main problem is, that lan/lat-values denote the surface's normal angle, rather than an angle measured from some cutting plane.

Depending on what accuracy you have to achieve, the haversine formula is definitively out of discussion. This one should yield better results: https://en.wikipedia.org/wiki/Vincenty%27s_formulae

Nevertheless: the code shown in the article just hurts the area in my brain that has to handle basic math.

• Kashim (unregistered) in reply to Remy Porter

That's true, this would definitely be easy to catch in testing, if you have a strong test suite of any kind. And this would likely be caught by any other engineer who looked at it. However, if no one was looking closely, and no one was was testing cases that were terribly close to the poles (most likely everyone was in North America anyway)

I will accept that TRWTF is that no one looked at the code closely enough to catch this before it got into production, or that someone trusted this person enough to have them implement a core/important part of a system without them knowing at all what they were doing.

• fyl2xp1 (unregistered)

Some kind of a "fun-fact" regarding practical implications of ellipsoids vs. spheroids: https://en.wikipedia.org/wiki/Qibla#North_American_interpretations

• Kashim (unregistered) in reply to Kashim

Finish of the first paragraph: (most likely everyone was in NA anyway) then it likely would produce results at least somewhat close to the actual results. As long as you stay within one continent (that isn't Asia) you aren't going to get results more than a couple of miles off, and if you're just considering distance to the nearest NWS system, a couple of miles most likely doesn't make a huge difference.

• Smartass (unregistered)

How 'bout it's wrong because it's just that: wrong. I know nothing about properly calculating that distance, but the featured formula is showing details about only one end of the 2-point line, which just can't be enough to calculate that distance. The result of that formula probably doesn't have any meaning whatsoever.

• Publius (unregistered) in reply to Quite

Maybe he used to work for CityWide Bank: http://www.nbc.com/saturday-night-live/video/first-citiwide-bank-change-ii/n9703

"How do we make money? Simple. Volume."

• Alsot (unregistered)

That formula works if all the values are local, but if you're looking world wide, there are MANY problems. The biggest I can see is that lat/long values can be negative, but this treats them all as positive so points on opposite sides of the planet would be calculated as right next to each other.

"any number squared is positive, so you don't need abs()" .. Oh, yeah? What if the latitude is a complex value? What then, eh?

• Herby (unregistered)

Put this in the same category as time calculations. It ain't easy! Things get even more complex when you want to calculate intersecting lines on the surface of the earth. Two "straight" lines can even intersect TWICE if you have them in the right place. A program I worked on (Airplane navigation) needed to know if something intersected or not. My first test was using simple squares based on Lon & Lat. I figured if this intersected (the common case was that it didn't) I would take the high road and calculate the true place using all sorts of spherical and the like coordinates. If not, then skip it. I made the assumption that the lines didn't intersect twice which for the most part was OK in the cases I was getting around to.

The bigger problems happen when you go from geocentric coordinates (measuring from the center of the earth) to/from geodesic coordinates (down "points" to the center of the earth, and oblate spheroid things). Gets plenty confusing when you go from one place to another and things change. Ain't GPS wonderful??!!!

• JJ (unregistered)

Even a bunch of CERN physicists got this wrong. Remember the excited atatements about neutrinos travelling faster than light? Quickly retracted after a high school student pointed out neutrinos tend to iust dig through earth instead of taking the surface roads... :-)

• Worf (unregistered) in reply to Kashim

Note - Don't say "North America" when you mean "USA". Assuming the lower 48 is flat versus on a spheroid surface may not give you must deviation, but when you head north it really matters. Because in Canada it varies pretty significantly. Maybe not much when you're just going along the 49th parallel, but once you headed north a few hundred kilometers the error becomes big (and well, given the territory extends to the North Pole...).

It's probably also a big deal in Alaska...

• George Gonzalez (unregistered)

Um, this isn't even remotely correct, degrees do not convert to miles that way, and you are on a sphere, not a plane. A degree of latitude is a constant number of miles, but a degree of longitude is 25,000/360 only at the equator. You have to use a bit fancier formula to do real distances on a sphere and convert degrees to distance..

• Yahoo Dee Men You Win (unregistered) in reply to JJ

From what I recall, the super-luminal neutrino thing was actually a faulty optical fibre connection resulting in a degraded signal. Nothing to do with geographic distances.

• Khudzlin (nodebb)

@Smartass The calculation uses latitude difference (latdiff) and longitude difference (londiff). So both endpoints are used.

@Herby On a sphere, two "straight lines" always intersect twice (if you continue them long enough). That's because the analog of a straight line (shortest path) on a sphere is a great circle (circle with the same center and radius as the sphere), and any two of those intersect at antipodal points.

• Gordon JC Pearce (github) in reply to Worf

I worked with some severely WTFy software (it's worth a post in its own right, when I can repress the memories enough) that was written by a software company in Canada, and made a lot of assumptions based on those southerly latitudes. Anywhere in the UK, everything was really weirdly squashed. In Scotland the distortion was so severe that maps were drawn trapezoidally, and looked like sat nav "head up" maps.

• londiff on holiday (unregistered)

Does londiff wrap at +/-180 degrees? If not, the international date line is going to be much more fun than the poles.

• Khudzlin (nodebb)

The international date line doesn't strictly follow the 180° meridian. There are detours in the Bering strait (so the line goes between Siberia and Alaska), eastward around the Kiribati (to avoid the IDL crossing the country) and eastward around some islands belonging to New Zealand (same reason). The problem with wrapping would occur across the meridian, regardless of where the IDL is (fortunately, there isn't much land around it). Also, if londiff is more than 180°, it should be replaced by 360° - londiff to avoid going the long way around.

• > we had to implement a currency converter. (unregistered) in reply to Quite

At the wholesale level, the forward and reverse conversion rates are so close that, for many purposes, clients can and do use the reciprocal of the "buy" rate as the "sell" rate, or the reciprocal of the "sell" rate as the "buy" rate, or the "mid" rate as either the buy or sell rate.

I supplied treasury software for several years, and although our software supported buy, sell and mid rates, virtually all of our clients used only one rate.

(All clients always use the reciprocal of the transaction rate for some reports and projections, but that's a different requirement)

• Norman Diamond (unregistered)

(most likely everyone was in North America anyway)

Non-sequitur. Some parts of North America are as far north as the UK, which was already mentioned as having 40% difference from a flat-Earther's calculation. Some parts of North America are even farther north, making the programmer a suitable target for a poleish joke.

• Norman Diamond (unregistered)

Things get even more complex when you want to calculate intersecting lines on the surface of the earth.

It seems to me a line should intersect the surface of the earth twice, or possibly more depending on how it passes through mountains and valleys.

• Mike a Person (unregistered)

Fun happens with New Zealand or Taiwan that are moving fast enough on tectonic plates to make a difference when you calculate a distance to them from year to year.

From memory of oil survey navigation software, their map datums have a time variable in them too...

it would be enough to give delivery by GPS drone a bad name as it drops packages into the neighbour's pool rather than the front driveway...

• Graham Asher (unregistered) in reply to CarrieVS

For the UK or any similar-sized country, you can get results within one part in a thousand by converting from lat/long to Universal Transverse Mercator (or the Ordnance Survey GB projection for the UK), obtaining results in projected metres, then using Pythagoras on that. It's effectively the equivalent of measuring with a ruler on a topographic map.

• Khudzlin (nodebb)

Well, that conversion doesn't give equal weights to latitude and longitude (unless you're near the equator).

• Anon (unregistered)

When working for a defense contractor in the US I once had this argument with my boss for several days. She was insistent that her formula was right. I never could quite convince her, but she did finally give in and let me use my own formula in calculating the distance. What's worse, we were testing the validity of a missile defense system and this formula was being used to test the size and shape of the 'error ellipse' (ie, area in which there was a xx% chance of the missile hitting). They'd been using the Euclidean formula for testing for over 3 years. :(

• B.o.B. (unregistered) in reply to ray10k

It's FLAT.

• kmier (unregistered) in reply to ray10k

"lat is flat"

• Robert (unregistered) in reply to Anon

As a person with Asperger's syndrome, this is one of the things that piss me off the most about neurotypicals: their insistence that social rules (rank, protocol, etc.) trump the rules of mathematics.