diff --git a/src/backend/utils/adt/geo_ops.c b/src/backend/utils/adt/geo_ops.c
index fc6493a0d7..a358296c98 100644
--- a/src/backend/utils/adt/geo_ops.c
+++ b/src/backend/utils/adt/geo_ops.c
@@ -1055,13 +1055,20 @@ line_send(PG_FUNCTION_ARGS)
 static inline void
 line_construct(LINE *result, Point *pt, float8 m)
 {
-	if (m == DBL_MAX)
+	if (isinf(m))
 	{
 		/* vertical - use "x = C" */
 		result->A = -1.0;
 		result->B = 0.0;
 		result->C = pt->x;
 	}
+	else if (m == 0)
+	{
+		/* horizontal - use "y = C" */
+		result->A = 0.0;
+		result->B = -1.0;
+		result->C = pt->y;
+	}
 	else
 	{
 		/* use "mx - y + yinter = 0" */
@@ -1197,7 +1204,7 @@ line_sl(LINE *line)
 	if (FPzero(line->A))
 		return 0.0;
 	if (FPzero(line->B))
-		return DBL_MAX;
+		return get_float8_infinity();
 	return float8_div(line->A, -line->B);
 }
 
@@ -1209,7 +1216,7 @@ static inline float8
 line_invsl(LINE *line)
 {
 	if (FPzero(line->A))
-		return DBL_MAX;
+		return get_float8_infinity();
 	if (FPzero(line->B))
 		return 0.0;
 	return float8_div(line->B, line->A);
@@ -1974,13 +1981,13 @@ point_slope(PG_FUNCTION_ARGS)
 /*
  * Return slope of two points
  *
- * Note that this function returns DBL_MAX when the points are the same.
+ * Note that this function returns Inf when the points are the same.
  */
 static inline float8
 point_sl(Point *pt1, Point *pt2)
 {
 	if (FPeq(pt1->x, pt2->x))
-		return DBL_MAX;
+		return get_float8_infinity();
 	if (FPeq(pt1->y, pt2->y))
 		return 0.0;
 	return float8_div(float8_mi(pt1->y, pt2->y), float8_mi(pt1->x, pt2->x));
@@ -1998,7 +2005,7 @@ point_invsl(Point *pt1, Point *pt2)
 	if (FPeq(pt1->x, pt2->x))
 		return 0.0;
 	if (FPeq(pt1->y, pt2->y))
-		return DBL_MAX;
+		return get_float8_infinity();
 	return float8_div(float8_mi(pt1->x, pt2->x), float8_mi(pt2->y, pt1->y));
 }
 
diff --git a/src/include/utils/geo_decls.h b/src/include/utils/geo_decls.h
index d4617558f1..6d19033f4e 100644
--- a/src/include/utils/geo_decls.h
+++ b/src/include/utils/geo_decls.h
@@ -20,23 +20,57 @@
 
 #include "fmgr.h"
 
+#include <math.h>
+
 /*--------------------------------------------------------------------
  * Useful floating point utilities and constants.
  *-------------------------------------------------------------------
  *
- * XXX: They are not NaN-aware.
+ * XXX: These are not NaN-aware.  They have, however, been made to give
+ * sane answers for infinity inputs (where it's important to avoid
+ * computing Inf minus Inf).
  */
 
 #define EPSILON					1.0E-06
 
 #ifdef EPSILON
 #define FPzero(A)				(fabs(A) <= EPSILON)
-#define FPeq(A,B)				(fabs((A) - (B)) <= EPSILON)
-#define FPne(A,B)				(fabs((A) - (B)) > EPSILON)
-#define FPlt(A,B)				((B) - (A) > EPSILON)
-#define FPle(A,B)				((A) - (B) <= EPSILON)
-#define FPgt(A,B)				((A) - (B) > EPSILON)
-#define FPge(A,B)				((B) - (A) <= EPSILON)
+
+static inline bool
+FPeq(double A, double B)
+{
+	return A == B || fabs(A - B) <= EPSILON;
+}
+
+static inline bool
+FPne(double A, double B)
+{
+	return A != B && fabs(A - B) > EPSILON;
+}
+
+static inline bool
+FPlt(double A, double B)
+{
+	return A != B && B - A > EPSILON;
+}
+
+static inline bool
+FPle(double A, double B)
+{
+	return A == B || A - B <= EPSILON;
+}
+
+static inline bool
+FPgt(double A, double B)
+{
+	return A != B && A - B > EPSILON;
+}
+
+static inline bool
+FPge(double A, double B)
+{
+	return A == B || B - A <= EPSILON;
+}
 #else
 #define FPzero(A)				((A) == 0)
 #define FPeq(A,B)				((A) == (B))
diff --git a/src/test/regress/expected/geometry.out b/src/test/regress/expected/geometry.out
index 8499300b53..3233dae21b 100644
--- a/src/test/regress/expected/geometry.out
+++ b/src/test/regress/expected/geometry.out
@@ -111,108 +111,108 @@ SELECT '' AS one, p1.f1
 
 -- Slope
 SELECT p1.f1, p2.f1, slope(p1.f1, p2.f1) FROM POINT_TBL p1, POINT_TBL p2;
-        f1         |        f1         |       slope        
--------------------+-------------------+--------------------
- (0,0)             | (0,0)             | 1.79769313486e+308
- (0,0)             | (-10,0)           |                  0
- (0,0)             | (-3,4)            |     -1.33333333333
- (0,0)             | (5.1,34.5)        |      6.76470588235
- (0,0)             | (-5,-12)          |                2.4
- (0,0)             | (1e-300,-1e-300)  | 1.79769313486e+308
- (0,0)             | (1e+300,Infinity) |           Infinity
- (0,0)             | (Infinity,1e+300) |                  0
- (0,0)             | (NaN,NaN)         |                NaN
- (0,0)             | (10,10)           |                  1
- (-10,0)           | (0,0)             |                  0
- (-10,0)           | (-10,0)           | 1.79769313486e+308
- (-10,0)           | (-3,4)            |     0.571428571429
- (-10,0)           | (5.1,34.5)        |      2.28476821192
- (-10,0)           | (-5,-12)          |               -2.4
- (-10,0)           | (1e-300,-1e-300)  |                  0
- (-10,0)           | (1e+300,Infinity) |           Infinity
- (-10,0)           | (Infinity,1e+300) |                  0
- (-10,0)           | (NaN,NaN)         |                NaN
- (-10,0)           | (10,10)           |                0.5
- (-3,4)            | (0,0)             |     -1.33333333333
- (-3,4)            | (-10,0)           |     0.571428571429
- (-3,4)            | (-3,4)            | 1.79769313486e+308
- (-3,4)            | (5.1,34.5)        |      3.76543209877
- (-3,4)            | (-5,-12)          |                  8
- (-3,4)            | (1e-300,-1e-300)  |     -1.33333333333
- (-3,4)            | (1e+300,Infinity) |           Infinity
- (-3,4)            | (Infinity,1e+300) |                  0
- (-3,4)            | (NaN,NaN)         |                NaN
- (-3,4)            | (10,10)           |     0.461538461538
- (5.1,34.5)        | (0,0)             |      6.76470588235
- (5.1,34.5)        | (-10,0)           |      2.28476821192
- (5.1,34.5)        | (-3,4)            |      3.76543209877
- (5.1,34.5)        | (5.1,34.5)        | 1.79769313486e+308
- (5.1,34.5)        | (-5,-12)          |      4.60396039604
- (5.1,34.5)        | (1e-300,-1e-300)  |      6.76470588235
- (5.1,34.5)        | (1e+300,Infinity) |           Infinity
- (5.1,34.5)        | (Infinity,1e+300) |                  0
- (5.1,34.5)        | (NaN,NaN)         |                NaN
- (5.1,34.5)        | (10,10)           |                 -5
- (-5,-12)          | (0,0)             |                2.4
- (-5,-12)          | (-10,0)           |               -2.4
- (-5,-12)          | (-3,4)            |                  8
- (-5,-12)          | (5.1,34.5)        |      4.60396039604
- (-5,-12)          | (-5,-12)          | 1.79769313486e+308
- (-5,-12)          | (1e-300,-1e-300)  |                2.4
- (-5,-12)          | (1e+300,Infinity) |           Infinity
- (-5,-12)          | (Infinity,1e+300) |                  0
- (-5,-12)          | (NaN,NaN)         |                NaN
- (-5,-12)          | (10,10)           |      1.46666666667
- (1e-300,-1e-300)  | (0,0)             | 1.79769313486e+308
- (1e-300,-1e-300)  | (-10,0)           |                  0
- (1e-300,-1e-300)  | (-3,4)            |     -1.33333333333
- (1e-300,-1e-300)  | (5.1,34.5)        |      6.76470588235
- (1e-300,-1e-300)  | (-5,-12)          |                2.4
- (1e-300,-1e-300)  | (1e-300,-1e-300)  | 1.79769313486e+308
- (1e-300,-1e-300)  | (1e+300,Infinity) |           Infinity
- (1e-300,-1e-300)  | (Infinity,1e+300) |                  0
- (1e-300,-1e-300)  | (NaN,NaN)         |                NaN
- (1e-300,-1e-300)  | (10,10)           |                  1
- (1e+300,Infinity) | (0,0)             |           Infinity
- (1e+300,Infinity) | (-10,0)           |           Infinity
- (1e+300,Infinity) | (-3,4)            |           Infinity
- (1e+300,Infinity) | (5.1,34.5)        |           Infinity
- (1e+300,Infinity) | (-5,-12)          |           Infinity
- (1e+300,Infinity) | (1e-300,-1e-300)  |           Infinity
- (1e+300,Infinity) | (1e+300,Infinity) | 1.79769313486e+308
- (1e+300,Infinity) | (Infinity,1e+300) |                NaN
- (1e+300,Infinity) | (NaN,NaN)         |                NaN
- (1e+300,Infinity) | (10,10)           |           Infinity
- (Infinity,1e+300) | (0,0)             |                  0
- (Infinity,1e+300) | (-10,0)           |                  0
- (Infinity,1e+300) | (-3,4)            |                  0
- (Infinity,1e+300) | (5.1,34.5)        |                  0
- (Infinity,1e+300) | (-5,-12)          |                  0
- (Infinity,1e+300) | (1e-300,-1e-300)  |                  0
- (Infinity,1e+300) | (1e+300,Infinity) |                NaN
- (Infinity,1e+300) | (Infinity,1e+300) |                  0
- (Infinity,1e+300) | (NaN,NaN)         |                NaN
- (Infinity,1e+300) | (10,10)           |                  0
- (NaN,NaN)         | (0,0)             |                NaN
- (NaN,NaN)         | (-10,0)           |                NaN
- (NaN,NaN)         | (-3,4)            |                NaN
- (NaN,NaN)         | (5.1,34.5)        |                NaN
- (NaN,NaN)         | (-5,-12)          |                NaN
- (NaN,NaN)         | (1e-300,-1e-300)  |                NaN
- (NaN,NaN)         | (1e+300,Infinity) |                NaN
- (NaN,NaN)         | (Infinity,1e+300) |                NaN
- (NaN,NaN)         | (NaN,NaN)         |                NaN
- (NaN,NaN)         | (10,10)           |                NaN
- (10,10)           | (0,0)             |                  1
- (10,10)           | (-10,0)           |                0.5
- (10,10)           | (-3,4)            |     0.461538461538
- (10,10)           | (5.1,34.5)        |                 -5
- (10,10)           | (-5,-12)          |      1.46666666667
- (10,10)           | (1e-300,-1e-300)  |                  1
- (10,10)           | (1e+300,Infinity) |           Infinity
- (10,10)           | (Infinity,1e+300) |                  0
- (10,10)           | (NaN,NaN)         |                NaN
- (10,10)           | (10,10)           | 1.79769313486e+308
+        f1         |        f1         |     slope      
+-------------------+-------------------+----------------
+ (0,0)             | (0,0)             |       Infinity
+ (0,0)             | (-10,0)           |              0
+ (0,0)             | (-3,4)            | -1.33333333333
+ (0,0)             | (5.1,34.5)        |  6.76470588235
+ (0,0)             | (-5,-12)          |            2.4
+ (0,0)             | (1e-300,-1e-300)  |       Infinity
+ (0,0)             | (1e+300,Infinity) |       Infinity
+ (0,0)             | (Infinity,1e+300) |              0
+ (0,0)             | (NaN,NaN)         |            NaN
+ (0,0)             | (10,10)           |              1
+ (-10,0)           | (0,0)             |              0
+ (-10,0)           | (-10,0)           |       Infinity
+ (-10,0)           | (-3,4)            | 0.571428571429
+ (-10,0)           | (5.1,34.5)        |  2.28476821192
+ (-10,0)           | (-5,-12)          |           -2.4
+ (-10,0)           | (1e-300,-1e-300)  |              0
+ (-10,0)           | (1e+300,Infinity) |       Infinity
+ (-10,0)           | (Infinity,1e+300) |              0
+ (-10,0)           | (NaN,NaN)         |            NaN
+ (-10,0)           | (10,10)           |            0.5
+ (-3,4)            | (0,0)             | -1.33333333333
+ (-3,4)            | (-10,0)           | 0.571428571429
+ (-3,4)            | (-3,4)            |       Infinity
+ (-3,4)            | (5.1,34.5)        |  3.76543209877
+ (-3,4)            | (-5,-12)          |              8
+ (-3,4)            | (1e-300,-1e-300)  | -1.33333333333
+ (-3,4)            | (1e+300,Infinity) |       Infinity
+ (-3,4)            | (Infinity,1e+300) |              0
+ (-3,4)            | (NaN,NaN)         |            NaN
+ (-3,4)            | (10,10)           | 0.461538461538
+ (5.1,34.5)        | (0,0)             |  6.76470588235
+ (5.1,34.5)        | (-10,0)           |  2.28476821192
+ (5.1,34.5)        | (-3,4)            |  3.76543209877
+ (5.1,34.5)        | (5.1,34.5)        |       Infinity
+ (5.1,34.5)        | (-5,-12)          |  4.60396039604
+ (5.1,34.5)        | (1e-300,-1e-300)  |  6.76470588235
+ (5.1,34.5)        | (1e+300,Infinity) |       Infinity
+ (5.1,34.5)        | (Infinity,1e+300) |              0
+ (5.1,34.5)        | (NaN,NaN)         |            NaN
+ (5.1,34.5)        | (10,10)           |             -5
+ (-5,-12)          | (0,0)             |            2.4
+ (-5,-12)          | (-10,0)           |           -2.4
+ (-5,-12)          | (-3,4)            |              8
+ (-5,-12)          | (5.1,34.5)        |  4.60396039604
+ (-5,-12)          | (-5,-12)          |       Infinity
+ (-5,-12)          | (1e-300,-1e-300)  |            2.4
+ (-5,-12)          | (1e+300,Infinity) |       Infinity
+ (-5,-12)          | (Infinity,1e+300) |              0
+ (-5,-12)          | (NaN,NaN)         |            NaN
+ (-5,-12)          | (10,10)           |  1.46666666667
+ (1e-300,-1e-300)  | (0,0)             |       Infinity
+ (1e-300,-1e-300)  | (-10,0)           |              0
+ (1e-300,-1e-300)  | (-3,4)            | -1.33333333333
+ (1e-300,-1e-300)  | (5.1,34.5)        |  6.76470588235
+ (1e-300,-1e-300)  | (-5,-12)          |            2.4
+ (1e-300,-1e-300)  | (1e-300,-1e-300)  |       Infinity
+ (1e-300,-1e-300)  | (1e+300,Infinity) |       Infinity
+ (1e-300,-1e-300)  | (Infinity,1e+300) |              0
+ (1e-300,-1e-300)  | (NaN,NaN)         |            NaN
+ (1e-300,-1e-300)  | (10,10)           |              1
+ (1e+300,Infinity) | (0,0)             |       Infinity
+ (1e+300,Infinity) | (-10,0)           |       Infinity
+ (1e+300,Infinity) | (-3,4)            |       Infinity
+ (1e+300,Infinity) | (5.1,34.5)        |       Infinity
+ (1e+300,Infinity) | (-5,-12)          |       Infinity
+ (1e+300,Infinity) | (1e-300,-1e-300)  |       Infinity
+ (1e+300,Infinity) | (1e+300,Infinity) |       Infinity
+ (1e+300,Infinity) | (Infinity,1e+300) |            NaN
+ (1e+300,Infinity) | (NaN,NaN)         |            NaN
+ (1e+300,Infinity) | (10,10)           |       Infinity
+ (Infinity,1e+300) | (0,0)             |              0
+ (Infinity,1e+300) | (-10,0)           |              0
+ (Infinity,1e+300) | (-3,4)            |              0
+ (Infinity,1e+300) | (5.1,34.5)        |              0
+ (Infinity,1e+300) | (-5,-12)          |              0
+ (Infinity,1e+300) | (1e-300,-1e-300)  |              0
+ (Infinity,1e+300) | (1e+300,Infinity) |            NaN
+ (Infinity,1e+300) | (Infinity,1e+300) |       Infinity
+ (Infinity,1e+300) | (NaN,NaN)         |            NaN
+ (Infinity,1e+300) | (10,10)           |              0
+ (NaN,NaN)         | (0,0)             |            NaN
+ (NaN,NaN)         | (-10,0)           |            NaN
+ (NaN,NaN)         | (-3,4)            |            NaN
+ (NaN,NaN)         | (5.1,34.5)        |            NaN
+ (NaN,NaN)         | (-5,-12)          |            NaN
+ (NaN,NaN)         | (1e-300,-1e-300)  |            NaN
+ (NaN,NaN)         | (1e+300,Infinity) |            NaN
+ (NaN,NaN)         | (Infinity,1e+300) |            NaN
+ (NaN,NaN)         | (NaN,NaN)         |            NaN
+ (NaN,NaN)         | (10,10)           |            NaN
+ (10,10)           | (0,0)             |              1
+ (10,10)           | (-10,0)           |            0.5
+ (10,10)           | (-3,4)            | 0.461538461538
+ (10,10)           | (5.1,34.5)        |             -5
+ (10,10)           | (-5,-12)          |  1.46666666667
+ (10,10)           | (1e-300,-1e-300)  |              1
+ (10,10)           | (1e+300,Infinity) |       Infinity
+ (10,10)           | (Infinity,1e+300) |              0
+ (10,10)           | (NaN,NaN)         |            NaN
+ (10,10)           | (10,10)           |       Infinity
 (100 rows)
 
 -- Add point
@@ -563,7 +563,7 @@ SELECT p.f1, l.s, p.f1 <-> l.s AS dist_pl, l.s <-> p.f1 AS dist_lp FROM POINT_TB
  (1e+300,Infinity) | {0,-1,3}                              |           Infinity |           Infinity
  (1e+300,Infinity) | {-1,0,3}                              |                NaN |                NaN
  (Infinity,1e+300) | {0,-1,5}                              |                NaN |                NaN
- (Infinity,1e+300) | {1,0,5}                               |                NaN |                NaN
+ (Infinity,1e+300) | {1,0,5}                               |           Infinity |           Infinity
  (Infinity,1e+300) | {0,3,0}                               |                NaN |                NaN
  (Infinity,1e+300) | {1,-1,0}                              |                NaN |                NaN
  (Infinity,1e+300) | {-0.4,-1,-6}                          |                NaN |                NaN
@@ -571,7 +571,7 @@ SELECT p.f1, l.s, p.f1 <-> l.s AS dist_pl, l.s <-> p.f1 AS dist_lp FROM POINT_TB
  (Infinity,1e+300) | {3,NaN,5}                             |                NaN |                NaN
  (Infinity,1e+300) | {NaN,NaN,NaN}                         |                NaN |                NaN
  (Infinity,1e+300) | {0,-1,3}                              |                NaN |                NaN
- (Infinity,1e+300) | {-1,0,3}                              |                NaN |                NaN
+ (Infinity,1e+300) | {-1,0,3}                              |           Infinity |           Infinity
  (NaN,NaN)         | {0,-1,5}                              |                NaN |                NaN
  (NaN,NaN)         | {1,0,5}                               |                NaN |                NaN
  (NaN,NaN)         | {0,3,0}                               |                NaN |                NaN
@@ -983,7 +983,7 @@ SELECT p.f1, l.s, p.f1 ## l.s FROM POINT_TBL p, LINE_TBL l;
  (1e+300,Infinity) | {0,-1,3}                              | (1e+300,3)
  (1e+300,Infinity) | {-1,0,3}                              | 
  (Infinity,1e+300) | {0,-1,5}                              | 
- (Infinity,1e+300) | {1,0,5}                               | 
+ (Infinity,1e+300) | {1,0,5}                               | (-5,1e+300)
  (Infinity,1e+300) | {0,3,0}                               | 
  (Infinity,1e+300) | {1,-1,0}                              | 
  (Infinity,1e+300) | {-0.4,-1,-6}                          | 
@@ -991,7 +991,7 @@ SELECT p.f1, l.s, p.f1 ## l.s FROM POINT_TBL p, LINE_TBL l;
  (Infinity,1e+300) | {3,NaN,5}                             | 
  (Infinity,1e+300) | {NaN,NaN,NaN}                         | 
  (Infinity,1e+300) | {0,-1,3}                              | 
- (Infinity,1e+300) | {-1,0,3}                              | 
+ (Infinity,1e+300) | {-1,0,3}                              | (3,1e+300)
  (NaN,NaN)         | {0,-1,5}                              | 
  (NaN,NaN)         | {1,0,5}                               | 
  (NaN,NaN)         | {0,3,0}                               | 
