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