Mercurial > octave
annotate liboctave/array/Range.cc @ 21229:a83e7a384ee0
create and install a subset of config.h in octave-config.h
* mk-octave-config-h.sh: New file.
* Makefile.am (EXTRA_DIST): Add mk-octave-config.h.sh to the list.
(octinclude_HEADERS): Add octave-config.h to the list.
(octave-config.h): New rule.
* common.mk (do_subst_config_vals, do_subst_cross_config_vals):
Don't substitute unused ENABLE options.
* configure.ac: Note the reason for using oct-conf-post.in.h.
Add OCTAVE_ prefix to ENABLE_BOUNDS_CHECK ENABLE_ATOMIC_REFCOUNT,
ENABLE_64, ENABLE_OPENMP, and ENABLE_FLOAT_TRUNCATE in calls to
AC_DEFINE. Change all uses.
* oct-conf-post.in.h: Define HAVE_OCTAVE_DEPRECATED_ATTR instead of
HAVE_ATTR_DEPRECATED. Likewise for HAVE_ATTR_NORETURN and
HAVE_ATTR_UNUSED. Change all uses.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Mon, 08 Feb 2016 17:30:29 -0500 |
parents | f7121e111991 |
children | 40de9f8f23a6 |
rev | line source |
---|---|
3 | 1 /* |
2 | |
19697
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
18852
diff
changeset
|
3 Copyright (C) 1993-2015 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 |
21202
f7121e111991
maint: indent #ifdef blocks in liboctave and src directories.
Rik <rik@octave.org>
parents:
21131
diff
changeset
|
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)) | |
20228
00cf2847355d
Deprecate Array::nelem() and Range::nelem() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19697
diff
changeset
|
47 && (NINTbig (rng_base) == rng_base || rng_numel < 1) |
00cf2847355d
Deprecate Array::nelem() and Range::nelem() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19697
diff
changeset
|
48 && (NINTbig (rng_inc) == rng_inc || rng_numel <= 1)); |
2383 | 49 } |
645 | 50 |
51 Matrix | |
52 Range::matrix_value (void) const | |
53 { | |
20974
1edf15793cac
maint: Use is_empty rather than "numel () == 0" for clarity.
Rik <rik@octave.org>
parents:
20531
diff
changeset
|
54 if (rng_numel > 0 && cache.is_empty ()) |
645 | 55 { |
20228
00cf2847355d
Deprecate Array::nelem() and Range::nelem() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19697
diff
changeset
|
56 cache.resize (1, rng_numel); |
20531
dcfbf4c1c3c8
eliminate trailing whitespace and tabs from sources
John W. Eaton <jwe@octave.org>
parents:
20513
diff
changeset
|
57 |
20513 | 58 // The first element must always be *exactly* the base. |
59 // E.g, -0 would otherwise become +0 in the loop (-0 + 0*increment). | |
60 cache(0) = rng_base; | |
61 | |
645 | 62 double b = rng_base; |
63 double increment = rng_inc; | |
20513 | 64 for (octave_idx_type i = 1; i < rng_numel - 1; i++) |
65 cache(i) = b + i * increment; | |
4791 | 66 |
20513 | 67 cache(rng_numel - 1) = rng_limit; |
645 | 68 } |
69 | |
4810 | 70 return cache; |
645 | 71 } |
3 | 72 |
9986
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
73 double |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
74 Range::checkelem (octave_idx_type i) const |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
75 { |
20228
00cf2847355d
Deprecate Array::nelem() and Range::nelem() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19697
diff
changeset
|
76 if (i < 0 || i >= rng_numel) |
21100
e39e05d90788
Switch gripe_XXX to either err_XXX or warn_XXX naming scheme.
Rik <rik@octave.org>
parents:
20974
diff
changeset
|
77 err_index_out_of_range (1, 1, i+1, rng_numel); |
9986
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
78 |
16169
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
79 if (i == 0) |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
80 return rng_base; |
20228
00cf2847355d
Deprecate Array::nelem() and Range::nelem() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19697
diff
changeset
|
81 else if (i < rng_numel - 1) |
16169
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
82 return rng_base + i * rng_inc; |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
83 else |
20513 | 84 return rng_limit; |
9986
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 |
16169
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
87 double |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
88 Range::elem (octave_idx_type i) const |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
89 { |
21229
a83e7a384ee0
create and install a subset of config.h in octave-config.h
John W. Eaton <jwe@octave.org>
parents:
21202
diff
changeset
|
90 #if defined (OCTAVE_ENABLE_BOUNDS_CHECK) |
16169
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
91 return checkelem (i); |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
92 #else |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
93 if (i == 0) |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
94 return rng_base; |
20228
00cf2847355d
Deprecate Array::nelem() and Range::nelem() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19697
diff
changeset
|
95 else if (i < rng_numel - 1) |
16169
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
96 return rng_base + i * rng_inc; |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
97 else |
20513 | 98 return rng_limit; |
16169
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
99 #endif |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
100 } |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
101 |
16186
b4a6895a9863
Use proper OO-class for __rangeidx_helper().
Rik <rik@octave.org>
parents:
16169
diff
changeset
|
102 // Helper class used solely for idx_vector.loop () function call |
b4a6895a9863
Use proper OO-class for __rangeidx_helper().
Rik <rik@octave.org>
parents:
16169
diff
changeset
|
103 class __rangeidx_helper |
9986
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
104 { |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
105 public: |
16186
b4a6895a9863
Use proper OO-class for __rangeidx_helper().
Rik <rik@octave.org>
parents:
16169
diff
changeset
|
106 __rangeidx_helper (double *a, double b, double i, double l, octave_idx_type n) |
16169
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
107 : 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
|
108 |
9986
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
109 void operator () (octave_idx_type i) |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
110 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
111 if (i == 0) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
112 *array++ = base; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
113 else if (i < nmax) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
114 *array++ = base + i * inc; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
115 else |
20513 | 116 *array++ = limit; |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
117 } |
16186
b4a6895a9863
Use proper OO-class for __rangeidx_helper().
Rik <rik@octave.org>
parents:
16169
diff
changeset
|
118 |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
119 private: |
16186
b4a6895a9863
Use proper OO-class for __rangeidx_helper().
Rik <rik@octave.org>
parents:
16169
diff
changeset
|
120 |
b4a6895a9863
Use proper OO-class for __rangeidx_helper().
Rik <rik@octave.org>
parents:
16169
diff
changeset
|
121 double *array, base, inc, limit; |
b4a6895a9863
Use proper OO-class for __rangeidx_helper().
Rik <rik@octave.org>
parents:
16169
diff
changeset
|
122 octave_idx_type nmax; |
b4a6895a9863
Use proper OO-class for __rangeidx_helper().
Rik <rik@octave.org>
parents:
16169
diff
changeset
|
123 |
9986
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
124 }; |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
125 |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
126 Array<double> |
9986
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
127 Range::index (const idx_vector& i) const |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
128 { |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
129 Array<double> retval; |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
130 |
20228
00cf2847355d
Deprecate Array::nelem() and Range::nelem() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19697
diff
changeset
|
131 octave_idx_type n = rng_numel; |
9986
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
132 |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
133 if (i.is_colon ()) |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
134 { |
20228
00cf2847355d
Deprecate Array::nelem() and Range::nelem() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19697
diff
changeset
|
135 retval = matrix_value ().reshape (dim_vector (rng_numel, 1)); |
9986
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
136 } |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
137 else |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
138 { |
10366
e5ae13b8b2c2
improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents:
10314
diff
changeset
|
139 if (i.extent (n) != n) |
21100
e39e05d90788
Switch gripe_XXX to either err_XXX or warn_XXX naming scheme.
Rik <rik@octave.org>
parents:
20974
diff
changeset
|
140 err_index_out_of_range (1, 1, i.extent (n), n); // throws |
10366
e5ae13b8b2c2
improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents:
10314
diff
changeset
|
141 |
9986
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
142 dim_vector rd = i.orig_dimensions (); |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
143 octave_idx_type il = i.length (n); |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
144 |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
145 // taken from Array.cc. |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
146 if (n != 1 && rd.is_vector ()) |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
147 rd = dim_vector (1, il); |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
148 |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
149 retval.clear (rd); |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
150 |
16186
b4a6895a9863
Use proper OO-class for __rangeidx_helper().
Rik <rik@octave.org>
parents:
16169
diff
changeset
|
151 // idx_vector loop across all values in i, |
b4a6895a9863
Use proper OO-class for __rangeidx_helper().
Rik <rik@octave.org>
parents:
16169
diff
changeset
|
152 // executing __rangeidx_helper (i) for each i |
b4a6895a9863
Use proper OO-class for __rangeidx_helper().
Rik <rik@octave.org>
parents:
16169
diff
changeset
|
153 i.loop (n, __rangeidx_helper (retval.fortran_vec (), |
20228
00cf2847355d
Deprecate Array::nelem() and Range::nelem() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19697
diff
changeset
|
154 rng_base, rng_inc, rng_limit, rng_numel)); |
9986
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 |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
157 return retval; |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
158 } |
4810 | 159 |
20228
00cf2847355d
Deprecate Array::nelem() and Range::nelem() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19697
diff
changeset
|
160 // NOTE: max and min only return useful values if numel > 0. |
00cf2847355d
Deprecate Array::nelem() and Range::nelem() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19697
diff
changeset
|
161 // do_minmax_body() in max.cc avoids calling Range::min/max if numel == 0. |
208 | 162 |
163 double | |
164 Range::min (void) const | |
165 { | |
166 double retval = 0.0; | |
20228
00cf2847355d
Deprecate Array::nelem() and Range::nelem() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19697
diff
changeset
|
167 if (rng_numel > 0) |
208 | 168 { |
169 if (rng_inc > 0) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
170 retval = rng_base; |
208 | 171 else |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
172 { |
20228
00cf2847355d
Deprecate Array::nelem() and Range::nelem() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19697
diff
changeset
|
173 retval = rng_base + (rng_numel - 1) * rng_inc; |
4791 | 174 |
20513 | 175 // Require '<=' test. See note in max (). |
16169
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
176 if (retval <= rng_limit) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
177 retval = rng_limit; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
178 } |
4791 | 179 |
208 | 180 } |
181 return retval; | |
182 } | |
183 | |
184 double | |
185 Range::max (void) const | |
186 { | |
187 double retval = 0.0; | |
20228
00cf2847355d
Deprecate Array::nelem() and Range::nelem() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19697
diff
changeset
|
188 if (rng_numel > 0) |
208 | 189 { |
190 if (rng_inc > 0) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
191 { |
20228
00cf2847355d
Deprecate Array::nelem() and Range::nelem() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19697
diff
changeset
|
192 retval = rng_base + (rng_numel - 1) * rng_inc; |
4791 | 193 |
20513 | 194 // On some machines (x86 with extended precision floating point |
195 // arithmetic, for example) it is possible that we can overshoot the | |
196 // limit by approximately the machine precision even though we were | |
197 // very careful in our calculation of the number of elements. | |
198 // Therefore, we clip the result to the limit if it overshoots. | |
199 // The test also includes equality (>= rng_limit) to have expressions | |
200 // such as -5:1:-0 result in a -0 endpoint. | |
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 } |
208 | 204 else |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
205 retval = rng_base; |
208 | 206 } |
207 return retval; | |
208 } | |
209 | |
210 void | |
7458 | 211 Range::sort_internal (bool ascending) |
208 | 212 { |
20513 | 213 if ((ascending && rng_base > rng_limit && rng_inc < 0.0) |
214 || (! ascending && rng_base < rng_limit && rng_inc > 0.0)) | |
208 | 215 { |
20513 | 216 std::swap (rng_base, rng_limit); |
7458 | 217 rng_inc = -rng_inc; |
218 clear_cache (); | |
219 } | |
220 } | |
221 | |
222 void | |
223 Range::sort_internal (Array<octave_idx_type>& sidx, bool ascending) | |
224 { | |
20228
00cf2847355d
Deprecate Array::nelem() and Range::nelem() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19697
diff
changeset
|
225 octave_idx_type nel = numel (); |
7458 | 226 |
227 sidx.resize (dim_vector (1, nel)); | |
228 | |
229 octave_idx_type *psidx = sidx.fortran_vec (); | |
230 | |
231 bool reverse = false; | |
232 | |
20513 | 233 if ((ascending && rng_base > rng_limit && rng_inc < 0.0) |
234 || (! ascending && rng_base < rng_limit && rng_inc > 0.0)) | |
7458 | 235 { |
20513 | 236 std::swap (rng_base, rng_limit); |
7458 | 237 rng_inc = -rng_inc; |
238 clear_cache (); | |
239 reverse = true; | |
240 } | |
241 | |
242 octave_idx_type tmp = reverse ? nel - 1 : 0; | |
7467 | 243 octave_idx_type stp = reverse ? -1 : 1; |
7458 | 244 |
7467 | 245 for (octave_idx_type i = 0; i < nel; i++, tmp += stp) |
7458 | 246 psidx[i] = tmp; |
247 | |
248 } | |
249 | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
250 Matrix |
7620
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7467
diff
changeset
|
251 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
|
252 { |
8971 | 253 return matrix_value ().diag (k); |
7620
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7467
diff
changeset
|
254 } |
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7467
diff
changeset
|
255 |
7458 | 256 Range |
257 Range::sort (octave_idx_type dim, sortmode mode) const | |
258 { | |
259 Range retval = *this; | |
260 | |
261 if (dim == 1) | |
262 { | |
7463
2467639bd8c0
eliminate UNDEFINED sort mode
John W. Eaton <jwe@octave.org>
parents:
7458
diff
changeset
|
263 if (mode == ASCENDING) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
264 retval.sort_internal (true); |
7463
2467639bd8c0
eliminate UNDEFINED sort mode
John W. Eaton <jwe@octave.org>
parents:
7458
diff
changeset
|
265 else if (mode == DESCENDING) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
266 retval.sort_internal (false); |
7458 | 267 } |
268 else if (dim != 0) | |
269 (*current_liboctave_error_handler) ("Range::sort: invalid dimension"); | |
270 | |
271 return retval; | |
272 } | |
273 | |
274 Range | |
275 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
|
276 sortmode mode) const |
7458 | 277 { |
278 Range retval = *this; | |
279 | |
280 if (dim == 1) | |
281 { | |
7463
2467639bd8c0
eliminate UNDEFINED sort mode
John W. Eaton <jwe@octave.org>
parents:
7458
diff
changeset
|
282 if (mode == ASCENDING) |
16169
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
283 retval.sort_internal (sidx, true); |
7463
2467639bd8c0
eliminate UNDEFINED sort mode
John W. Eaton <jwe@octave.org>
parents:
7458
diff
changeset
|
284 else if (mode == DESCENDING) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
285 retval.sort_internal (sidx, false); |
7458 | 286 } |
287 else if (dim != 0) | |
288 (*current_liboctave_error_handler) ("Range::sort: invalid dimension"); | |
289 | |
290 return retval; | |
208 | 291 } |
292 | |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8589
diff
changeset
|
293 sortmode |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8589
diff
changeset
|
294 Range::is_sorted (sortmode mode) const |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8589
diff
changeset
|
295 { |
20228
00cf2847355d
Deprecate Array::nelem() and Range::nelem() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19697
diff
changeset
|
296 if (rng_numel > 1 && rng_inc > 0) |
16169
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
297 mode = (mode == DESCENDING) ? UNSORTED : ASCENDING; |
20228
00cf2847355d
Deprecate Array::nelem() and Range::nelem() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19697
diff
changeset
|
298 else if (rng_numel > 1 && rng_inc < 0) |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8589
diff
changeset
|
299 mode = (mode == ASCENDING) ? UNSORTED : DESCENDING; |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8589
diff
changeset
|
300 else |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8589
diff
changeset
|
301 mode = mode ? mode : ASCENDING; |
8742
d2b06871afac
add missing return statement
Jaroslav Hajek <highegg@gmail.com>
parents:
8721
diff
changeset
|
302 |
d2b06871afac
add missing return statement
Jaroslav Hajek <highegg@gmail.com>
parents:
8721
diff
changeset
|
303 return mode; |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8589
diff
changeset
|
304 } |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8589
diff
changeset
|
305 |
20513 | 306 void |
307 Range::set_base (double b) | |
308 { | |
309 if (rng_base != b) | |
310 { | |
311 rng_base = b; | |
312 rng_numel = numel_internal (); | |
313 | |
314 double tmplimit = rng_limit; | |
315 | |
316 if (rng_inc > 0) | |
317 tmplimit = max (); | |
318 else | |
319 tmplimit = min (); | |
320 | |
321 if (tmplimit != rng_limit) | |
322 rng_limit = tmplimit; | |
323 | |
324 clear_cache (); | |
325 } | |
326 } | |
327 | |
328 void | |
329 Range::set_limit (double l) | |
330 { | |
331 if (rng_limit != l) | |
332 { | |
333 rng_limit = l; | |
334 rng_numel = numel_internal (); | |
335 | |
336 double tmplimit = rng_limit; | |
337 | |
338 if (rng_inc > 0) | |
339 tmplimit = max (); | |
340 else | |
341 tmplimit = min (); | |
342 | |
343 if (tmplimit != rng_limit) | |
344 rng_limit = tmplimit; | |
345 | |
346 clear_cache (); | |
347 } | |
348 } | |
349 | |
350 void | |
351 Range::set_inc (double i) | |
352 { | |
353 if (rng_inc != i) | |
354 { | |
355 rng_inc = i; | |
356 rng_numel = numel_internal (); | |
357 | |
358 double tmplimit = rng_limit; | |
359 | |
360 if (rng_inc > 0) | |
361 tmplimit = max (); | |
362 else | |
363 tmplimit = min (); | |
364 | |
365 if (tmplimit != rng_limit) | |
366 rng_limit = tmplimit; | |
367 | |
368 clear_cache (); | |
369 } | |
370 } | |
371 | |
3504 | 372 std::ostream& |
373 operator << (std::ostream& os, const Range& a) | |
3 | 374 { |
375 double b = a.base (); | |
376 double increment = a.inc (); | |
20228
00cf2847355d
Deprecate Array::nelem() and Range::nelem() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19697
diff
changeset
|
377 octave_idx_type num_elem = a.numel (); |
3 | 378 |
16169
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
379 if (num_elem > 1) |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
380 { |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
381 // 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
|
382 os << b << " "; |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
383 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
|
384 os << b + i * increment << " "; |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
385 } |
3 | 386 |
20513 | 387 // Print out exactly the last element, rather than a calculated last element. |
388 os << a.rng_limit << "\n"; | |
3 | 389 |
390 return os; | |
391 } | |
392 | |
3504 | 393 std::istream& |
394 operator >> (std::istream& is, Range& a) | |
3 | 395 { |
208 | 396 is >> a.rng_base; |
3 | 397 if (is) |
398 { | |
20513 | 399 double tmp_rng_limit; |
400 is >> tmp_rng_limit; | |
401 | |
3 | 402 if (is) |
20513 | 403 is >> a.rng_inc; |
404 | |
405 // Clip the rng_limit to the true limit and rebuild numel, clear cache | |
406 a.set_limit (tmp_rng_limit); | |
3 | 407 } |
408 | |
409 return is; | |
410 } | |
411 | |
2599 | 412 Range |
413 operator - (const Range& r) | |
414 { | |
20228
00cf2847355d
Deprecate Array::nelem() and Range::nelem() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19697
diff
changeset
|
415 return Range (-r.base (), -r.limit (), -r.inc (), r.numel ()); |
2599 | 416 } |
417 | |
8553
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
418 Range operator + (double x, const Range& r) |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
419 { |
20228
00cf2847355d
Deprecate Array::nelem() and Range::nelem() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19697
diff
changeset
|
420 Range result (x + r.base (), x + r.limit (), r.inc (), r.numel ()); |
00cf2847355d
Deprecate Array::nelem() and Range::nelem() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19697
diff
changeset
|
421 if (result.rng_numel < 0) |
8971 | 422 result.cache = x + r.matrix_value (); |
423 | |
424 return result; | |
8553
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
425 } |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
426 |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
427 Range operator + (const Range& r, double x) |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
428 { |
20228
00cf2847355d
Deprecate Array::nelem() and Range::nelem() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19697
diff
changeset
|
429 Range result (r.base () + x, r.limit () + x, r.inc (), r.numel ()); |
00cf2847355d
Deprecate Array::nelem() and Range::nelem() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19697
diff
changeset
|
430 if (result.rng_numel < 0) |
8971 | 431 result.cache = r.matrix_value () + x; |
432 | |
433 return result; | |
8553
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
434 } |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
435 |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
436 Range operator - (double x, const Range& r) |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
437 { |
20228
00cf2847355d
Deprecate Array::nelem() and Range::nelem() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19697
diff
changeset
|
438 Range result (x - r.base (), x - r.limit (), -r.inc (), r.numel ()); |
00cf2847355d
Deprecate Array::nelem() and Range::nelem() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19697
diff
changeset
|
439 if (result.rng_numel < 0) |
8971 | 440 result.cache = x - r.matrix_value (); |
441 | |
442 return result; | |
8553
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
443 } |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
444 |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
445 Range operator - (const Range& r, double x) |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
446 { |
20228
00cf2847355d
Deprecate Array::nelem() and Range::nelem() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19697
diff
changeset
|
447 Range result (r.base () - x, r.limit () - x, r.inc (), r.numel ()); |
00cf2847355d
Deprecate Array::nelem() and Range::nelem() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19697
diff
changeset
|
448 if (result.rng_numel < 0) |
8971 | 449 result.cache = r.matrix_value () - x; |
450 | |
451 return result; | |
8553
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
452 } |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
453 |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
454 Range operator * (double x, const Range& r) |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
455 { |
20228
00cf2847355d
Deprecate Array::nelem() and Range::nelem() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19697
diff
changeset
|
456 Range result (x * r.base (), x * r.limit (), x * r.inc (), r.numel ()); |
00cf2847355d
Deprecate Array::nelem() and Range::nelem() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19697
diff
changeset
|
457 if (result.rng_numel < 0) |
8971 | 458 result.cache = x * r.matrix_value (); |
459 | |
460 return result; | |
8553
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
461 } |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
462 |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
463 Range operator * (const Range& r, double x) |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
464 { |
20228
00cf2847355d
Deprecate Array::nelem() and Range::nelem() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19697
diff
changeset
|
465 Range result (r.base () * x, r.limit () * x, r.inc () * x, r.numel ()); |
00cf2847355d
Deprecate Array::nelem() and Range::nelem() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19697
diff
changeset
|
466 if (result.rng_numel < 0) |
8971 | 467 result.cache = r.matrix_value () * x; |
468 | |
469 return result; | |
8553
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
470 } |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
471 |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
472 |
1546 | 473 // C See Knuth, Art Of Computer Programming, Vol. 1, Problem 1.2.4-5. |
474 // C | |
475 // C===Tolerant FLOOR function. | |
476 // C | |
477 // C X - is given as a Double Precision argument to be operated on. | |
478 // C It is assumed that X is represented with M mantissa bits. | |
479 // C CT - is given as a Comparison Tolerance such that | |
480 // C 0.LT.CT.LE.3-SQRT(5)/2. If the relative difference between | |
481 // C X and A whole number is less than CT, then TFLOOR is | |
482 // C returned as this whole number. By treating the | |
483 // C floating-point numbers as a finite ordered set note that | |
484 // C the heuristic EPS=2.**(-(M-1)) and CT=3*EPS causes | |
485 // C arguments of TFLOOR/TCEIL to be treated as whole numbers | |
486 // C if they are exactly whole numbers or are immediately | |
487 // C adjacent to whole number representations. Since EPS, the | |
488 // C "distance" between floating-point numbers on the unit | |
489 // C interval, and M, the number of bits in X'S mantissa, exist | |
490 // C on every floating-point computer, TFLOOR/TCEIL are | |
491 // C consistently definable on every floating-point computer. | |
492 // C | |
493 // C For more information see the following references: | |
494 // C (1) P. E. Hagerty, "More On Fuzzy Floor And Ceiling," APL QUOTE | |
495 // C QUAD 8(4):20-24, June 1978. Note that TFLOOR=FL5. | |
496 // C (2) L. M. Breed, "Definitions For Fuzzy Floor And Ceiling", APL | |
497 // C QUOTE QUAD 8(3):16-23, March 1978. This paper cites FL1 through | |
498 // C FL5, the history of five years of evolutionary development of | |
499 // C FL5 - the seven lines of code below - by open collaboration | |
500 // C and corroboration of the mathematical-computing community. | |
501 // C | |
502 // C Penn State University Center for Academic Computing | |
503 // C H. D. Knoble - August, 1978. | |
504 | |
505 static inline double | |
506 tfloor (double x, double ct) | |
507 { | |
508 // C---------FLOOR(X) is the largest integer algebraically less than | |
509 // C or equal to X; that is, the unfuzzy FLOOR function. | |
510 | |
511 // DINT (X) = X - DMOD (X, 1.0); | |
512 // FLOOR (X) = DINT (X) - DMOD (2.0 + DSIGN (1.0, X), 3.0); | |
513 | |
514 // C---------Hagerty's FL5 function follows... | |
515 | |
516 double q = 1.0; | |
517 | |
518 if (x < 0.0) | |
519 q = 1.0 - ct; | |
520 | |
521 double rmax = q / (2.0 - ct); | |
522 | |
11450
5eb10763069f
substitute and use LAPACK_LIBS in mkoctfile script
John W. Eaton <jwe@octave.org>
parents:
10366
diff
changeset
|
523 double t1 = 1.0 + gnulib::floor (x); |
1546 | 524 t1 = (ct / q) * (t1 < 0.0 ? -t1 : t1); |
525 t1 = rmax < t1 ? rmax : t1; | |
526 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
|
527 t1 = gnulib::floor (x + t1); |
1546 | 528 |
1555 | 529 if (x <= 0.0 || (t1 - x) < rmax) |
1546 | 530 return t1; |
531 else | |
532 return t1 - 1.0; | |
533 } | |
534 | |
535 static inline double | |
536 tceil (double x, double ct) | |
537 { | |
538 return -tfloor (-x, ct); | |
539 } | |
540 | |
3753 | 541 static inline bool |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
542 teq (double u, double v, |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
543 double ct = 3.0 * std::numeric_limits<double>::epsilon ()) |
3753 | 544 { |
545 double tu = fabs (u); | |
546 double tv = fabs (v); | |
547 | |
548 return fabs (u - v) < ((tu > tv ? tu : tv) * ct); | |
549 } | |
550 | |
5275 | 551 octave_idx_type |
20228
00cf2847355d
Deprecate Array::nelem() and Range::nelem() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19697
diff
changeset
|
552 Range::numel_internal (void) const |
1360 | 553 { |
5275 | 554 octave_idx_type retval = -1; |
3 | 555 |
3858 | 556 if (rng_inc == 0 |
557 || (rng_limit > rng_base && rng_inc < 0) | |
558 || (rng_limit < rng_base && rng_inc > 0)) | |
559 { | |
560 retval = 0; | |
561 } | |
562 else | |
563 { | |
15220
61822c866ba1
use std::numeric_limits<T>::epsilon in C++ code
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
564 double ct = 3.0 * std::numeric_limits<double>::epsilon (); |
3 | 565 |
3858 | 566 double tmp = tfloor ((rng_limit - rng_base + rng_inc) / rng_inc, ct); |
567 | |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
568 octave_idx_type n_elt = (tmp > 0.0 ? static_cast<octave_idx_type> (tmp) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
569 : 0); |
398 | 570 |
3858 | 571 // If the final element that we would compute for the range is |
572 // equal to the limit of the range, or is an adjacent floating | |
573 // point number, accept it. Otherwise, try a range with one | |
574 // fewer element. If that fails, try again with one more | |
575 // element. | |
576 // | |
577 // I'm not sure this is very good, but it seems to work better than | |
578 // just using tfloor as above. For example, without it, the | |
579 // expression 1.8:0.05:1.9 fails to produce the expected result of | |
580 // [1.8, 1.85, 1.9]. | |
3753 | 581 |
3858 | 582 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
|
583 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
584 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
|
585 n_elt--; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
586 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
|
587 n_elt++; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
588 } |
3858 | 589 |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
590 retval = (n_elt < std::numeric_limits<octave_idx_type>::max () - 1) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
591 ? n_elt : -1; |
3753 | 592 } |
593 | |
3858 | 594 return retval; |
3 | 595 } |