Skip to content

Commit 9fe649e

Browse files
committed
In geo_ops.c, represent infinite slope as Infinity, not DBL_MAX.
Since we're assuming IEEE floats these days, there seems little reason not to do this. It has the advantage that when the slope is computed as infinite due to the presence of Inf coordinates, we get saner behavior than before from line_construct(), and thence also in some dependent operations such as finding the closest point. Also fix line_construct() to special-case slope zero. The previous coding got the right answer in most cases, but it could compute C as NaN when the point has Inf coordinates. Discussion: https://postgr.es/m/CAGf+fX70rWFOk5cd00uMfa__0yP+vtQg5ck7c2Onb-Yczp0URA@mail.gmail.com
1 parent 8597a48 commit 9fe649e

File tree

2 files changed

+140
-133
lines changed

2 files changed

+140
-133
lines changed

src/backend/utils/adt/geo_ops.c

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,13 +1055,20 @@ line_send(PG_FUNCTION_ARGS)
10551055
static inline void
10561056
line_construct(LINE *result, Point *pt, float8 m)
10571057
{
1058-
if (m == DBL_MAX)
1058+
if (isinf(m))
10591059
{
10601060
/* vertical - use "x = C" */
10611061
result->A = -1.0;
10621062
result->B = 0.0;
10631063
result->C = pt->x;
10641064
}
1065+
else if (m == 0)
1066+
{
1067+
/* horizontal - use "y = C" */
1068+
result->A = 0.0;
1069+
result->B = -1.0;
1070+
result->C = pt->y;
1071+
}
10651072
else
10661073
{
10671074
/* use "mx - y + yinter = 0" */
@@ -1201,7 +1208,7 @@ line_sl(LINE *line)
12011208
if (FPzero(line->A))
12021209
return 0.0;
12031210
if (FPzero(line->B))
1204-
return DBL_MAX;
1211+
return get_float8_infinity();
12051212
return float8_div(line->A, -line->B);
12061213
}
12071214

@@ -1213,7 +1220,7 @@ static inline float8
12131220
line_invsl(LINE *line)
12141221
{
12151222
if (FPzero(line->A))
1216-
return DBL_MAX;
1223+
return get_float8_infinity();
12171224
if (FPzero(line->B))
12181225
return 0.0;
12191226
return float8_div(line->B, line->A);
@@ -1979,13 +1986,13 @@ point_slope(PG_FUNCTION_ARGS)
19791986
/*
19801987
* Return slope of two points
19811988
*
1982-
* Note that this function returns DBL_MAX when the points are the same.
1989+
* Note that this function returns Inf when the points are the same.
19831990
*/
19841991
static inline float8
19851992
point_sl(Point *pt1, Point *pt2)
19861993
{
19871994
if (FPeq(pt1->x, pt2->x))
1988-
return DBL_MAX;
1995+
return get_float8_infinity();
19891996
if (FPeq(pt1->y, pt2->y))
19901997
return 0.0;
19911998
return float8_div(float8_mi(pt1->y, pt2->y), float8_mi(pt1->x, pt2->x));
@@ -2003,7 +2010,7 @@ point_invsl(Point *pt1, Point *pt2)
20032010
if (FPeq(pt1->x, pt2->x))
20042011
return 0.0;
20052012
if (FPeq(pt1->y, pt2->y))
2006-
return DBL_MAX;
2013+
return get_float8_infinity();
20072014
return float8_div(float8_mi(pt1->x, pt2->x), float8_mi(pt2->y, pt1->y));
20082015
}
20092016

0 commit comments

Comments
 (0)