Thread: BUG #4872: Geometric function problem

BUG #4872: Geometric function problem

From
"Nick Roosevelt"
Date:
The following bug has been logged online:

Bug reference:      4872
Logged by:          Nick Roosevelt
Email address:      nroose@thepinc.com
PostgreSQL version: 8.2.5
Operating system:   Linux
Description:        Geometric function problem
Details:

I am getting bad results for distance between point and lseg.  As you can
see below, the first result is correct, and the second is clearly not.


nroose_dev=> select version();
                                                  version
----------------------------------------------------------------------------
-------------------------------
 PostgreSQL 8.2.5 on x86_64-unknown-linux-gnu, compiled by GCC gcc (GCC)
4.1.2 20070626 (Red Hat 4.1.2-13)
(1 row)

nroose_dev=> select point(0,0) <-> lseg(point(10,-100),point(10,450));
 ?column?
----------
       10
(1 row)

nroose_dev=> select point(0,0) <-> lseg(point(10,-100),point(11,450));
     ?column?
------------------
 100.498756211209
(1 row)

nroose_dev=>

Re: BUG #4872: Geometric function problem

From
Tom Lane
Date:
"Nick Roosevelt" <nroose@thepinc.com> writes:
> I am getting bad results for distance between point and lseg.  As you can
> see below, the first result is correct, and the second is clearly not.

Hmm ... what it looks like to me is that there's an ancient thinko
in dist_ps_internal().  It's trying to calculate the slope of the
perpendicular to the given line segment, and it gets it wrong.
The segment's own slope would be deltaY / deltaX, so the slope
of the perpendicular should be the negative inverse of that, ie
-deltaX / deltaY, but what it was actually calculating was
-deltaY / deltaX.  So it was getting the wrong answers for any
situation where the given line segment's slope wasn't +1/-1
(or 0 or infinite, which are correctly special-cased).

Depressingly, fixing this changes none of the regression test outputs;
apparently all the test cases involving distances were one of the
special cases.  But it's really amazing no one complained of this
before ...

            regards, tom lane