3
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
3
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
1315
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
3
|
20 |
|
21 */ |
|
22 |
4192
|
23 #if defined (__GNUG__) && defined (USE_PRAGMA_INTERFACE_IMPLEMENTATION) |
1296
|
24 #pragma implementation |
|
25 #endif |
|
26 |
238
|
27 #ifdef HAVE_CONFIG_H |
1192
|
28 #include <config.h> |
3
|
29 #endif |
|
30 |
1546
|
31 #include <cfloat> |
1367
|
32 #include <climits> |
1546
|
33 #include <cmath> |
1367
|
34 |
3503
|
35 #include <iostream> |
3
|
36 |
|
37 #include "Range.h" |
645
|
38 #include "dMatrix.h" |
2383
|
39 #include "lo-mappers.h" |
|
40 #include "lo-utils.h" |
|
41 |
|
42 bool |
|
43 Range::all_elements_are_ints (void) const |
|
44 { |
|
45 // If the base and increment are ints, the final value in the range |
|
46 // will also be an integer, even if the limit is not. |
|
47 |
|
48 return (! (xisnan (rng_base) || xisnan (rng_inc)) |
2800
|
49 && NINT (rng_base) == rng_base |
|
50 && NINT (rng_inc) == rng_inc); |
2383
|
51 } |
645
|
52 |
|
53 Matrix |
|
54 Range::matrix_value (void) const |
|
55 { |
|
56 Matrix retval; |
|
57 |
|
58 if (rng_nelem > 0) |
|
59 { |
|
60 retval.resize (1, rng_nelem); |
|
61 double b = rng_base; |
|
62 double increment = rng_inc; |
|
63 for (int i = 0; i < rng_nelem; i++) |
4791
|
64 retval(i) = b + i * increment; |
|
65 |
|
66 // On some machines (x86 with extended precision floating point |
|
67 // arithmetic, for example) it is possible that we can overshoot |
|
68 // the limit by approximately the machine precision even though |
|
69 // we were very careful in our calculation of the number of |
|
70 // elements. |
|
71 |
|
72 if ((rng_inc > 0 && retval(rng_nelem-1) > rng_limit) |
|
73 || (rng_inc < 0 && retval(rng_nelem-1) < rng_limit)) |
|
74 retval(rng_nelem-1) = rng_limit; |
645
|
75 } |
|
76 |
|
77 return retval; |
|
78 } |
3
|
79 |
208
|
80 // NOTE: max and min only return useful values if nelem > 0. |
|
81 |
|
82 double |
|
83 Range::min (void) const |
|
84 { |
|
85 double retval = 0.0; |
|
86 if (rng_nelem > 0) |
|
87 { |
|
88 if (rng_inc > 0) |
|
89 retval = rng_base; |
|
90 else |
4791
|
91 { |
|
92 retval = rng_base + (rng_nelem - 1) * rng_inc; |
|
93 |
|
94 // See the note in the matrix_value method above. |
|
95 |
|
96 if (retval < rng_limit) |
|
97 retval = rng_limit; |
|
98 } |
|
99 |
208
|
100 } |
|
101 return retval; |
|
102 } |
|
103 |
|
104 double |
|
105 Range::max (void) const |
|
106 { |
|
107 double retval = 0.0; |
|
108 if (rng_nelem > 0) |
|
109 { |
|
110 if (rng_inc > 0) |
4791
|
111 { |
|
112 retval = rng_base + (rng_nelem - 1) * rng_inc; |
|
113 |
|
114 // See the note in the matrix_value method above. |
|
115 |
|
116 if (retval > rng_limit) |
|
117 retval = rng_limit; |
|
118 } |
208
|
119 else |
|
120 retval = rng_base; |
|
121 } |
|
122 return retval; |
|
123 } |
|
124 |
|
125 void |
|
126 Range::sort (void) |
|
127 { |
|
128 if (rng_base > rng_limit && rng_inc < 0.0) |
|
129 { |
|
130 double tmp = rng_base; |
|
131 rng_base = min (); |
|
132 rng_limit = tmp; |
|
133 rng_inc = -rng_inc; |
|
134 } |
|
135 } |
|
136 |
3
|
137 void |
|
138 Range::print_range (void) |
|
139 { |
3504
|
140 std::cerr << "Range: rng_base = " << rng_base |
|
141 << " rng_limit " << rng_limit |
|
142 << " rng_inc " << rng_inc |
|
143 << " rng_nelem " << rng_nelem << "\n"; |
3
|
144 } |
|
145 |
3504
|
146 std::ostream& |
|
147 operator << (std::ostream& os, const Range& a) |
3
|
148 { |
|
149 double b = a.base (); |
|
150 double increment = a.inc (); |
|
151 int num_elem = a.nelem (); |
|
152 |
4791
|
153 for (int i = 0; i < num_elem-1; i++) |
3
|
154 os << b + i * increment << " "; |
|
155 |
4791
|
156 // Prevent overshoot. See comment in the matrix_value method |
|
157 // above. |
|
158 |
|
159 os << (increment > 0 ? a.max () : a.min ()) << "\n"; |
3
|
160 |
|
161 return os; |
|
162 } |
|
163 |
3504
|
164 std::istream& |
|
165 operator >> (std::istream& is, Range& a) |
3
|
166 { |
208
|
167 is >> a.rng_base; |
3
|
168 if (is) |
|
169 { |
208
|
170 is >> a.rng_limit; |
3
|
171 if (is) |
|
172 { |
208
|
173 is >> a.rng_inc; |
|
174 a.rng_nelem = a.nelem_internal (); |
3
|
175 } |
|
176 } |
|
177 |
|
178 return is; |
|
179 } |
|
180 |
2599
|
181 Range |
|
182 operator - (const Range& r) |
|
183 { |
|
184 return Range (-r.base (), -r.limit (), -r.inc ()); |
|
185 } |
|
186 |
1546
|
187 // C See Knuth, Art Of Computer Programming, Vol. 1, Problem 1.2.4-5. |
|
188 // C |
|
189 // C===Tolerant FLOOR function. |
|
190 // C |
|
191 // C X - is given as a Double Precision argument to be operated on. |
|
192 // C It is assumed that X is represented with M mantissa bits. |
|
193 // C CT - is given as a Comparison Tolerance such that |
|
194 // C 0.LT.CT.LE.3-SQRT(5)/2. If the relative difference between |
|
195 // C X and A whole number is less than CT, then TFLOOR is |
|
196 // C returned as this whole number. By treating the |
|
197 // C floating-point numbers as a finite ordered set note that |
|
198 // C the heuristic EPS=2.**(-(M-1)) and CT=3*EPS causes |
|
199 // C arguments of TFLOOR/TCEIL to be treated as whole numbers |
|
200 // C if they are exactly whole numbers or are immediately |
|
201 // C adjacent to whole number representations. Since EPS, the |
|
202 // C "distance" between floating-point numbers on the unit |
|
203 // C interval, and M, the number of bits in X'S mantissa, exist |
|
204 // C on every floating-point computer, TFLOOR/TCEIL are |
|
205 // C consistently definable on every floating-point computer. |
|
206 // C |
|
207 // C For more information see the following references: |
|
208 // C (1) P. E. Hagerty, "More On Fuzzy Floor And Ceiling," APL QUOTE |
|
209 // C QUAD 8(4):20-24, June 1978. Note that TFLOOR=FL5. |
|
210 // C (2) L. M. Breed, "Definitions For Fuzzy Floor And Ceiling", APL |
|
211 // C QUOTE QUAD 8(3):16-23, March 1978. This paper cites FL1 through |
|
212 // C FL5, the history of five years of evolutionary development of |
|
213 // C FL5 - the seven lines of code below - by open collaboration |
|
214 // C and corroboration of the mathematical-computing community. |
|
215 // C |
|
216 // C Penn State University Center for Academic Computing |
|
217 // C H. D. Knoble - August, 1978. |
|
218 |
|
219 static inline double |
|
220 tfloor (double x, double ct) |
|
221 { |
|
222 // C---------FLOOR(X) is the largest integer algebraically less than |
|
223 // C or equal to X; that is, the unfuzzy FLOOR function. |
|
224 |
|
225 // DINT (X) = X - DMOD (X, 1.0); |
|
226 // FLOOR (X) = DINT (X) - DMOD (2.0 + DSIGN (1.0, X), 3.0); |
|
227 |
|
228 // C---------Hagerty's FL5 function follows... |
|
229 |
|
230 double q = 1.0; |
|
231 |
|
232 if (x < 0.0) |
|
233 q = 1.0 - ct; |
|
234 |
|
235 double rmax = q / (2.0 - ct); |
|
236 |
|
237 double t1 = 1.0 + floor (x); |
|
238 t1 = (ct / q) * (t1 < 0.0 ? -t1 : t1); |
|
239 t1 = rmax < t1 ? rmax : t1; |
|
240 t1 = ct > t1 ? ct : t1; |
|
241 t1 = floor (x + t1); |
|
242 |
1555
|
243 if (x <= 0.0 || (t1 - x) < rmax) |
1546
|
244 return t1; |
|
245 else |
|
246 return t1 - 1.0; |
|
247 } |
|
248 |
|
249 static inline double |
|
250 tceil (double x, double ct) |
|
251 { |
|
252 return -tfloor (-x, ct); |
|
253 } |
|
254 |
|
255 static inline double |
|
256 round (double x, double ct) |
|
257 { |
|
258 return tfloor (x+0.5, ct); |
|
259 } |
|
260 |
3753
|
261 static inline bool |
|
262 teq (double u, double v, double ct = 3.0 * DBL_EPSILON) |
|
263 { |
|
264 double tu = fabs (u); |
|
265 double tv = fabs (v); |
|
266 |
|
267 return fabs (u - v) < ((tu > tv ? tu : tv) * ct); |
|
268 } |
|
269 |
1360
|
270 int |
|
271 Range::nelem_internal (void) const |
|
272 { |
3858
|
273 int retval = -1; |
3
|
274 |
3858
|
275 if (rng_inc == 0 |
|
276 || (rng_limit > rng_base && rng_inc < 0) |
|
277 || (rng_limit < rng_base && rng_inc > 0)) |
|
278 { |
|
279 retval = 0; |
|
280 } |
|
281 else |
|
282 { |
|
283 double ct = 3.0 * DBL_EPSILON; |
3
|
284 |
3858
|
285 double tmp = tfloor ((rng_limit - rng_base + rng_inc) / rng_inc, ct); |
|
286 |
|
287 int n_elt = (tmp > 0.0 ? static_cast<int> (tmp) : 0); |
398
|
288 |
3858
|
289 // If the final element that we would compute for the range is |
|
290 // equal to the limit of the range, or is an adjacent floating |
|
291 // point number, accept it. Otherwise, try a range with one |
|
292 // fewer element. If that fails, try again with one more |
|
293 // element. |
|
294 // |
|
295 // I'm not sure this is very good, but it seems to work better than |
|
296 // just using tfloor as above. For example, without it, the |
|
297 // expression 1.8:0.05:1.9 fails to produce the expected result of |
|
298 // [1.8, 1.85, 1.9]. |
3753
|
299 |
3858
|
300 if (! teq (rng_base + (n_elt - 1) * rng_inc, rng_limit)) |
|
301 { |
|
302 if (teq (rng_base + (n_elt - 2) * rng_inc, rng_limit)) |
|
303 n_elt--; |
|
304 else if (teq (rng_base + n_elt * rng_inc, rng_limit)) |
|
305 n_elt++; |
|
306 } |
|
307 |
|
308 retval = (n_elt >= INT_MAX - 1) ? -1 : n_elt; |
3753
|
309 } |
|
310 |
3858
|
311 return retval; |
3
|
312 } |
|
313 |
|
314 /* |
|
315 ;;; Local Variables: *** |
|
316 ;;; mode: C++ *** |
|
317 ;;; End: *** |
|
318 */ |