43 {
return c ==
'i' || c ==
'j'; }
59 if (std::toupper (c) ==
'I')
65 if (std::tolower (c1) ==
'n' && std::tolower (c2) ==
'f')
71 is.setstate (std::ios::failbit);
86 if (c1 ==
'a' && c2 ==
'N')
92 is.setstate (std::ios::failbit);
101 static std::istringstream&
104 have_sign = imag =
false;
115 bool negative =
false;
118 if (c ==
'+' || c ==
'-')
144 num = negative ? -1.0 : 1.0;
149 if (std::tolower (c) !=
'n')
214 is.setstate (std::ios::failbit);
244 #if defined (HAVE_CXX_COMPLEX_SETTERS)
249 #elif defined (HAVE_CXX_COMPLEX_REFERENCE_ACCESSORS)
267 std::string str = str_arg;
271 str.erase (std::remove (str.begin (), str.end(),
','), str.end ());
272 std::istringstream is (str);
287 if (!
extract_num (is, num, i2, s2) || i1 == i2 || ! s2)
297 DEFUN (str2double, args, ,
299 @deftypefn {Built-in Function} {} str2double (@var{s})\n\
300 Convert a string to a real or complex number.\n\
302 The string must be in one of the following formats where a and b are real\n\
303 numbers and the complex unit is @qcode{'i'} or @qcode{'j'}:\n\
319 If present, a and/or b are of the form @nospell{[+-]d[,.]d[[eE][+-]d]} where\n\
320 the brackets indicate optional arguments and @qcode{'d'} indicates zero or\n\
321 more digits. The special input values @code{Inf}, @code{NaN}, and @code{NA}\n\
322 are also accepted.\n\
324 @var{s} may be a character string, character matrix, or cell array. For\n\
325 character arrays the conversion is repeated for every row, and a double or\n\
326 complex array is returned. Empty rows in @var{s} are deleted and not\n\
327 returned in the numeric array. For cell arrays each character string\n\
328 element is processed and a double or complex array of the same dimensions as\n\
329 @var{s} is returned.\n\
331 For unconvertible scalar or character string input @code{str2double} returns\n\
332 a NaN@. Similarly, for character array input @code{str2double} returns a\n\
333 NaN for any row of @var{s} that could not be converted. For a cell array,\n\
334 @code{str2double} returns a NaN for any element of @var{s} for which\n\
335 conversion fails. Note that numeric elements in a mixed string/numeric\n\
336 cell array are not strings and the conversion will fail for these elements\n\
339 @code{str2double} can replace @code{str2num}, and it avoids the security\n\
340 risk of using @code{eval} on unknown data.\n\
346 if (args.length () != 1)
348 else if (args(0).is_string ())
350 if (args(0).rows () == 0 || args(0).columns () == 0)
354 else if (args(0).
rows () == 1 && args(0).
ndims () == 2)
365 else if (args(0).is_cell ())
367 const Cell cell = args(0).cell_value ();
374 if (cell(i).is_string ())
static Complex str2double1(const std::string &str_arg)
octave_idx_type rows(void) const
OCTINTERP_API void print_usage(void)
octave_idx_type numel(void) const
Number of elements in the array.
#define DEFUN(name, args_name, nargout_name, doc)
static bool is_imag_unit(int c)
static std::istringstream & extract_num(std::istringstream &is, double &num, bool &imag, bool &have_sign)
const dim_vector & dims(void) const
Return a const-reference so that dims ()(i) works efficiently.
static double single_num(std::istringstream &is)
Array< U > map(F fcn) const
Apply function fcn to each element of the Array.
std::string string_value(bool force=false) const
static void set_component(Complex &c, double num, bool imag)
ColumnVector imag(const ComplexColumnVector &a)
static MArray< double > const octave_idx_type const octave_idx_type octave_idx_type octave_idx_type octave_idx_type c1
std::complex< double > Complex