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