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 |
238
|
32 #include <iostream.h> |
3
|
33 #include <limits.h> |
|
34 |
|
35 #include "Range.h" |
645
|
36 #include "dMatrix.h" |
|
37 |
|
38 Matrix |
|
39 Range::matrix_value (void) const |
|
40 { |
|
41 Matrix retval; |
|
42 |
|
43 if (rng_nelem > 0) |
|
44 { |
|
45 retval.resize (1, rng_nelem); |
|
46 double b = rng_base; |
|
47 double increment = rng_inc; |
|
48 for (int i = 0; i < rng_nelem; i++) |
|
49 retval.elem (0, i) = b + i * increment; |
|
50 } |
|
51 |
|
52 return retval; |
|
53 } |
3
|
54 |
208
|
55 // NOTE: max and min only return useful values if nelem > 0. |
|
56 |
|
57 double |
|
58 Range::min (void) const |
|
59 { |
|
60 double retval = 0.0; |
|
61 if (rng_nelem > 0) |
|
62 { |
|
63 if (rng_inc > 0) |
|
64 retval = rng_base; |
|
65 else |
|
66 retval = rng_base + (rng_nelem - 1) * rng_inc; |
|
67 } |
|
68 return retval; |
|
69 } |
|
70 |
|
71 double |
|
72 Range::max (void) const |
|
73 { |
|
74 double retval = 0.0; |
|
75 if (rng_nelem > 0) |
|
76 { |
|
77 if (rng_inc > 0) |
|
78 retval = rng_base + (rng_nelem - 1) * rng_inc; |
|
79 else |
|
80 retval = rng_base; |
|
81 } |
|
82 return retval; |
|
83 } |
|
84 |
|
85 void |
|
86 Range::sort (void) |
|
87 { |
|
88 if (rng_base > rng_limit && rng_inc < 0.0) |
|
89 { |
|
90 double tmp = rng_base; |
|
91 rng_base = min (); |
|
92 rng_limit = tmp; |
|
93 rng_inc = -rng_inc; |
|
94 } |
|
95 } |
|
96 |
3
|
97 void |
|
98 Range::print_range (void) |
|
99 { |
208
|
100 cerr << "Range: rng_base = " << rng_base |
|
101 << " rng_limit " << rng_limit |
|
102 << " rng_inc " << rng_inc |
|
103 << " rng_nelem " << rng_nelem << "\n"; |
3
|
104 } |
|
105 |
|
106 ostream& |
|
107 operator << (ostream& os, const Range& a) |
|
108 { |
|
109 double b = a.base (); |
|
110 double increment = a.inc (); |
|
111 int num_elem = a.nelem (); |
|
112 |
|
113 for (int i = 0; i < num_elem; i++) |
|
114 os << b + i * increment << " "; |
|
115 |
|
116 os << "\n"; |
|
117 |
|
118 return os; |
|
119 } |
|
120 |
|
121 istream& |
|
122 operator >> (istream& is, Range& a) |
|
123 { |
208
|
124 is >> a.rng_base; |
3
|
125 if (is) |
|
126 { |
208
|
127 is >> a.rng_limit; |
3
|
128 if (is) |
|
129 { |
208
|
130 is >> a.rng_inc; |
|
131 a.rng_nelem = a.nelem_internal (); |
3
|
132 } |
|
133 } |
|
134 |
|
135 return is; |
|
136 } |
|
137 |
|
138 int |
|
139 Range::nelem_internal (void) const |
|
140 { |
398
|
141 // Find an approximate number of intervals, then do the best we can to |
|
142 // find the number of intervals that we would get if we had done |
3
|
143 // something like |
|
144 // |
|
145 // nelem = 0; |
|
146 // while (base + nelem * inc <= limit) |
|
147 // nelem++; |
|
148 // |
|
149 // (for limit > base && inc > 0) |
398
|
150 // |
|
151 // The number of elements in the range is one greater than the number |
|
152 // of intervals. |
3
|
153 |
398
|
154 // We can't have more than INT_MAX elements in the range. |
|
155 |
|
156 double d_n_intervals = (rng_limit - rng_base) / rng_inc; |
|
157 int max_intervals = INT_MAX - 1; |
|
158 double d_max_val = (double) max_intervals; |
3
|
159 |
398
|
160 if (d_n_intervals > d_max_val) |
3
|
161 return -1; |
|
162 |
398
|
163 int n_intervals = (d_n_intervals > 0) |
|
164 ? ((int) (d_n_intervals + 0.5)) |
|
165 : ((int) (d_n_intervals - 0.5)); |
|
166 |
208
|
167 if (rng_limit > rng_base && rng_inc > 0) |
3
|
168 { |
|
169 // Our approximation may have been too big. |
|
170 |
398
|
171 while (rng_base + n_intervals * rng_inc > rng_limit && n_intervals > 0) |
|
172 n_intervals--; |
|
173 |
|
174 // Now that we are close, get the actual number. Try to avoid |
|
175 // problems with extended precision registers. |
3
|
176 |
398
|
177 for (;;) |
|
178 { |
|
179 volatile double tmp_inc = (n_intervals + 1) * rng_inc; |
|
180 volatile double tmp_val = rng_base + tmp_inc; |
|
181 if (tmp_val <= rng_limit && n_intervals < max_intervals) |
|
182 n_intervals++; |
|
183 else |
|
184 break; |
|
185 } |
3
|
186 } |
208
|
187 else if (rng_limit < rng_base && rng_inc < 0) |
3
|
188 { |
|
189 // Our approximation may have been too big. |
|
190 |
398
|
191 while (rng_base + n_intervals * rng_inc < rng_limit && n_intervals > 0) |
|
192 n_intervals--; |
|
193 |
|
194 // Now that we are close, get the actual number. Try to avoid |
|
195 // problems with extended precision registers. |
3
|
196 |
398
|
197 for (;;) |
|
198 { |
|
199 volatile double tmp_inc = (n_intervals + 1) * rng_inc; |
|
200 volatile double tmp_val = rng_base + tmp_inc; |
|
201 if (tmp_val >= rng_limit && n_intervals < max_intervals) |
|
202 n_intervals++; |
|
203 else |
|
204 break; |
|
205 } |
3
|
206 } |
208
|
207 else if (rng_limit == rng_base) |
398
|
208 n_intervals = 0; |
3
|
209 else |
398
|
210 n_intervals = -1; |
3
|
211 |
398
|
212 return (n_intervals >= max_intervals) ? -1 : n_intervals + 1; |
3
|
213 } |
|
214 |
|
215 /* |
|
216 ;;; Local Variables: *** |
|
217 ;;; mode: C++ *** |
|
218 ;;; page-delimiter: "^/\\*" *** |
|
219 ;;; End: *** |
|
220 */ |