Mercurial > hg > octave-lyh
diff liboctave/Range.cc @ 3858:de05e6bdf897
[project @ 2001-11-08 19:34:22 by jwe]
author | jwe |
---|---|
date | Thu, 08 Nov 2001 19:34:23 +0000 |
parents | f751e43de300 |
children | 6e86256e9c54 |
line wrap: on
line diff
--- a/liboctave/Range.cc +++ b/liboctave/Range.cc @@ -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; } /*