GNU Octave 7.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
lo-array-errwarn.cc
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////
2//
3// Copyright (C) 2016-2022 The Octave Project Developers
4//
5// See the file COPYRIGHT.md in the top-level directory of this
6// distribution or <https://octave.org/copyright/>.
7//
8// This file is part of Octave.
9//
10// Octave is free software: you can redistribute it and/or modify it
11// under the terms of the GNU General Public License as published by
12// the Free Software Foundation, either version 3 of the License, or
13// (at your option) any later version.
14//
15// Octave is distributed in the hope that it will be useful, but
16// WITHOUT ANY WARRANTY; without even the implied warranty of
17// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18// GNU General Public License for more details.
19//
20// You should have received a copy of the GNU General Public License
21// along with Octave; see the file COPYING. If not, see
22// <https://www.gnu.org/licenses/>.
23//
24////////////////////////////////////////////////////////////////////////
25
26#if defined (HAVE_CONFIG_H)
27# include "config.h"
28#endif
29
30#include <cinttypes>
31#include <cmath>
32
33#include <limits>
34#include <sstream>
35
36#include "lo-array-errwarn.h"
37#include "lo-error.h"
38
39namespace octave
40{
41 // Text constants used to shorten code below.
42
43 static const char *error_id_nonconformant_args
44 = "Octave:nonconformant-args";
45
47 = "Octave:index-out-of-bounds";
48
49 static const char *error_id_invalid_index = "Octave:invalid-index";
50
52 = "Octave:nearly-singular-matrix";
53
54 static const char *warning_id_singular_matrix = "Octave:singular-matrix";
55
56 void
58 {
59 (*current_liboctave_error_handler)
60 ("invalid conversion from NaN to logical");
61 }
62
63 void
65 {
66 (*current_liboctave_error_handler)
67 ("invalid conversion from NaN to character");
68 }
69
70 void
71 err_nonconformant (const char *op,
72 octave_idx_type op1_len, octave_idx_type op2_len)
73 {
74 const char *err_id = error_id_nonconformant_args;
75
76 (*current_liboctave_error_with_id_handler)
77 (err_id, "%s: nonconformant arguments (op1 len: %" OCTAVE_IDX_TYPE_FORMAT
78 ", op2 len: % " OCTAVE_IDX_TYPE_FORMAT ")",
79 op, op1_len, op2_len);
80 }
81
82 void
83 err_nonconformant (const char *op,
84 octave_idx_type op1_nr, octave_idx_type op1_nc,
85 octave_idx_type op2_nr, octave_idx_type op2_nc)
86 {
87 const char *err_id = error_id_nonconformant_args;
88
89 (*current_liboctave_error_with_id_handler)
90 (err_id, "%s: nonconformant arguments "
91 "(op1 is %" OCTAVE_IDX_TYPE_FORMAT "x%" OCTAVE_IDX_TYPE_FORMAT ", "
92 "op2 is %" OCTAVE_IDX_TYPE_FORMAT"x%" OCTAVE_IDX_TYPE_FORMAT ")",
93 op, op1_nr, op1_nc, op2_nr, op2_nc);
94 }
95
96 void
97 err_nonconformant (const char *op,
98 const dim_vector& op1_dims, const dim_vector& op2_dims)
99 {
100 const char *err_id = error_id_nonconformant_args;
101
102 std::string op1_dims_str = op1_dims.str ();
103 std::string op2_dims_str = op2_dims.str ();
104
105 (*current_liboctave_error_with_id_handler)
106 (err_id, "%s: nonconformant arguments (op1 is %s, op2 is %s)",
107 op, op1_dims_str.c_str (), op2_dims_str.c_str ());
108 }
109
110 void
112 octave_idx_type ext)
113 {
114 const char *err_id = error_id_index_out_of_bounds;
115
116 (*current_liboctave_error_with_id_handler)
117 (err_id, "A(%s) = []: index out of bounds: value %" OCTAVE_IDX_TYPE_FORMAT
118 " out of bound %" OCTAVE_IDX_TYPE_FORMAT,
119 is1d ? "I" : "..,I,..", idx, ext);
120 }
121
122 // Show the expression that caused the error, e.g., "A(-1,_)",
123 // "A(0+1i)", "A(_,3)". Show how many indices come before/after the
124 // offending one, e.g., (<error>), (<error>,_), or (_,<error>,...[x5]...)
125
126 std::string
128 {
129 std::ostringstream buf;
130
131 if (m_var.empty () || m_var == "<unknown>")
132 buf << "index ";
133 else
134 buf << m_var;
135
136 bool show_parens = m_dim > 0;
137
138 if (show_parens)
139 {
140 if (m_dim < 5)
141 {
142 buf << '(';
143
144 for (octave_idx_type i = 1; i < m_dim; i++)
145 buf << "_,";
146 }
147 else
148 buf << "(...[x" << m_dim - 1 << "]...";
149 }
150
151 buf << m_index;
152
153 if (show_parens)
154 {
155 if (m_nd - m_dim < 5)
156 {
157 for (octave_idx_type i = 0; i < m_nd - m_dim; i++)
158 buf << ",_";
159
160 if (m_nd >= m_dim)
161 buf << ')';
162 }
163 else
164 buf << "...[x" << m_nd - m_dim << "]...)";
165 }
166
167 return buf.str ();
168 }
169
171 {
172 public:
173
174 invalid_index (const std::string& value, octave_idx_type ndim,
175 octave_idx_type dimen)
176 : index_exception (value, ndim, dimen)
177 {
178 // Virtual, but the one we want to call is defined in this class.
180 }
181
182 void update_message (void)
183 {
184 static std::string exp
185 = std::to_string (std::numeric_limits<octave_idx_type>::digits);
186
187 set_message (expression ()
188 + ": subscripts must be either integers 1 to (2^" + exp
189 + ")-1 or logicals");
190 }
191
192 // ID of error to throw
193 const char * err_id (void) const
194 {
196 }
197 };
198
199 // Complain if an index is negative, fractional, or too big.
200
201 void
202 err_invalid_index (const std::string& idx, octave_idx_type nd,
203 octave_idx_type dim, const std::string&)
204 {
205 invalid_index e (idx, nd, dim);
206
207 throw e;
208 }
209
210 void
212 octave_idx_type dim, const std::string& var)
213 {
214 err_invalid_index (std::to_string (n + 1), nd, dim, var);
215 }
216
217 void
219 const std::string& var)
220 {
221 std::ostringstream buf;
222 buf << n + 1;
223
224 if (! std::isnan (n))
225 {
226 // if n not an integer, but would be printed as one, show diff
227 double nearest = std::floor (n + 1.5);
228 if (n + 1 != nearest && (buf.str ().find ('.') == std::string::npos))
229 buf << std::showpos << (n + 1 - nearest);
230 }
231
232 err_invalid_index (buf.str (), nd, dim, var);
233 }
234
235 // Complain for read access beyond the bounds of an array.
236
238 {
239 public:
240
241 out_of_range (const std::string& value, octave_idx_type nd,
243 const dim_vector& size)
244 : index_exception (value, nd, dim), m_size (size), m_extent (ext)
245 {
246 // Virtual, but the one we want to call is defined in this class.
248 }
249
250 void update_message (void)
251 {
252 set_message (expression () + ": out of bound "
253 + std::to_string (m_extent)
254 + " (dimensions are " + m_size.str ('x') + ")");
255 }
256
257 // ID of error to throw.
258 const char * err_id (void) const
259 {
261 }
262
263 private:
264
265 // Dimension of object being accessed.
267
268 // Length of dimension being accessed.
270 };
271
272 // Complain of an index that is out of range
273 void
275 octave_idx_type ext, const dim_vector& dv)
276 {
277 throw out_of_range (std::to_string (idx), nd, dim, ext, dv);
278 }
279
280 void
282 {
283 (*current_liboctave_error_with_id_handler)
284 ("Octave:invalid-resize",
285 "Invalid resizing operation or ambiguous assignment to an out-of-bounds array element");
286 }
287
288 void
289 warn_singular_matrix (double rcond)
290 {
291 if (rcond == 0.0)
292 {
293 (*current_liboctave_warning_with_id_handler)
295 "matrix singular to machine precision");
296 }
297 else
298 {
299 (*current_liboctave_warning_with_id_handler)
301 "matrix singular to machine precision, rcond = %g", rcond);
302 }
303 }
304
305 // DEPRECATED in Octave 6.
306
307 // Complain of an index that is out of range, but we don't know matrix size
308 void
310 octave_idx_type ext)
311 {
312 // The dim_vector setting here doesn't really make sense. However,
313 // this function has been deprecated and will be removed in version
314 // 8, so there's no need to attempt to fix it.
315
316 throw out_of_range (std::to_string (idx), nd, dim, ext,
317 dim_vector (1, 1, 1, 1, 1, 1, 1));
318 }
319}
320
321/* Tests in test/index.tst */
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:94
OCTAVE_API std::string str(char sep='x') const
Definition: dim-vector.cc:68
OCTAVE_API std::string expression(void) const
const char * err_id(void) const
invalid_index(const std::string &value, octave_idx_type ndim, octave_idx_type dimen)
out_of_range(const std::string &value, octave_idx_type nd, octave_idx_type dim, octave_idx_type ext, const dim_vector &size)
octave_idx_type m_extent
const char * err_id(void) const
bool isnan(bool)
Definition: lo-mappers.h:178
std::complex< T > floor(const std::complex< T > &x)
Definition: lo-mappers.h:130
static const char * error_id_index_out_of_bounds
static OCTAVE_NORETURN void err_index_out_of_range(void)
Definition: idx-vector.cc:52
void err_invalid_index(const std::string &idx, octave_idx_type nd, octave_idx_type dim, const std::string &)
void err_del_index_out_of_range(bool is1d, octave_idx_type idx, octave_idx_type ext)
void err_nan_to_logical_conversion(void)
void err_invalid_resize(void)
void warn_singular_matrix(double rcond)
static const char * error_id_nonconformant_args
static const char * warning_id_nearly_singular_matrix
static const char * warning_id_singular_matrix
void err_nan_to_character_conversion(void)
static const char * error_id_invalid_index
void err_nonconformant(const char *op, octave_idx_type op1_len, octave_idx_type op2_len)