Index: /trunk/psLib/src/math/psStats.c
===================================================================
--- /trunk/psLib/src/math/psStats.c	(revision 26640)
+++ /trunk/psLib/src/math/psStats.c	(revision 26641)
@@ -751,4 +751,12 @@
         psTrace(TRACE, 6, "-------------------- Iterating on Bin size.  Iteration number %d --------------------\n", iterate);
 
+	if (iterate >= PS_ROBUST_MAX_ITERATIONS) {
+	  // This occurs when a large number of the values are identical --- a bin size cannot be found
+	  // that will spread out the distribution.  Therefore, set what we can, and fall over
+	  // gracefully.
+	  COUNT_WARNING(10, 100, "Maximum number of iterations (%d) exceeded.", PS_ROBUST_MAX_ITERATIONS);
+	  goto escape;
+	}
+
         // Get the minimum and maximum values
         int numValid = vectorMinMax(myVector, mask, maskVal, statsMinMax); // Number of valid values
@@ -835,6 +843,6 @@
 		if (mask->data.PS_TYPE_VECTOR_MASK_DATA[i] & maskVal) continue;
 		bool invalid = false;
-		invalid |= (myVector->data.F32[i] < minKeep);
-		invalid |= (myVector->data.F32[i] > maxKeep);
+		invalid |= (myVector->data.F32[i] <= minKeep);
+		invalid |= (myVector->data.F32[i] >= maxKeep);
 		invalid |= (!isfinite(myVector->data.F32[i]));
 		if (!invalid) continue;
@@ -842,8 +850,14 @@
 		nInvalid ++;
             }
-	    psTrace(TRACE, 6, "data is concentrated in a single bin, masking %d extreme outliers and retrying\n", nInvalid);
-            psFree(histogram);
-            psFree(cumulative);
-	    continue;
+
+	    if (nInvalid) {
+	      psTrace(TRACE, 6, "data is concentrated in a single bin, masking %d extreme outliers and retrying\n", nInvalid);
+	      psFree(histogram);
+	      psFree(cumulative);
+	      histogram = NULL;
+	      cumulative = NULL;
+	      continue;
+	    }
+	    // if we did not mask anything, give up.
 	}
 
