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 |
1546
|
32 #include <cfloat> |
1367
|
33 #include <climits> |
1546
|
34 #include <cmath> |
1367
|
35 |
238
|
36 #include <iostream.h> |
3
|
37 |
|
38 #include "Range.h" |
645
|
39 #include "dMatrix.h" |
|
40 |
|
41 Matrix |
|
42 Range::matrix_value (void) const |
|
43 { |
|
44 Matrix retval; |
|
45 |
|
46 if (rng_nelem > 0) |
|
47 { |
|
48 retval.resize (1, rng_nelem); |
|
49 double b = rng_base; |
|
50 double increment = rng_inc; |
|
51 for (int i = 0; i < rng_nelem; i++) |
|
52 retval.elem (0, i) = b + i * increment; |
|
53 } |
|
54 |
|
55 return retval; |
|
56 } |
3
|
57 |
208
|
58 // NOTE: max and min only return useful values if nelem > 0. |
|
59 |
|
60 double |
|
61 Range::min (void) const |
|
62 { |
|
63 double retval = 0.0; |
|
64 if (rng_nelem > 0) |
|
65 { |
|
66 if (rng_inc > 0) |
|
67 retval = rng_base; |
|
68 else |
|
69 retval = rng_base + (rng_nelem - 1) * rng_inc; |
|
70 } |
|
71 return retval; |
|
72 } |
|
73 |
|
74 double |
|
75 Range::max (void) const |
|
76 { |
|
77 double retval = 0.0; |
|
78 if (rng_nelem > 0) |
|
79 { |
|
80 if (rng_inc > 0) |
|
81 retval = rng_base + (rng_nelem - 1) * rng_inc; |
|
82 else |
|
83 retval = rng_base; |
|
84 } |
|
85 return retval; |
|
86 } |
|
87 |
|
88 void |
|
89 Range::sort (void) |
|
90 { |
|
91 if (rng_base > rng_limit && rng_inc < 0.0) |
|
92 { |
|
93 double tmp = rng_base; |
|
94 rng_base = min (); |
|
95 rng_limit = tmp; |
|
96 rng_inc = -rng_inc; |
|
97 } |
|
98 } |
|
99 |
3
|
100 void |
|
101 Range::print_range (void) |
|
102 { |
208
|
103 cerr << "Range: rng_base = " << rng_base |
|
104 << " rng_limit " << rng_limit |
|
105 << " rng_inc " << rng_inc |
|
106 << " rng_nelem " << rng_nelem << "\n"; |
3
|
107 } |
|
108 |
|
109 ostream& |
|
110 operator << (ostream& os, const Range& a) |
|
111 { |
|
112 double b = a.base (); |
|
113 double increment = a.inc (); |
|
114 int num_elem = a.nelem (); |
|
115 |
|
116 for (int i = 0; i < num_elem; i++) |
|
117 os << b + i * increment << " "; |
|
118 |
|
119 os << "\n"; |
|
120 |
|
121 return os; |
|
122 } |
|
123 |
|
124 istream& |
|
125 operator >> (istream& is, Range& a) |
|
126 { |
208
|
127 is >> a.rng_base; |
3
|
128 if (is) |
|
129 { |
208
|
130 is >> a.rng_limit; |
3
|
131 if (is) |
|
132 { |
208
|
133 is >> a.rng_inc; |
|
134 a.rng_nelem = a.nelem_internal (); |
3
|
135 } |
|
136 } |
|
137 |
|
138 return is; |
|
139 } |
|
140 |
1546
|
141 // C See Knuth, Art Of Computer Programming, Vol. 1, Problem 1.2.4-5. |
|
142 // C |
|
143 // C===Tolerant FLOOR function. |
|
144 // C |
|
145 // C X - is given as a Double Precision argument to be operated on. |
|
146 // C It is assumed that X is represented with M mantissa bits. |
|
147 // C CT - is given as a Comparison Tolerance such that |
|
148 // C 0.LT.CT.LE.3-SQRT(5)/2. If the relative difference between |
|
149 // C X and A whole number is less than CT, then TFLOOR is |
|
150 // C returned as this whole number. By treating the |
|
151 // C floating-point numbers as a finite ordered set note that |
|
152 // C the heuristic EPS=2.**(-(M-1)) and CT=3*EPS causes |
|
153 // C arguments of TFLOOR/TCEIL to be treated as whole numbers |
|
154 // C if they are exactly whole numbers or are immediately |
|
155 // C adjacent to whole number representations. Since EPS, the |
|
156 // C "distance" between floating-point numbers on the unit |
|
157 // C interval, and M, the number of bits in X'S mantissa, exist |
|
158 // C on every floating-point computer, TFLOOR/TCEIL are |
|
159 // C consistently definable on every floating-point computer. |
|
160 // C |
|
161 // C For more information see the following references: |
|
162 // C (1) P. E. Hagerty, "More On Fuzzy Floor And Ceiling," APL QUOTE |
|
163 // C QUAD 8(4):20-24, June 1978. Note that TFLOOR=FL5. |
|
164 // C (2) L. M. Breed, "Definitions For Fuzzy Floor And Ceiling", APL |
|
165 // C QUOTE QUAD 8(3):16-23, March 1978. This paper cites FL1 through |
|
166 // C FL5, the history of five years of evolutionary development of |
|
167 // C FL5 - the seven lines of code below - by open collaboration |
|
168 // C and corroboration of the mathematical-computing community. |
|
169 // C |
|
170 // C Penn State University Center for Academic Computing |
|
171 // C H. D. Knoble - August, 1978. |
|
172 |
|
173 static inline double |
|
174 tfloor (double x, double ct) |
|
175 { |
|
176 // C---------FLOOR(X) is the largest integer algebraically less than |
|
177 // C or equal to X; that is, the unfuzzy FLOOR function. |
|
178 |
|
179 // DINT (X) = X - DMOD (X, 1.0); |
|
180 // FLOOR (X) = DINT (X) - DMOD (2.0 + DSIGN (1.0, X), 3.0); |
|
181 |
|
182 // C---------Hagerty's FL5 function follows... |
|
183 |
|
184 double q = 1.0; |
|
185 |
|
186 if (x < 0.0) |
|
187 q = 1.0 - ct; |
|
188 |
|
189 double rmax = q / (2.0 - ct); |
|
190 |
|
191 double t1 = 1.0 + floor (x); |
|
192 t1 = (ct / q) * (t1 < 0.0 ? -t1 : t1); |
|
193 t1 = rmax < t1 ? rmax : t1; |
|
194 t1 = ct > t1 ? ct : t1; |
|
195 t1 = floor (x + t1); |
|
196 |
1555
|
197 if (x <= 0.0 || (t1 - x) < rmax) |
1546
|
198 return t1; |
|
199 else |
|
200 return t1 - 1.0; |
|
201 } |
|
202 |
|
203 static inline double |
|
204 tceil (double x, double ct) |
|
205 { |
|
206 return -tfloor (-x, ct); |
|
207 } |
|
208 |
|
209 static inline double |
|
210 round (double x, double ct) |
|
211 { |
|
212 return tfloor (x+0.5, ct); |
|
213 } |
|
214 |
1360
|
215 int |
|
216 Range::nelem_internal (void) const |
|
217 { |
1546
|
218 double ct = 3.0 * DBL_EPSILON; |
3
|
219 |
1555
|
220 double tmp = tfloor ((rng_limit - rng_base + rng_inc) / rng_inc, ct); |
3
|
221 |
1546
|
222 int n_intervals = (int) (tmp > 0.0 ? tmp : 0); |
398
|
223 |
1546
|
224 return (n_intervals >= INT_MAX - 1) ? -1 : n_intervals; |
3
|
225 } |
|
226 |
|
227 /* |
|
228 ;;; Local Variables: *** |
|
229 ;;; mode: C++ *** |
|
230 ;;; page-delimiter: "^/\\*" *** |
|
231 ;;; End: *** |
|
232 */ |