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