comparison liboctave/Range.cc @ 5275:23b37da9fd5b

[project @ 2005-04-08 16:07:35 by jwe]
author jwe
date Fri, 08 Apr 2005 16:07:37 +0000
parents e35b034d3523
children 4c8a2e4e0717
comparison
equal deleted inserted replaced
5274:eae7b40388e9 5275:23b37da9fd5b
39 { 39 {
40 // If the base and increment are ints, the final value in the range 40 // If the base and increment are ints, the final value in the range
41 // will also be an integer, even if the limit is not. 41 // will also be an integer, even if the limit is not.
42 42
43 return (! (xisnan (rng_base) || xisnan (rng_inc)) 43 return (! (xisnan (rng_base) || xisnan (rng_inc))
44 && NINT (rng_base) == rng_base 44 && NINTbig (rng_base) == rng_base
45 && NINT (rng_inc) == rng_inc); 45 && NINTbig (rng_inc) == rng_inc);
46 } 46 }
47 47
48 Matrix 48 Matrix
49 Range::matrix_value (void) const 49 Range::matrix_value (void) const
50 { 50 {
51 if (rng_nelem > 0 && cache.rows () == 0) 51 if (rng_nelem > 0 && cache.rows () == 0)
52 { 52 {
53 cache.resize (1, rng_nelem); 53 cache.resize (1, rng_nelem);
54 double b = rng_base; 54 double b = rng_base;
55 double increment = rng_inc; 55 double increment = rng_inc;
56 for (int i = 0; i < rng_nelem; i++) 56 for (octave_idx_type i = 0; i < rng_nelem; i++)
57 cache(i) = b + i * increment; 57 cache(i) = b + i * increment;
58 58
59 // On some machines (x86 with extended precision floating point 59 // On some machines (x86 with extended precision floating point
60 // arithmetic, for example) it is possible that we can overshoot 60 // arithmetic, for example) it is possible that we can overshoot
61 // the limit by approximately the machine precision even though 61 // the limit by approximately the machine precision even though
132 std::ostream& 132 std::ostream&
133 operator << (std::ostream& os, const Range& a) 133 operator << (std::ostream& os, const Range& a)
134 { 134 {
135 double b = a.base (); 135 double b = a.base ();
136 double increment = a.inc (); 136 double increment = a.inc ();
137 int num_elem = a.nelem (); 137 octave_idx_type num_elem = a.nelem ();
138 138
139 for (int i = 0; i < num_elem-1; i++) 139 for (octave_idx_type i = 0; i < num_elem-1; i++)
140 os << b + i * increment << " "; 140 os << b + i * increment << " ";
141 141
142 // Prevent overshoot. See comment in the matrix_value method 142 // Prevent overshoot. See comment in the matrix_value method
143 // above. 143 // above.
144 144
245 double tv = fabs (v); 245 double tv = fabs (v);
246 246
247 return fabs (u - v) < ((tu > tv ? tu : tv) * ct); 247 return fabs (u - v) < ((tu > tv ? tu : tv) * ct);
248 } 248 }
249 249
250 int 250 octave_idx_type
251 Range::nelem_internal (void) const 251 Range::nelem_internal (void) const
252 { 252 {
253 int retval = -1; 253 octave_idx_type retval = -1;
254 254
255 if (rng_inc == 0 255 if (rng_inc == 0
256 || (rng_limit > rng_base && rng_inc < 0) 256 || (rng_limit > rng_base && rng_inc < 0)
257 || (rng_limit < rng_base && rng_inc > 0)) 257 || (rng_limit < rng_base && rng_inc > 0))
258 { 258 {
262 { 262 {
263 double ct = 3.0 * DBL_EPSILON; 263 double ct = 3.0 * DBL_EPSILON;
264 264
265 double tmp = tfloor ((rng_limit - rng_base + rng_inc) / rng_inc, ct); 265 double tmp = tfloor ((rng_limit - rng_base + rng_inc) / rng_inc, ct);
266 266
267 int n_elt = (tmp > 0.0 ? static_cast<int> (tmp) : 0); 267 octave_idx_type n_elt = (tmp > 0.0 ? static_cast<octave_idx_type> (tmp) : 0);
268 268
269 // If the final element that we would compute for the range is 269 // If the final element that we would compute for the range is
270 // equal to the limit of the range, or is an adjacent floating 270 // equal to the limit of the range, or is an adjacent floating
271 // point number, accept it. Otherwise, try a range with one 271 // point number, accept it. Otherwise, try a range with one
272 // fewer element. If that fails, try again with one more 272 // fewer element. If that fails, try again with one more