Re: input out of error with haversine formula

From: Dean Rasheed <dean(dot)a(dot)rasheed(at)gmail(dot)com>
To: Vince Carney <vincecarney(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: input out of error with haversine formula
Date: 2010-10-15 07:08:05
Message-ID: AANLkTi=mx=2E90UL7v=SqDBtTn-c7WRHzEzyA1CfArUv@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 15 October 2010 06:03, Vince Carney <vincecarney(at)gmail(dot)com> wrote:
> The following will return an input out of error as the acos() function
> cannot be -1 <= x <= 1.
> SELECT * FROM
>                 (SELECT *, (3959 * acos(cos(radians(37.7438640)) *
> cos(radians(37.7438640)) * cos(radians(-97.4631299) -
>                 radians(-97.4631299)) + sin(radians(37.7438640)) *
> sin(radians(37.7438640))))
>                 AS distance
>                 FROM foo) AS distances
>                 WHERE distance < 10
>                 ORDER BY distance
> If I break this down the following returns 1:
> SELECT (cos(radians(37.7438640)) * cos(radians(37.7438640)) *
> cos(radians(-97.4631299) - radians(-97.4631299)) + sin(radians(37.7438640))
> * sin(radians(37.743864000)));
> acos(1) would give me 0.
> Thoughts or workaround suggestions?
> Thanks.
> --Vince--

This form of the Haversine formula is known to suffer from large
rounding errors when the distance between the points is small. It is
much better to use the arcsin(..) form of this formula, which has much
greater accuracy, particularly for the common case of small distances.
See http://en.wikipedia.org/wiki/Great-circle_distance

Regards,
Dean

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Martial Braem 2010-10-15 07:36:09 Strange phenomenon
Previous Message Nick 2010-10-15 06:55:08 Re: It it possible to get this result in one query?