changeset 3858:de05e6bdf897

[project @ 2001-11-08 19:34:22 by jwe]
author jwe
date Thu, 08 Nov 2001 19:34:23 +0000
parents f7c7ecb63a7e
children 890a7e4c1362
files liboctave/ChangeLog liboctave/Range.cc
diffstat 2 files changed, 38 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
--- a/liboctave/ChangeLog	Wed Nov 07 05:36:35 2001 +0000
+++ b/liboctave/ChangeLog	Thu Nov 08 19:34:23 2001 +0000
@@ -1,3 +1,8 @@
+2001-11-08  John W. Eaton  <jwe@bevo.che.wisc.edu>
+
+	* Range.cc (Range::nelem_internal): Special case ranges that must
+	have zero elements.
+
 2001-11-06  John W. Eaton  <jwe@bevo.che.wisc.edu>
 
 	* Makefile.in: Split out readline and pathsearch functionality
--- a/liboctave/Range.cc	Wed Nov 07 05:36:35 2001 +0000
+++ b/liboctave/Range.cc	Thu Nov 08 19:34:23 2001 +0000
@@ -242,31 +242,45 @@
 int
 Range::nelem_internal (void) const
 {
-  double ct = 3.0 * DBL_EPSILON;
+  int retval = -1;
 
-  double tmp = tfloor ((rng_limit - rng_base + rng_inc) / rng_inc, ct);
+  if (rng_inc == 0
+      || (rng_limit > rng_base && rng_inc < 0)
+      || (rng_limit < rng_base && rng_inc > 0))
+    {
+      retval = 0;
+    }
+  else
+    {
+      double ct = 3.0 * DBL_EPSILON;
 
-  int n_elt = (tmp > 0.0 ? static_cast<int> (tmp) : 0);
+      double tmp = tfloor ((rng_limit - rng_base + rng_inc) / rng_inc, ct);
+
+      int n_elt = (tmp > 0.0 ? static_cast<int> (tmp) : 0);
 
-  // If the final element that we would compute for the range is equal
-  // to the limit of the range, or is an adjacent floating point
-  // number, accept it.  Otherwise, try a range with one fewer
-  // element.  If that fails, try again with one more element.
-  //
-  // I'm not sure this is very good, but it seems to work better than
-  // just using tfloor as above.  For example, without it, the
-  // expression 1.8:0.05:1.9 fails to produce the expected result of
-  // [1.8, 1.85, 1.9].
+      // If the final element that we would compute for the range is
+      // equal to the limit of the range, or is an adjacent floating
+      // point number, accept it.  Otherwise, try a range with one
+      // fewer element.  If that fails, try again with one more
+      // element.
+      //
+      // I'm not sure this is very good, but it seems to work better than
+      // just using tfloor as above.  For example, without it, the
+      // expression 1.8:0.05:1.9 fails to produce the expected result of
+      // [1.8, 1.85, 1.9].
 
-  if (! teq (rng_base + (n_elt - 1) * rng_inc, rng_limit))
-    {
-      if (teq (rng_base + (n_elt - 2) * rng_inc, rng_limit))
-	n_elt--;
-      else if (teq (rng_base + n_elt * rng_inc, rng_limit))
-	n_elt++;
+      if (! teq (rng_base + (n_elt - 1) * rng_inc, rng_limit))
+	{
+	  if (teq (rng_base + (n_elt - 2) * rng_inc, rng_limit))
+	    n_elt--;
+	  else if (teq (rng_base + n_elt * rng_inc, rng_limit))
+	    n_elt++;
+	}
+
+      retval = (n_elt >= INT_MAX - 1) ? -1 : n_elt;
     }
 
-  return (n_elt >= INT_MAX - 1) ? -1 : n_elt;
+  return retval;
 }
 
 /*