3
|
1 // Range.cc -*- C++ -*- |
|
2 /* |
|
3 |
1011
|
4 Copyright (C) 1992, 1993, 1994, 1995 John W. Eaton |
3
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
|
10 Free Software Foundation; either version 2, or (at your option) any |
|
11 later version. |
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
|
19 along with Octave; see the file COPYING. If not, write to the Free |
1315
|
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
3
|
21 |
|
22 */ |
|
23 |
1296
|
24 #if defined (__GNUG__) |
|
25 #pragma implementation |
|
26 #endif |
|
27 |
238
|
28 #ifdef HAVE_CONFIG_H |
1192
|
29 #include <config.h> |
3
|
30 #endif |
|
31 |
1367
|
32 #include <climits> |
|
33 |
238
|
34 #include <iostream.h> |
3
|
35 |
|
36 #include "Range.h" |
645
|
37 #include "dMatrix.h" |
|
38 |
|
39 Matrix |
|
40 Range::matrix_value (void) const |
|
41 { |
|
42 Matrix retval; |
|
43 |
|
44 if (rng_nelem > 0) |
|
45 { |
|
46 retval.resize (1, rng_nelem); |
|
47 double b = rng_base; |
|
48 double increment = rng_inc; |
|
49 for (int i = 0; i < rng_nelem; i++) |
|
50 retval.elem (0, i) = b + i * increment; |
|
51 } |
|
52 |
|
53 return retval; |
|
54 } |
3
|
55 |
208
|
56 // NOTE: max and min only return useful values if nelem > 0. |
|
57 |
|
58 double |
|
59 Range::min (void) const |
|
60 { |
|
61 double retval = 0.0; |
|
62 if (rng_nelem > 0) |
|
63 { |
|
64 if (rng_inc > 0) |
|
65 retval = rng_base; |
|
66 else |
|
67 retval = rng_base + (rng_nelem - 1) * rng_inc; |
|
68 } |
|
69 return retval; |
|
70 } |
|
71 |
|
72 double |
|
73 Range::max (void) const |
|
74 { |
|
75 double retval = 0.0; |
|
76 if (rng_nelem > 0) |
|
77 { |
|
78 if (rng_inc > 0) |
|
79 retval = rng_base + (rng_nelem - 1) * rng_inc; |
|
80 else |
|
81 retval = rng_base; |
|
82 } |
|
83 return retval; |
|
84 } |
|
85 |
|
86 void |
|
87 Range::sort (void) |
|
88 { |
|
89 if (rng_base > rng_limit && rng_inc < 0.0) |
|
90 { |
|
91 double tmp = rng_base; |
|
92 rng_base = min (); |
|
93 rng_limit = tmp; |
|
94 rng_inc = -rng_inc; |
|
95 } |
|
96 } |
|
97 |
3
|
98 void |
|
99 Range::print_range (void) |
|
100 { |
208
|
101 cerr << "Range: rng_base = " << rng_base |
|
102 << " rng_limit " << rng_limit |
|
103 << " rng_inc " << rng_inc |
|
104 << " rng_nelem " << rng_nelem << "\n"; |
3
|
105 } |
|
106 |
|
107 ostream& |
|
108 operator << (ostream& os, const Range& a) |
|
109 { |
|
110 double b = a.base (); |
|
111 double increment = a.inc (); |
|
112 int num_elem = a.nelem (); |
|
113 |
|
114 for (int i = 0; i < num_elem; i++) |
|
115 os << b + i * increment << " "; |
|
116 |
|
117 os << "\n"; |
|
118 |
|
119 return os; |
|
120 } |
|
121 |
|
122 istream& |
|
123 operator >> (istream& is, Range& a) |
|
124 { |
208
|
125 is >> a.rng_base; |
3
|
126 if (is) |
|
127 { |
208
|
128 is >> a.rng_limit; |
3
|
129 if (is) |
|
130 { |
208
|
131 is >> a.rng_inc; |
|
132 a.rng_nelem = a.nelem_internal (); |
3
|
133 } |
|
134 } |
|
135 |
|
136 return is; |
|
137 } |
|
138 |
398
|
139 // Find an approximate number of intervals, then do the best we can to |
|
140 // find the number of intervals that we would get if we had done |
3
|
141 // something like |
|
142 // |
|
143 // nelem = 0; |
|
144 // while (base + nelem * inc <= limit) |
|
145 // nelem++; |
|
146 // |
|
147 // (for limit > base && inc > 0) |
398
|
148 // |
|
149 // The number of elements in the range is one greater than the number |
|
150 // of intervals. |
3
|
151 |
1360
|
152 int |
|
153 Range::nelem_internal (void) const |
|
154 { |
|
155 // We can't have more than INT_MAX elements in the range. |
398
|
156 |
|
157 double d_n_intervals = (rng_limit - rng_base) / rng_inc; |
|
158 int max_intervals = INT_MAX - 1; |
|
159 double d_max_val = (double) max_intervals; |
3
|
160 |
398
|
161 if (d_n_intervals > d_max_val) |
3
|
162 return -1; |
|
163 |
398
|
164 int n_intervals = (d_n_intervals > 0) |
|
165 ? ((int) (d_n_intervals + 0.5)) |
|
166 : ((int) (d_n_intervals - 0.5)); |
|
167 |
208
|
168 if (rng_limit > rng_base && rng_inc > 0) |
3
|
169 { |
1360
|
170 // Our approximation may have been too big. |
3
|
171 |
398
|
172 while (rng_base + n_intervals * rng_inc > rng_limit && n_intervals > 0) |
|
173 n_intervals--; |
|
174 |
1360
|
175 // Now that we are close, get the actual number. Try to avoid |
|
176 // problems with extended precision registers. |
3
|
177 |
398
|
178 for (;;) |
|
179 { |
|
180 volatile double tmp_inc = (n_intervals + 1) * rng_inc; |
|
181 volatile double tmp_val = rng_base + tmp_inc; |
|
182 if (tmp_val <= rng_limit && n_intervals < max_intervals) |
|
183 n_intervals++; |
|
184 else |
|
185 break; |
|
186 } |
3
|
187 } |
208
|
188 else if (rng_limit < rng_base && rng_inc < 0) |
3
|
189 { |
1360
|
190 // Our approximation may have been too big. |
3
|
191 |
398
|
192 while (rng_base + n_intervals * rng_inc < rng_limit && n_intervals > 0) |
|
193 n_intervals--; |
|
194 |
1360
|
195 // Now that we are close, get the actual number. Try to avoid |
|
196 // problems with extended precision registers. |
3
|
197 |
398
|
198 for (;;) |
|
199 { |
|
200 volatile double tmp_inc = (n_intervals + 1) * rng_inc; |
|
201 volatile double tmp_val = rng_base + tmp_inc; |
|
202 if (tmp_val >= rng_limit && n_intervals < max_intervals) |
|
203 n_intervals++; |
|
204 else |
|
205 break; |
|
206 } |
3
|
207 } |
208
|
208 else if (rng_limit == rng_base) |
398
|
209 n_intervals = 0; |
3
|
210 else |
398
|
211 n_intervals = -1; |
3
|
212 |
398
|
213 return (n_intervals >= max_intervals) ? -1 : n_intervals + 1; |
3
|
214 } |
|
215 |
|
216 /* |
|
217 ;;; Local Variables: *** |
|
218 ;;; mode: C++ *** |
|
219 ;;; page-delimiter: "^/\\*" *** |
|
220 ;;; End: *** |
|
221 */ |