Mercurial > hg > octave-lyh
annotate liboctave/array/Range.cc @ 16169:0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
* libinterp/interpfcn/pr-output.cc(octave_print_internal): print base
or limit of range rather than using expression base+i*increment
which can destroy the signbit of base/limit.
* liboctave/array/Range.cc(Range constructor): Move trivial 2-line constructor
to .h file.
* liboctave/array/Range.cc(matrix_value, checkelem): Return base for first
element of array. Return limit of range for end of array if appropriate.
* liboctave/array/Range.cc(elem): Move function from Range.h Return base for
first element of array. Return limit of range for end of array if appropriate.
* liboctave/array/Range.cc(_rangeindex_helper, index): Return base for first
element of array. Return limit of range for end of array if appropriate.
* liboctave/array/Range.cc(min, max): Use '<=' or '>=' tests to
return base or limit if appropriate.
* liboctave/array/Range.cc(is_sorted): Place more common test first in
if/else if/else tree.
* liboctave/array/Range.cc(operator <<): Return base for first
element of array. Return limit of range for end of array if appropriate.
liboctave/array/Range.h(Range constructor): Put trivial 2-line constructor
in .h file.
liboctave/array/Range.h(elem): Move function which has become more complicated
to Range.cc.
* test/range.tst: Add %!tests for corner cases of base and limit of range.
author | Rik <rik@octave.org> |
---|---|
date | Fri, 01 Mar 2013 14:06:02 -0800 |
parents | 648dabbb4c6b |
children | b4a6895a9863 |
rev | line source |
---|---|
3 | 1 /* |
2 | |
14138
72c96de7a403
maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents:
12326
diff
changeset
|
3 Copyright (C) 1993-2012 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> |
1367 | 28 |
3503 | 29 #include <iostream> |
6490 | 30 #include <limits> |
3 | 31 |
32 #include "Range.h" | |
7458 | 33 #include "lo-error.h" |
2383 | 34 #include "lo-mappers.h" |
7231 | 35 #include "lo-math.h" |
2383 | 36 #include "lo-utils.h" |
10366
e5ae13b8b2c2
improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents:
10314
diff
changeset
|
37 #include "Array-util.h" |
2383 | 38 |
39 bool | |
40 Range::all_elements_are_ints (void) const | |
41 { | |
42 // If the base and increment are ints, the final value in the range | |
6457 | 43 // will also be an integer, even if the limit is not. If there is one |
44 // or fewer elements only the base needs to be an integer | |
2383 | 45 |
46 return (! (xisnan (rng_base) || xisnan (rng_inc)) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
47 && (NINTbig (rng_base) == rng_base || rng_nelem < 1) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
48 && (NINTbig (rng_inc) == rng_inc || rng_nelem <= 1)); |
2383 | 49 } |
645 | 50 |
51 Matrix | |
52 Range::matrix_value (void) const | |
53 { | |
8971 | 54 if (rng_nelem > 0 && cache.nelem () == 0) |
645 | 55 { |
4810 | 56 cache.resize (1, rng_nelem); |
645 | 57 double b = rng_base; |
58 double increment = rng_inc; | |
16169
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
59 if (rng_nelem > 0) |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
60 { |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
61 // The first element must always be *exactly* the base. |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
62 // E.g, -0 would otherwise become +0 in the loop (-0 + 0*increment). |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
63 cache(0) = b; |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
64 for (octave_idx_type i = 1; i < rng_nelem; i++) |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
65 cache(i) = b + i * increment; |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
66 } |
4791 | 67 |
68 // On some machines (x86 with extended precision floating point | |
69 // arithmetic, for example) it is possible that we can overshoot | |
70 // the limit by approximately the machine precision even though | |
71 // we were very careful in our calculation of the number of | |
16169
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
72 // elements. The tests need equality (>= rng_limit or <= rng_limit) |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
73 // to have expressions like -5:1:-0 result in a -0 endpoint. |
4791 | 74 |
16169
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
75 if ((rng_inc > 0 && cache(rng_nelem-1) >= rng_limit) |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
76 || (rng_inc < 0 && cache(rng_nelem-1) <= rng_limit)) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
77 cache(rng_nelem-1) = rng_limit; |
645 | 78 } |
79 | |
4810 | 80 return cache; |
645 | 81 } |
3 | 82 |
9986
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
83 double |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
84 Range::checkelem (octave_idx_type i) const |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
85 { |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
86 if (i < 0 || i >= rng_nelem) |
10366
e5ae13b8b2c2
improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents:
10314
diff
changeset
|
87 gripe_index_out_of_range (1, 1, i+1, rng_nelem); |
9986
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
88 |
16169
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
89 if (i == 0) |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
90 return rng_base; |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
91 else if (i < rng_nelem - 1) |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
92 return rng_base + i * rng_inc; |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
93 else |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
94 { |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
95 double end = rng_base + i * rng_inc; |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
96 if ((rng_inc > 0 && end >= rng_limit) |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
97 || (rng_inc < 0 && end <= rng_limit)) |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
98 return rng_limit; |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
99 else |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
100 return end; |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
101 } |
9986
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
102 } |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
103 |
16169
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
104 double |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
105 Range::elem (octave_idx_type i) const |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
106 { |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
107 #if defined (BOUNDS_CHECKING) |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
108 return checkelem (i); |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
109 #else |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
110 if (i == 0) |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
111 return rng_base; |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
112 else if (i < rng_nelem - 1) |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
113 return rng_base + i * rng_inc; |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
114 else |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
115 { |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
116 double end = rng_base + i * rng_inc; |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
117 if ((rng_inc > 0 && end >= rng_limit) |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
118 || (rng_inc < 0 && end <= rng_limit)) |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
119 return rng_limit; |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
120 else |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
121 return end; |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
122 } |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
123 #endif |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
124 } |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
125 |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
126 // Pseudo-class used for idx_vector.loop () function call |
9986
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
127 struct _rangeidx_helper |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
128 { |
16169
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
129 double *array, base, inc, limit; |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
130 octave_idx_type nmax; |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
131 |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
132 _rangeidx_helper (double *a, double b, double i, double l, octave_idx_type n) |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
133 : array (a), base (b), inc (i), limit (l), nmax (n-1) { } |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
134 |
9986
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
135 void operator () (octave_idx_type i) |
16169
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
136 { |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
137 if (i == 0) |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
138 *array++ = base; |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
139 else if (i < nmax) |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
140 *array++ = base + i * inc; |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
141 else |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
142 { |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
143 double end = base + i * inc; |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
144 if ((inc > 0 && end >= limit) |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
145 || (inc < 0 && end <= limit)) |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
146 *array++ = limit; |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
147 else |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
148 *array++ = end; |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
149 } |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
150 } |
9986
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
151 }; |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
152 |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
153 Array<double> |
9986
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
154 Range::index (const idx_vector& i) const |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
155 { |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
156 Array<double> retval; |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
157 |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
158 octave_idx_type n = rng_nelem; |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
159 |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
160 if (i.is_colon ()) |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
161 { |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
162 retval = matrix_value ().reshape (dim_vector (rng_nelem, 1)); |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
163 } |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
164 else |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
165 { |
10366
e5ae13b8b2c2
improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents:
10314
diff
changeset
|
166 if (i.extent (n) != n) |
e5ae13b8b2c2
improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents:
10314
diff
changeset
|
167 gripe_index_out_of_range (1, 1, i.extent (n), n); // throws |
e5ae13b8b2c2
improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents:
10314
diff
changeset
|
168 |
9986
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
169 dim_vector rd = i.orig_dimensions (); |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
170 octave_idx_type il = i.length (n); |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
171 |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
172 // taken from Array.cc. |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
173 if (n != 1 && rd.is_vector ()) |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
174 rd = dim_vector (1, il); |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
175 |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
176 retval.clear (rd); |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
177 |
16169
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
178 // idx_vector loop across all values in i, executing _rangeidx_helper (i) foreach i |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
179 i.loop (n, _rangeidx_helper (retval.fortran_vec (), |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
180 rng_base, rng_inc, rng_limit, rng_nelem)); |
9986
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
181 } |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
182 |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
183 return retval; |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
184 } |
4810 | 185 |
208 | 186 // NOTE: max and min only return useful values if nelem > 0. |
187 | |
188 double | |
189 Range::min (void) const | |
190 { | |
191 double retval = 0.0; | |
192 if (rng_nelem > 0) | |
193 { | |
194 if (rng_inc > 0) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
195 retval = rng_base; |
208 | 196 else |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
197 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
198 retval = rng_base + (rng_nelem - 1) * rng_inc; |
4791 | 199 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
200 // See the note in the matrix_value method above. |
16169
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
201 if (retval <= rng_limit) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
202 retval = rng_limit; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
203 } |
4791 | 204 |
208 | 205 } |
206 return retval; | |
207 } | |
208 | |
209 double | |
210 Range::max (void) const | |
211 { | |
212 double retval = 0.0; | |
213 if (rng_nelem > 0) | |
214 { | |
215 if (rng_inc > 0) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
216 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
217 retval = rng_base + (rng_nelem - 1) * rng_inc; |
4791 | 218 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
219 // See the note in the matrix_value method above. |
16169
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
220 if (retval >= rng_limit) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
221 retval = rng_limit; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
222 } |
208 | 223 else |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
224 retval = rng_base; |
208 | 225 } |
226 return retval; | |
227 } | |
228 | |
229 void | |
7458 | 230 Range::sort_internal (bool ascending) |
208 | 231 { |
7458 | 232 if (ascending && rng_base > rng_limit && rng_inc < 0.0) |
208 | 233 { |
234 double tmp = rng_base; | |
235 rng_base = min (); | |
236 rng_limit = tmp; | |
237 rng_inc = -rng_inc; | |
4811 | 238 clear_cache (); |
208 | 239 } |
8565 | 240 else if (! ascending && rng_base < rng_limit && rng_inc > 0.0) |
7458 | 241 { |
242 double tmp = rng_limit; | |
243 rng_limit = min (); | |
244 rng_base = tmp; | |
245 rng_inc = -rng_inc; | |
246 clear_cache (); | |
247 } | |
248 } | |
249 | |
250 void | |
251 Range::sort_internal (Array<octave_idx_type>& sidx, bool ascending) | |
252 { | |
253 octave_idx_type nel = nelem (); | |
254 | |
255 sidx.resize (dim_vector (1, nel)); | |
256 | |
257 octave_idx_type *psidx = sidx.fortran_vec (); | |
258 | |
259 bool reverse = false; | |
260 | |
261 if (ascending && rng_base > rng_limit && rng_inc < 0.0) | |
262 { | |
263 double tmp = rng_base; | |
264 rng_base = min (); | |
265 rng_limit = tmp; | |
266 rng_inc = -rng_inc; | |
267 clear_cache (); | |
268 reverse = true; | |
269 } | |
270 else if (! ascending && rng_base < rng_limit && rng_inc > 0.0) | |
271 { | |
272 double tmp = rng_limit; | |
273 rng_limit = min (); | |
274 rng_base = tmp; | |
275 rng_inc = -rng_inc; | |
276 clear_cache (); | |
277 reverse = true; | |
278 } | |
279 | |
280 octave_idx_type tmp = reverse ? nel - 1 : 0; | |
7467 | 281 octave_idx_type stp = reverse ? -1 : 1; |
7458 | 282 |
7467 | 283 for (octave_idx_type i = 0; i < nel; i++, tmp += stp) |
7458 | 284 psidx[i] = tmp; |
285 | |
286 } | |
287 | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
288 Matrix |
7620
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7467
diff
changeset
|
289 Range::diag (octave_idx_type k) const |
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7467
diff
changeset
|
290 { |
8971 | 291 return matrix_value ().diag (k); |
7620
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7467
diff
changeset
|
292 } |
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7467
diff
changeset
|
293 |
7458 | 294 Range |
295 Range::sort (octave_idx_type dim, sortmode mode) const | |
296 { | |
297 Range retval = *this; | |
298 | |
299 if (dim == 1) | |
300 { | |
7463
2467639bd8c0
eliminate UNDEFINED sort mode
John W. Eaton <jwe@octave.org>
parents:
7458
diff
changeset
|
301 if (mode == ASCENDING) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
302 retval.sort_internal (true); |
7463
2467639bd8c0
eliminate UNDEFINED sort mode
John W. Eaton <jwe@octave.org>
parents:
7458
diff
changeset
|
303 else if (mode == DESCENDING) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
304 retval.sort_internal (false); |
7458 | 305 } |
306 else if (dim != 0) | |
307 (*current_liboctave_error_handler) ("Range::sort: invalid dimension"); | |
308 | |
309 return retval; | |
310 } | |
311 | |
312 Range | |
313 Range::sort (Array<octave_idx_type>& sidx, octave_idx_type dim, | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
314 sortmode mode) const |
7458 | 315 { |
316 Range retval = *this; | |
317 | |
318 if (dim == 1) | |
319 { | |
7463
2467639bd8c0
eliminate UNDEFINED sort mode
John W. Eaton <jwe@octave.org>
parents:
7458
diff
changeset
|
320 if (mode == ASCENDING) |
16169
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
321 retval.sort_internal (sidx, true); |
7463
2467639bd8c0
eliminate UNDEFINED sort mode
John W. Eaton <jwe@octave.org>
parents:
7458
diff
changeset
|
322 else if (mode == DESCENDING) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
323 retval.sort_internal (sidx, false); |
7458 | 324 } |
325 else if (dim != 0) | |
326 (*current_liboctave_error_handler) ("Range::sort: invalid dimension"); | |
327 | |
328 return retval; | |
208 | 329 } |
330 | |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8589
diff
changeset
|
331 sortmode |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8589
diff
changeset
|
332 Range::is_sorted (sortmode mode) const |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8589
diff
changeset
|
333 { |
16169
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
334 if (rng_nelem > 1 && rng_inc > 0) |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
335 mode = (mode == DESCENDING) ? UNSORTED : ASCENDING; |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
336 else if (rng_nelem > 1 && rng_inc < 0) |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8589
diff
changeset
|
337 mode = (mode == ASCENDING) ? UNSORTED : DESCENDING; |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8589
diff
changeset
|
338 else |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8589
diff
changeset
|
339 mode = mode ? mode : ASCENDING; |
8742
d2b06871afac
add missing return statement
Jaroslav Hajek <highegg@gmail.com>
parents:
8721
diff
changeset
|
340 |
d2b06871afac
add missing return statement
Jaroslav Hajek <highegg@gmail.com>
parents:
8721
diff
changeset
|
341 return mode; |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8589
diff
changeset
|
342 } |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8589
diff
changeset
|
343 |
3504 | 344 std::ostream& |
345 operator << (std::ostream& os, const Range& a) | |
3 | 346 { |
347 double b = a.base (); | |
348 double increment = a.inc (); | |
5275 | 349 octave_idx_type num_elem = a.nelem (); |
3 | 350 |
16169
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
351 if (num_elem > 1) |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
352 { |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
353 // First element must be the base *exactly* (-0). |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
354 os << b << " "; |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
355 for (octave_idx_type i = 1; i < num_elem-1; i++) |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
356 os << b + i * increment << " "; |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
357 } |
3 | 358 |
16169
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
359 // Prevent overshoot. See comment in the matrix_value method above. |
4791 | 360 os << (increment > 0 ? a.max () : a.min ()) << "\n"; |
3 | 361 |
362 return os; | |
363 } | |
364 | |
3504 | 365 std::istream& |
366 operator >> (std::istream& is, Range& a) | |
3 | 367 { |
208 | 368 is >> a.rng_base; |
3 | 369 if (is) |
370 { | |
208 | 371 is >> a.rng_limit; |
3 | 372 if (is) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
373 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
374 is >> a.rng_inc; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
375 a.rng_nelem = a.nelem_internal (); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
376 } |
3 | 377 } |
378 | |
379 return is; | |
380 } | |
381 | |
2599 | 382 Range |
383 operator - (const Range& r) | |
384 { | |
8589
0131fa223dbc
make length invariant in range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
8565
diff
changeset
|
385 return Range (-r.base (), -r.inc (), r.nelem ()); |
2599 | 386 } |
387 | |
8553
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
388 Range operator + (double x, const Range& r) |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
389 { |
8971 | 390 Range result (x + r.base (), r.inc (), r.nelem ()); |
391 if (result.rng_nelem < 0) | |
392 result.cache = x + r.matrix_value (); | |
393 | |
394 return result; | |
8553
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
395 } |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
396 |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
397 Range operator + (const Range& r, double x) |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
398 { |
8971 | 399 Range result (r.base () + x, r.inc (), r.nelem ()); |
400 if (result.rng_nelem < 0) | |
401 result.cache = r.matrix_value () + x; | |
402 | |
403 return result; | |
8553
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
404 } |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
405 |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
406 Range operator - (double x, const Range& r) |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
407 { |
8971 | 408 Range result (x - r.base (), -r.inc (), r.nelem ()); |
409 if (result.rng_nelem < 0) | |
410 result.cache = x - r.matrix_value (); | |
411 | |
412 return result; | |
8553
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
413 } |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
414 |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
415 Range operator - (const Range& r, double x) |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
416 { |
8971 | 417 Range result (r.base () - x, r.inc (), r.nelem ()); |
418 if (result.rng_nelem < 0) | |
419 result.cache = r.matrix_value () - x; | |
420 | |
421 return result; | |
8553
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
422 } |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
423 |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
424 Range operator * (double x, const Range& r) |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
425 { |
8971 | 426 Range result (x * r.base (), x * r.inc (), r.nelem ()); |
427 if (result.rng_nelem < 0) | |
428 result.cache = x * r.matrix_value (); | |
429 | |
430 return result; | |
8553
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
431 } |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
432 |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
433 Range operator * (const Range& r, double x) |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
434 { |
8971 | 435 Range result (r.base () * x, r.inc () * x, r.nelem ()); |
436 if (result.rng_nelem < 0) | |
437 result.cache = r.matrix_value () * x; | |
438 | |
439 return result; | |
8553
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
440 } |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
441 |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
442 |
1546 | 443 // C See Knuth, Art Of Computer Programming, Vol. 1, Problem 1.2.4-5. |
444 // C | |
445 // C===Tolerant FLOOR function. | |
446 // C | |
447 // C X - is given as a Double Precision argument to be operated on. | |
448 // C It is assumed that X is represented with M mantissa bits. | |
449 // C CT - is given as a Comparison Tolerance such that | |
450 // C 0.LT.CT.LE.3-SQRT(5)/2. If the relative difference between | |
451 // C X and A whole number is less than CT, then TFLOOR is | |
452 // C returned as this whole number. By treating the | |
453 // C floating-point numbers as a finite ordered set note that | |
454 // C the heuristic EPS=2.**(-(M-1)) and CT=3*EPS causes | |
455 // C arguments of TFLOOR/TCEIL to be treated as whole numbers | |
456 // C if they are exactly whole numbers or are immediately | |
457 // C adjacent to whole number representations. Since EPS, the | |
458 // C "distance" between floating-point numbers on the unit | |
459 // C interval, and M, the number of bits in X'S mantissa, exist | |
460 // C on every floating-point computer, TFLOOR/TCEIL are | |
461 // C consistently definable on every floating-point computer. | |
462 // C | |
463 // C For more information see the following references: | |
464 // C (1) P. E. Hagerty, "More On Fuzzy Floor And Ceiling," APL QUOTE | |
465 // C QUAD 8(4):20-24, June 1978. Note that TFLOOR=FL5. | |
466 // C (2) L. M. Breed, "Definitions For Fuzzy Floor And Ceiling", APL | |
467 // C QUOTE QUAD 8(3):16-23, March 1978. This paper cites FL1 through | |
468 // C FL5, the history of five years of evolutionary development of | |
469 // C FL5 - the seven lines of code below - by open collaboration | |
470 // C and corroboration of the mathematical-computing community. | |
471 // C | |
472 // C Penn State University Center for Academic Computing | |
473 // C H. D. Knoble - August, 1978. | |
474 | |
475 static inline double | |
476 tfloor (double x, double ct) | |
477 { | |
478 // C---------FLOOR(X) is the largest integer algebraically less than | |
479 // C or equal to X; that is, the unfuzzy FLOOR function. | |
480 | |
481 // DINT (X) = X - DMOD (X, 1.0); | |
482 // FLOOR (X) = DINT (X) - DMOD (2.0 + DSIGN (1.0, X), 3.0); | |
483 | |
484 // C---------Hagerty's FL5 function follows... | |
485 | |
486 double q = 1.0; | |
487 | |
488 if (x < 0.0) | |
489 q = 1.0 - ct; | |
490 | |
491 double rmax = q / (2.0 - ct); | |
492 | |
11450
5eb10763069f
substitute and use LAPACK_LIBS in mkoctfile script
John W. Eaton <jwe@octave.org>
parents:
10366
diff
changeset
|
493 double t1 = 1.0 + gnulib::floor (x); |
1546 | 494 t1 = (ct / q) * (t1 < 0.0 ? -t1 : t1); |
495 t1 = rmax < t1 ? rmax : t1; | |
496 t1 = ct > t1 ? ct : t1; | |
11450
5eb10763069f
substitute and use LAPACK_LIBS in mkoctfile script
John W. Eaton <jwe@octave.org>
parents:
10366
diff
changeset
|
497 t1 = gnulib::floor (x + t1); |
1546 | 498 |
1555 | 499 if (x <= 0.0 || (t1 - x) < rmax) |
1546 | 500 return t1; |
501 else | |
502 return t1 - 1.0; | |
503 } | |
504 | |
505 static inline double | |
506 tceil (double x, double ct) | |
507 { | |
508 return -tfloor (-x, ct); | |
509 } | |
510 | |
3753 | 511 static inline bool |
15220
61822c866ba1
use std::numeric_limits<T>::epsilon in C++ code
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
512 teq (double u, double v, double ct = 3.0 * std::numeric_limits<double>::epsilon ()) |
3753 | 513 { |
514 double tu = fabs (u); | |
515 double tv = fabs (v); | |
516 | |
517 return fabs (u - v) < ((tu > tv ? tu : tv) * ct); | |
518 } | |
519 | |
5275 | 520 octave_idx_type |
1360 | 521 Range::nelem_internal (void) const |
522 { | |
5275 | 523 octave_idx_type retval = -1; |
3 | 524 |
3858 | 525 if (rng_inc == 0 |
526 || (rng_limit > rng_base && rng_inc < 0) | |
527 || (rng_limit < rng_base && rng_inc > 0)) | |
528 { | |
529 retval = 0; | |
530 } | |
531 else | |
532 { | |
15220
61822c866ba1
use std::numeric_limits<T>::epsilon in C++ code
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
533 double ct = 3.0 * std::numeric_limits<double>::epsilon (); |
3 | 534 |
3858 | 535 double tmp = tfloor ((rng_limit - rng_base + rng_inc) / rng_inc, ct); |
536 | |
5275 | 537 octave_idx_type n_elt = (tmp > 0.0 ? static_cast<octave_idx_type> (tmp) : 0); |
398 | 538 |
3858 | 539 // If the final element that we would compute for the range is |
540 // equal to the limit of the range, or is an adjacent floating | |
541 // point number, accept it. Otherwise, try a range with one | |
542 // fewer element. If that fails, try again with one more | |
543 // element. | |
544 // | |
545 // I'm not sure this is very good, but it seems to work better than | |
546 // just using tfloor as above. For example, without it, the | |
547 // expression 1.8:0.05:1.9 fails to produce the expected result of | |
548 // [1.8, 1.85, 1.9]. | |
3753 | 549 |
3858 | 550 if (! teq (rng_base + (n_elt - 1) * rng_inc, rng_limit)) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
551 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
552 if (teq (rng_base + (n_elt - 2) * rng_inc, rng_limit)) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
553 n_elt--; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
554 else if (teq (rng_base + n_elt * rng_inc, rng_limit)) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
555 n_elt++; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
556 } |
3858 | 557 |
16169
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
558 retval = (n_elt < std::numeric_limits<octave_idx_type>::max () - 1) ? n_elt : -1; |
3753 | 559 } |
560 | |
3858 | 561 return retval; |
3 | 562 } |