annotate libinterp/corefcn/str2double.cc @ 20587:f90c8372b7ba

eliminate many more simple uses of error_state * Cell.cc, __ichol__.cc, __ilu__.cc, balance.cc, bsxfun.cc, colloc.cc, det.cc, dlmread.cc, dynamic-ld.cc, eig.cc, fft.cc, fft2.cc, fftn.cc, gcd.cc, getgrent.cc, getpwent.cc, givens.cc, hess.cc, input.cc, levenshtein.cc, load-path.cc, lookup.cc, ls-mat-ascii.cc, ls-mat4.cc, lsode.cc, lu.cc, max.cc, md5sum.cc, mex.cc, pager.cc, pinv.cc, pr-output.cc, qz.cc, schur.cc, sparse.cc, sqrtm.cc, str2double.cc, strfns.cc, sub2ind.cc, sysdep.cc, time.cc, toplev.cc, tril.cc, tsearch.cc, typecast.cc, __init_gnuplot__.cc, __magick_read__.cc, __osmesa_print__.cc, amd.cc, audiodevinfo.cc, dmperm.cc, fftw.cc, symrcm.cc, ov-base-diag.cc, ov-base-sparse.cc, ov-base.cc, ov-bool-sparse.cc, ov-builtin.cc, ov-complex.cc, ov-cx-diag.cc, ov-cx-mat.cc, ov-cx-sparse.cc, ov-fcn-handle.cc, ov-fcn-inline.cc, ov-float.cc, ov-flt-complex.cc, ov-flt-cx-diag.cc, ov-flt-cx-mat.cc, ov-flt-re-diag.cc, ov-flt-re-mat.cc, ov-lazy-idx.cc, ov-mex-fcn.cc, ov-perm.cc, ov-range.cc, ov-re-diag.cc, ov-re-mat.cc, ov-re-sparse.cc, ov-scalar.cc, ov-str-mat.cc, op-bm-b.cc, op-bm-bm.cc, op-sbm-b.cc, op-sbm-bm.cc, op-str-m.cc, op-str-s.cc, oct-parse.in.yy, pt-cbinop.cc, pt-colon.cc, pt-decl.cc, pt-exp.cc, pt-id.cc, pt-misc.cc, pt-select.cc, pt-unop.cc: Eliminate simple uses of error_state.
author John W. Eaton <jwe@octave.org>
date Mon, 05 Oct 2015 19:29:36 -0400
parents 4f45eaf83908
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
10356
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
1 /*
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
2
19731
4197fc428c7d maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents: 18710
diff changeset
3 Copyright (C) 2010-2015 Jaroslav Hajek
10356
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
4 Copyright (C) 2010 VZLU Prague
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
5
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
6 This file is part of Octave.
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
7
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
8 Octave is free software; you can redistribute it and/or modify it
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
9 under the terms of the GNU General Public License as published by the
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
10 Free Software Foundation; either version 3 of the License, or (at your
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
11 option) any later version.
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
12
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
13 Octave is distributed in the hope that it will be useful, but WITHOUT
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
16 for more details.
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
17
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
18 You should have received a copy of the GNU General Public License
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
19 along with Octave; see the file COPYING. If not, see
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
20 <http://www.gnu.org/licenses/>.
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
21
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
22 */
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
23
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
24 #ifdef HAVE_CONFIG_H
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
25 #include <config.h>
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
26 #endif
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
27
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
28 #include <string>
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
29 #include <cctype>
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
30 #include <sstream>
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
31 #include <algorithm>
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
32
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
33 #include "lo-ieee.h"
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
34
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
35 #include "Cell.h"
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
36 #include "ov.h"
15039
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14854
diff changeset
37 #include "defun.h"
10356
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
38 #include "gripes.h"
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
39 #include "utils.h"
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
40
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11553
diff changeset
41 static inline bool
10356
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
42 is_imag_unit (int c)
10357
7658cd4bdcf2 handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents: 10356
diff changeset
43 { return c == 'i' || c == 'j'; }
7658cd4bdcf2 handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents: 10356
diff changeset
44
17379
b91f29f021a3 str2double.cc: miscellaneous simplifications to code.
Rik <rik@octave.org>
parents: 17281
diff changeset
45 static double
b91f29f021a3 str2double.cc: miscellaneous simplifications to code.
Rik <rik@octave.org>
parents: 17281
diff changeset
46 single_num (std::istringstream& is)
10357
7658cd4bdcf2 handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents: 10356
diff changeset
47 {
18710
5bd1ca29c5f0 Clean up questionable code bits identified by clang sanitize.
Rik <rik@octave.org>
parents: 18099
diff changeset
48 double num = 0.0;
17379
b91f29f021a3 str2double.cc: miscellaneous simplifications to code.
Rik <rik@octave.org>
parents: 17281
diff changeset
49
10357
7658cd4bdcf2 handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents: 10356
diff changeset
50 char c = is.peek ();
14077
b6eeeb67fa3f str2double: return NaN for things like "1 2 3 4" (bug #34713).
John W. Eaton <jwe@octave.org>
parents: 13141
diff changeset
51
b6eeeb67fa3f str2double: return NaN for things like "1 2 3 4" (bug #34713).
John W. Eaton <jwe@octave.org>
parents: 13141
diff changeset
52 // Skip spaces.
b6eeeb67fa3f str2double: return NaN for things like "1 2 3 4" (bug #34713).
John W. Eaton <jwe@octave.org>
parents: 13141
diff changeset
53 while (isspace (c))
b6eeeb67fa3f str2double: return NaN for things like "1 2 3 4" (bug #34713).
John W. Eaton <jwe@octave.org>
parents: 13141
diff changeset
54 {
b6eeeb67fa3f str2double: return NaN for things like "1 2 3 4" (bug #34713).
John W. Eaton <jwe@octave.org>
parents: 13141
diff changeset
55 is.get ();
b6eeeb67fa3f str2double: return NaN for things like "1 2 3 4" (bug #34713).
John W. Eaton <jwe@octave.org>
parents: 13141
diff changeset
56 c = is.peek ();
b6eeeb67fa3f str2double: return NaN for things like "1 2 3 4" (bug #34713).
John W. Eaton <jwe@octave.org>
parents: 13141
diff changeset
57 }
b6eeeb67fa3f str2double: return NaN for things like "1 2 3 4" (bug #34713).
John W. Eaton <jwe@octave.org>
parents: 13141
diff changeset
58
14702
a08f6e17336e str2double.cc: Case-insensitive detection of 'Inf' (Bug #36536).
Rik <octave@nomad.inbox5.com>
parents: 14501
diff changeset
59 if (std::toupper (c) == 'I')
10357
7658cd4bdcf2 handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents: 10356
diff changeset
60 {
7658cd4bdcf2 handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents: 10356
diff changeset
61 // It's infinity.
7658cd4bdcf2 handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents: 10356
diff changeset
62 is.get ();
18099
6a71e5030df5 Follow coding convention of defining and initializing only 1 variable per line in liboctinterp.
Rik <rik@octave.org>
parents: 17787
diff changeset
63 char c1 = is.get ();
6a71e5030df5 Follow coding convention of defining and initializing only 1 variable per line in liboctinterp.
Rik <rik@octave.org>
parents: 17787
diff changeset
64 char c2 = is.get ();
14702
a08f6e17336e str2double.cc: Case-insensitive detection of 'Inf' (Bug #36536).
Rik <octave@nomad.inbox5.com>
parents: 14501
diff changeset
65 if (std::tolower (c1) == 'n' && std::tolower (c2) == 'f')
10357
7658cd4bdcf2 handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents: 10356
diff changeset
66 {
7658cd4bdcf2 handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents: 10356
diff changeset
67 num = octave_Inf;
14702
a08f6e17336e str2double.cc: Case-insensitive detection of 'Inf' (Bug #36536).
Rik <octave@nomad.inbox5.com>
parents: 14501
diff changeset
68 is.peek (); // May set EOF bit.
10357
7658cd4bdcf2 handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents: 10356
diff changeset
69 }
7658cd4bdcf2 handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents: 10356
diff changeset
70 else
7658cd4bdcf2 handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents: 10356
diff changeset
71 is.setstate (std::ios::failbit); // indicate that read has failed.
7658cd4bdcf2 handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents: 10356
diff changeset
72 }
7658cd4bdcf2 handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents: 10356
diff changeset
73 else if (c == 'N')
7658cd4bdcf2 handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents: 10356
diff changeset
74 {
11414
5e6ef42a2445 Overhaul str2double function fixing bug and docstring and adding 'NA' processing.
Rik <octave@nomad.inbox5.com>
parents: 10846
diff changeset
75 // It's NA or NaN
10357
7658cd4bdcf2 handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents: 10356
diff changeset
76 is.get ();
11414
5e6ef42a2445 Overhaul str2double function fixing bug and docstring and adding 'NA' processing.
Rik <octave@nomad.inbox5.com>
parents: 10846
diff changeset
77 char c1 = is.get ();
5e6ef42a2445 Overhaul str2double function fixing bug and docstring and adding 'NA' processing.
Rik <octave@nomad.inbox5.com>
parents: 10846
diff changeset
78 if (c1 == 'A')
10357
7658cd4bdcf2 handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents: 10356
diff changeset
79 {
11414
5e6ef42a2445 Overhaul str2double function fixing bug and docstring and adding 'NA' processing.
Rik <octave@nomad.inbox5.com>
parents: 10846
diff changeset
80 num = octave_NA;
14091
c3ea07298af4 str2double: correctly handle things like ["1"; "2 3"; "4i"]
John W. Eaton <jwe@octave.org>
parents: 14077
diff changeset
81 is.peek (); // May set EOF bit.
10357
7658cd4bdcf2 handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents: 10356
diff changeset
82 }
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11553
diff changeset
83 else
11414
5e6ef42a2445 Overhaul str2double function fixing bug and docstring and adding 'NA' processing.
Rik <octave@nomad.inbox5.com>
parents: 10846
diff changeset
84 {
5e6ef42a2445 Overhaul str2double function fixing bug and docstring and adding 'NA' processing.
Rik <octave@nomad.inbox5.com>
parents: 10846
diff changeset
85 char c2 = is.get ();
5e6ef42a2445 Overhaul str2double function fixing bug and docstring and adding 'NA' processing.
Rik <octave@nomad.inbox5.com>
parents: 10846
diff changeset
86 if (c1 == 'a' && c2 == 'N')
5e6ef42a2445 Overhaul str2double function fixing bug and docstring and adding 'NA' processing.
Rik <octave@nomad.inbox5.com>
parents: 10846
diff changeset
87 {
5e6ef42a2445 Overhaul str2double function fixing bug and docstring and adding 'NA' processing.
Rik <octave@nomad.inbox5.com>
parents: 10846
diff changeset
88 num = octave_NaN;
14091
c3ea07298af4 str2double: correctly handle things like ["1"; "2 3"; "4i"]
John W. Eaton <jwe@octave.org>
parents: 14077
diff changeset
89 is.peek (); // May set EOF bit.
11414
5e6ef42a2445 Overhaul str2double function fixing bug and docstring and adding 'NA' processing.
Rik <octave@nomad.inbox5.com>
parents: 10846
diff changeset
90 }
5e6ef42a2445 Overhaul str2double function fixing bug and docstring and adding 'NA' processing.
Rik <octave@nomad.inbox5.com>
parents: 10846
diff changeset
91 else
5e6ef42a2445 Overhaul str2double function fixing bug and docstring and adding 'NA' processing.
Rik <octave@nomad.inbox5.com>
parents: 10846
diff changeset
92 is.setstate (std::ios::failbit); // indicate that read has failed.
5e6ef42a2445 Overhaul str2double function fixing bug and docstring and adding 'NA' processing.
Rik <octave@nomad.inbox5.com>
parents: 10846
diff changeset
93 }
10357
7658cd4bdcf2 handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents: 10356
diff changeset
94 }
7658cd4bdcf2 handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents: 10356
diff changeset
95 else
7658cd4bdcf2 handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents: 10356
diff changeset
96 is >> num;
7658cd4bdcf2 handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents: 10356
diff changeset
97
17379
b91f29f021a3 str2double.cc: miscellaneous simplifications to code.
Rik <rik@octave.org>
parents: 17281
diff changeset
98 return num;
10357
7658cd4bdcf2 handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents: 10356
diff changeset
99 }
10356
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
100
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
101 static std::istringstream&
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
102 extract_num (std::istringstream& is, double& num, bool& imag, bool& have_sign)
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
103 {
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
104 have_sign = imag = false;
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
105
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
106 char c = is.peek ();
14077
b6eeeb67fa3f str2double: return NaN for things like "1 2 3 4" (bug #34713).
John W. Eaton <jwe@octave.org>
parents: 13141
diff changeset
107
b6eeeb67fa3f str2double: return NaN for things like "1 2 3 4" (bug #34713).
John W. Eaton <jwe@octave.org>
parents: 13141
diff changeset
108 // Skip leading spaces.
b6eeeb67fa3f str2double: return NaN for things like "1 2 3 4" (bug #34713).
John W. Eaton <jwe@octave.org>
parents: 13141
diff changeset
109 while (isspace (c))
b6eeeb67fa3f str2double: return NaN for things like "1 2 3 4" (bug #34713).
John W. Eaton <jwe@octave.org>
parents: 13141
diff changeset
110 {
b6eeeb67fa3f str2double: return NaN for things like "1 2 3 4" (bug #34713).
John W. Eaton <jwe@octave.org>
parents: 13141
diff changeset
111 is.get ();
b6eeeb67fa3f str2double: return NaN for things like "1 2 3 4" (bug #34713).
John W. Eaton <jwe@octave.org>
parents: 13141
diff changeset
112 c = is.peek ();
b6eeeb67fa3f str2double: return NaN for things like "1 2 3 4" (bug #34713).
John W. Eaton <jwe@octave.org>
parents: 13141
diff changeset
113 }
b6eeeb67fa3f str2double: return NaN for things like "1 2 3 4" (bug #34713).
John W. Eaton <jwe@octave.org>
parents: 13141
diff changeset
114
10356
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
115 bool negative = false;
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
116
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
117 // Accept leading sign.
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
118 if (c == '+' || c == '-')
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
119 {
17379
b91f29f021a3 str2double.cc: miscellaneous simplifications to code.
Rik <rik@octave.org>
parents: 17281
diff changeset
120 have_sign = true;
10356
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
121 negative = c == '-';
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
122 is.get ();
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
123 c = is.peek ();
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
124 }
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
125
14077
b6eeeb67fa3f str2double: return NaN for things like "1 2 3 4" (bug #34713).
John W. Eaton <jwe@octave.org>
parents: 13141
diff changeset
126 // Skip spaces after sign.
b6eeeb67fa3f str2double: return NaN for things like "1 2 3 4" (bug #34713).
John W. Eaton <jwe@octave.org>
parents: 13141
diff changeset
127 while (isspace (c))
b6eeeb67fa3f str2double: return NaN for things like "1 2 3 4" (bug #34713).
John W. Eaton <jwe@octave.org>
parents: 13141
diff changeset
128 {
b6eeeb67fa3f str2double: return NaN for things like "1 2 3 4" (bug #34713).
John W. Eaton <jwe@octave.org>
parents: 13141
diff changeset
129 is.get ();
b6eeeb67fa3f str2double: return NaN for things like "1 2 3 4" (bug #34713).
John W. Eaton <jwe@octave.org>
parents: 13141
diff changeset
130 c = is.peek ();
b6eeeb67fa3f str2double: return NaN for things like "1 2 3 4" (bug #34713).
John W. Eaton <jwe@octave.org>
parents: 13141
diff changeset
131 }
b6eeeb67fa3f str2double: return NaN for things like "1 2 3 4" (bug #34713).
John W. Eaton <jwe@octave.org>
parents: 13141
diff changeset
132
14702
a08f6e17336e str2double.cc: Case-insensitive detection of 'Inf' (Bug #36536).
Rik <octave@nomad.inbox5.com>
parents: 14501
diff changeset
133 // Imaginary number (i*num or just i), or maybe 'inf'.
a08f6e17336e str2double.cc: Case-insensitive detection of 'Inf' (Bug #36536).
Rik <octave@nomad.inbox5.com>
parents: 14501
diff changeset
134 if (c == 'i')
10356
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
135 {
14702
a08f6e17336e str2double.cc: Case-insensitive detection of 'Inf' (Bug #36536).
Rik <octave@nomad.inbox5.com>
parents: 14501
diff changeset
136 // possible infinity.
14091
c3ea07298af4 str2double: correctly handle things like ["1"; "2 3"; "4i"]
John W. Eaton <jwe@octave.org>
parents: 14077
diff changeset
137 is.get ();
c3ea07298af4 str2double: correctly handle things like ["1"; "2 3"; "4i"]
John W. Eaton <jwe@octave.org>
parents: 14077
diff changeset
138 c = is.peek ();
14077
b6eeeb67fa3f str2double: return NaN for things like "1 2 3 4" (bug #34713).
John W. Eaton <jwe@octave.org>
parents: 13141
diff changeset
139
14702
a08f6e17336e str2double.cc: Case-insensitive detection of 'Inf' (Bug #36536).
Rik <octave@nomad.inbox5.com>
parents: 14501
diff changeset
140 if (is.eof ())
a08f6e17336e str2double.cc: Case-insensitive detection of 'Inf' (Bug #36536).
Rik <octave@nomad.inbox5.com>
parents: 14501
diff changeset
141 {
a08f6e17336e str2double.cc: Case-insensitive detection of 'Inf' (Bug #36536).
Rik <octave@nomad.inbox5.com>
parents: 14501
diff changeset
142 // just 'i' and string is finished. Return immediately.
a08f6e17336e str2double.cc: Case-insensitive detection of 'Inf' (Bug #36536).
Rik <octave@nomad.inbox5.com>
parents: 14501
diff changeset
143 imag = true;
17379
b91f29f021a3 str2double.cc: miscellaneous simplifications to code.
Rik <rik@octave.org>
parents: 17281
diff changeset
144 num = negative ? -1.0 : 1.0;
14702
a08f6e17336e str2double.cc: Case-insensitive detection of 'Inf' (Bug #36536).
Rik <octave@nomad.inbox5.com>
parents: 14501
diff changeset
145 return is;
a08f6e17336e str2double.cc: Case-insensitive detection of 'Inf' (Bug #36536).
Rik <octave@nomad.inbox5.com>
parents: 14501
diff changeset
146 }
a08f6e17336e str2double.cc: Case-insensitive detection of 'Inf' (Bug #36536).
Rik <octave@nomad.inbox5.com>
parents: 14501
diff changeset
147 else
17379
b91f29f021a3 str2double.cc: miscellaneous simplifications to code.
Rik <rik@octave.org>
parents: 17281
diff changeset
148 {
14702
a08f6e17336e str2double.cc: Case-insensitive detection of 'Inf' (Bug #36536).
Rik <octave@nomad.inbox5.com>
parents: 14501
diff changeset
149 if (std::tolower (c) != 'n')
a08f6e17336e str2double.cc: Case-insensitive detection of 'Inf' (Bug #36536).
Rik <octave@nomad.inbox5.com>
parents: 14501
diff changeset
150 imag = true;
a08f6e17336e str2double.cc: Case-insensitive detection of 'Inf' (Bug #36536).
Rik <octave@nomad.inbox5.com>
parents: 14501
diff changeset
151 is.unget ();
a08f6e17336e str2double.cc: Case-insensitive detection of 'Inf' (Bug #36536).
Rik <octave@nomad.inbox5.com>
parents: 14501
diff changeset
152 }
a08f6e17336e str2double.cc: Case-insensitive detection of 'Inf' (Bug #36536).
Rik <octave@nomad.inbox5.com>
parents: 14501
diff changeset
153 }
a08f6e17336e str2double.cc: Case-insensitive detection of 'Inf' (Bug #36536).
Rik <octave@nomad.inbox5.com>
parents: 14501
diff changeset
154 else if (c == 'j')
a08f6e17336e str2double.cc: Case-insensitive detection of 'Inf' (Bug #36536).
Rik <octave@nomad.inbox5.com>
parents: 14501
diff changeset
155 imag = true;
17379
b91f29f021a3 str2double.cc: miscellaneous simplifications to code.
Rik <rik@octave.org>
parents: 17281
diff changeset
156
14702
a08f6e17336e str2double.cc: Case-insensitive detection of 'Inf' (Bug #36536).
Rik <octave@nomad.inbox5.com>
parents: 14501
diff changeset
157 // It's i*num or just i
a08f6e17336e str2double.cc: Case-insensitive detection of 'Inf' (Bug #36536).
Rik <octave@nomad.inbox5.com>
parents: 14501
diff changeset
158 if (imag)
a08f6e17336e str2double.cc: Case-insensitive detection of 'Inf' (Bug #36536).
Rik <octave@nomad.inbox5.com>
parents: 14501
diff changeset
159 {
a08f6e17336e str2double.cc: Case-insensitive detection of 'Inf' (Bug #36536).
Rik <octave@nomad.inbox5.com>
parents: 14501
diff changeset
160 is.get ();
a08f6e17336e str2double.cc: Case-insensitive detection of 'Inf' (Bug #36536).
Rik <octave@nomad.inbox5.com>
parents: 14501
diff changeset
161 c = is.peek ();
14077
b6eeeb67fa3f str2double: return NaN for things like "1 2 3 4" (bug #34713).
John W. Eaton <jwe@octave.org>
parents: 13141
diff changeset
162 // Skip spaces after imaginary unit.
14091
c3ea07298af4 str2double: correctly handle things like ["1"; "2 3"; "4i"]
John W. Eaton <jwe@octave.org>
parents: 14077
diff changeset
163 while (isspace (c))
14077
b6eeeb67fa3f str2double: return NaN for things like "1 2 3 4" (bug #34713).
John W. Eaton <jwe@octave.org>
parents: 13141
diff changeset
164 {
b6eeeb67fa3f str2double: return NaN for things like "1 2 3 4" (bug #34713).
John W. Eaton <jwe@octave.org>
parents: 13141
diff changeset
165 is.get ();
14091
c3ea07298af4 str2double: correctly handle things like ["1"; "2 3"; "4i"]
John W. Eaton <jwe@octave.org>
parents: 14077
diff changeset
166 c = is.peek ();
14077
b6eeeb67fa3f str2double: return NaN for things like "1 2 3 4" (bug #34713).
John W. Eaton <jwe@octave.org>
parents: 13141
diff changeset
167 }
b6eeeb67fa3f str2double: return NaN for things like "1 2 3 4" (bug #34713).
John W. Eaton <jwe@octave.org>
parents: 13141
diff changeset
168
14091
c3ea07298af4 str2double: correctly handle things like ["1"; "2 3"; "4i"]
John W. Eaton <jwe@octave.org>
parents: 14077
diff changeset
169 if (c == '*')
10356
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
170 {
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
171 // Multiplier follows, we extract it as a number.
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
172 is.get ();
17379
b91f29f021a3 str2double.cc: miscellaneous simplifications to code.
Rik <rik@octave.org>
parents: 17281
diff changeset
173 num = single_num (is);
14091
c3ea07298af4 str2double: correctly handle things like ["1"; "2 3"; "4i"]
John W. Eaton <jwe@octave.org>
parents: 14077
diff changeset
174 if (is.good ())
c3ea07298af4 str2double: correctly handle things like ["1"; "2 3"; "4i"]
John W. Eaton <jwe@octave.org>
parents: 14077
diff changeset
175 c = is.peek ();
10356
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
176 }
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
177 else
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
178 num = 1.0;
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
179 }
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
180 else
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
181 {
11414
5e6ef42a2445 Overhaul str2double function fixing bug and docstring and adding 'NA' processing.
Rik <octave@nomad.inbox5.com>
parents: 10846
diff changeset
182 // It's num, num*i, or numi.
17379
b91f29f021a3 str2double.cc: miscellaneous simplifications to code.
Rik <rik@octave.org>
parents: 17281
diff changeset
183 num = single_num (is);
10356
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
184 if (is.good ())
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
185 {
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
186 c = is.peek ();
14077
b6eeeb67fa3f str2double: return NaN for things like "1 2 3 4" (bug #34713).
John W. Eaton <jwe@octave.org>
parents: 13141
diff changeset
187
b6eeeb67fa3f str2double: return NaN for things like "1 2 3 4" (bug #34713).
John W. Eaton <jwe@octave.org>
parents: 13141
diff changeset
188 // Skip spaces after number.
b6eeeb67fa3f str2double: return NaN for things like "1 2 3 4" (bug #34713).
John W. Eaton <jwe@octave.org>
parents: 13141
diff changeset
189 while (isspace (c))
b6eeeb67fa3f str2double: return NaN for things like "1 2 3 4" (bug #34713).
John W. Eaton <jwe@octave.org>
parents: 13141
diff changeset
190 {
b6eeeb67fa3f str2double: return NaN for things like "1 2 3 4" (bug #34713).
John W. Eaton <jwe@octave.org>
parents: 13141
diff changeset
191 is.get ();
b6eeeb67fa3f str2double: return NaN for things like "1 2 3 4" (bug #34713).
John W. Eaton <jwe@octave.org>
parents: 13141
diff changeset
192 c = is.peek ();
b6eeeb67fa3f str2double: return NaN for things like "1 2 3 4" (bug #34713).
John W. Eaton <jwe@octave.org>
parents: 13141
diff changeset
193 }
b6eeeb67fa3f str2double: return NaN for things like "1 2 3 4" (bug #34713).
John W. Eaton <jwe@octave.org>
parents: 13141
diff changeset
194
10356
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
195 if (c == '*')
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
196 {
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
197 is.get ();
14077
b6eeeb67fa3f str2double: return NaN for things like "1 2 3 4" (bug #34713).
John W. Eaton <jwe@octave.org>
parents: 13141
diff changeset
198 c = is.peek ();
b6eeeb67fa3f str2double: return NaN for things like "1 2 3 4" (bug #34713).
John W. Eaton <jwe@octave.org>
parents: 13141
diff changeset
199
b6eeeb67fa3f str2double: return NaN for things like "1 2 3 4" (bug #34713).
John W. Eaton <jwe@octave.org>
parents: 13141
diff changeset
200 // Skip spaces after operator.
b6eeeb67fa3f str2double: return NaN for things like "1 2 3 4" (bug #34713).
John W. Eaton <jwe@octave.org>
parents: 13141
diff changeset
201 while (isspace (c))
b6eeeb67fa3f str2double: return NaN for things like "1 2 3 4" (bug #34713).
John W. Eaton <jwe@octave.org>
parents: 13141
diff changeset
202 {
b6eeeb67fa3f str2double: return NaN for things like "1 2 3 4" (bug #34713).
John W. Eaton <jwe@octave.org>
parents: 13141
diff changeset
203 is.get ();
b6eeeb67fa3f str2double: return NaN for things like "1 2 3 4" (bug #34713).
John W. Eaton <jwe@octave.org>
parents: 13141
diff changeset
204 c = is.peek ();
b6eeeb67fa3f str2double: return NaN for things like "1 2 3 4" (bug #34713).
John W. Eaton <jwe@octave.org>
parents: 13141
diff changeset
205 }
b6eeeb67fa3f str2double: return NaN for things like "1 2 3 4" (bug #34713).
John W. Eaton <jwe@octave.org>
parents: 13141
diff changeset
206
10356
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
207 if (is_imag_unit (c))
11414
5e6ef42a2445 Overhaul str2double function fixing bug and docstring and adding 'NA' processing.
Rik <octave@nomad.inbox5.com>
parents: 10846
diff changeset
208 {
5e6ef42a2445 Overhaul str2double function fixing bug and docstring and adding 'NA' processing.
Rik <octave@nomad.inbox5.com>
parents: 10846
diff changeset
209 imag = true;
14077
b6eeeb67fa3f str2double: return NaN for things like "1 2 3 4" (bug #34713).
John W. Eaton <jwe@octave.org>
parents: 13141
diff changeset
210 is.get ();
14091
c3ea07298af4 str2double: correctly handle things like ["1"; "2 3"; "4i"]
John W. Eaton <jwe@octave.org>
parents: 14077
diff changeset
211 c = is.peek ();
11414
5e6ef42a2445 Overhaul str2double function fixing bug and docstring and adding 'NA' processing.
Rik <octave@nomad.inbox5.com>
parents: 10846
diff changeset
212 }
10356
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
213 else
17787
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
214 is.setstate (std::ios::failbit); // indicate read has failed.
10356
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
215 }
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
216 else if (is_imag_unit (c))
11414
5e6ef42a2445 Overhaul str2double function fixing bug and docstring and adding 'NA' processing.
Rik <octave@nomad.inbox5.com>
parents: 10846
diff changeset
217 {
5e6ef42a2445 Overhaul str2double function fixing bug and docstring and adding 'NA' processing.
Rik <octave@nomad.inbox5.com>
parents: 10846
diff changeset
218 imag = true;
5e6ef42a2445 Overhaul str2double function fixing bug and docstring and adding 'NA' processing.
Rik <octave@nomad.inbox5.com>
parents: 10846
diff changeset
219 is.get ();
14091
c3ea07298af4 str2double: correctly handle things like ["1"; "2 3"; "4i"]
John W. Eaton <jwe@octave.org>
parents: 14077
diff changeset
220 c = is.peek ();
11414
5e6ef42a2445 Overhaul str2double function fixing bug and docstring and adding 'NA' processing.
Rik <octave@nomad.inbox5.com>
parents: 10846
diff changeset
221 }
10356
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
222 }
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
223 }
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
224
14091
c3ea07298af4 str2double: correctly handle things like ["1"; "2 3"; "4i"]
John W. Eaton <jwe@octave.org>
parents: 14077
diff changeset
225 if (is.good ())
c3ea07298af4 str2double: correctly handle things like ["1"; "2 3"; "4i"]
John W. Eaton <jwe@octave.org>
parents: 14077
diff changeset
226 {
c3ea07298af4 str2double: correctly handle things like ["1"; "2 3"; "4i"]
John W. Eaton <jwe@octave.org>
parents: 14077
diff changeset
227 // Skip trailing spaces.
c3ea07298af4 str2double: correctly handle things like ["1"; "2 3"; "4i"]
John W. Eaton <jwe@octave.org>
parents: 14077
diff changeset
228 while (isspace (c))
c3ea07298af4 str2double: correctly handle things like ["1"; "2 3"; "4i"]
John W. Eaton <jwe@octave.org>
parents: 14077
diff changeset
229 {
c3ea07298af4 str2double: correctly handle things like ["1"; "2 3"; "4i"]
John W. Eaton <jwe@octave.org>
parents: 14077
diff changeset
230 is.get ();
c3ea07298af4 str2double: correctly handle things like ["1"; "2 3"; "4i"]
John W. Eaton <jwe@octave.org>
parents: 14077
diff changeset
231 c = is.peek ();
c3ea07298af4 str2double: correctly handle things like ["1"; "2 3"; "4i"]
John W. Eaton <jwe@octave.org>
parents: 14077
diff changeset
232 }
c3ea07298af4 str2double: correctly handle things like ["1"; "2 3"; "4i"]
John W. Eaton <jwe@octave.org>
parents: 14077
diff changeset
233 }
c3ea07298af4 str2double: correctly handle things like ["1"; "2 3"; "4i"]
John W. Eaton <jwe@octave.org>
parents: 14077
diff changeset
234
10356
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
235 if (negative)
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
236 num = -num;
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
237
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
238 return is;
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
239 }
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
240
10379
f578e6468d0c use C++-0x code in str2double
Jaroslav Hajek <highegg@gmail.com>
parents: 10378
diff changeset
241 static inline void
f578e6468d0c use C++-0x code in str2double
Jaroslav Hajek <highegg@gmail.com>
parents: 10378
diff changeset
242 set_component (Complex& c, double num, bool imag)
f578e6468d0c use C++-0x code in str2double
Jaroslav Hajek <highegg@gmail.com>
parents: 10378
diff changeset
243 {
10380
60acc47c203f configure checks for complex element setter/reference accessor methods
John W. Eaton <jwe@octave.org>
parents: 10379
diff changeset
244 #if defined (HAVE_CXX_COMPLEX_SETTERS)
10379
f578e6468d0c use C++-0x code in str2double
Jaroslav Hajek <highegg@gmail.com>
parents: 10378
diff changeset
245 if (imag)
10380
60acc47c203f configure checks for complex element setter/reference accessor methods
John W. Eaton <jwe@octave.org>
parents: 10379
diff changeset
246 c.imag (num);
10379
f578e6468d0c use C++-0x code in str2double
Jaroslav Hajek <highegg@gmail.com>
parents: 10378
diff changeset
247 else
10380
60acc47c203f configure checks for complex element setter/reference accessor methods
John W. Eaton <jwe@octave.org>
parents: 10379
diff changeset
248 c.real (num);
60acc47c203f configure checks for complex element setter/reference accessor methods
John W. Eaton <jwe@octave.org>
parents: 10379
diff changeset
249 #elif defined (HAVE_CXX_COMPLEX_REFERENCE_ACCESSORS)
60acc47c203f configure checks for complex element setter/reference accessor methods
John W. Eaton <jwe@octave.org>
parents: 10379
diff changeset
250 if (imag)
60acc47c203f configure checks for complex element setter/reference accessor methods
John W. Eaton <jwe@octave.org>
parents: 10379
diff changeset
251 c.imag () = num;
60acc47c203f configure checks for complex element setter/reference accessor methods
John W. Eaton <jwe@octave.org>
parents: 10379
diff changeset
252 else
60acc47c203f configure checks for complex element setter/reference accessor methods
John W. Eaton <jwe@octave.org>
parents: 10379
diff changeset
253 c.real () = num;
10379
f578e6468d0c use C++-0x code in str2double
Jaroslav Hajek <highegg@gmail.com>
parents: 10378
diff changeset
254 #else
f578e6468d0c use C++-0x code in str2double
Jaroslav Hajek <highegg@gmail.com>
parents: 10378
diff changeset
255 if (imag)
f578e6468d0c use C++-0x code in str2double
Jaroslav Hajek <highegg@gmail.com>
parents: 10378
diff changeset
256 c = Complex (c.real (), num);
f578e6468d0c use C++-0x code in str2double
Jaroslav Hajek <highegg@gmail.com>
parents: 10378
diff changeset
257 else
f578e6468d0c use C++-0x code in str2double
Jaroslav Hajek <highegg@gmail.com>
parents: 10378
diff changeset
258 c = Complex (num, c.imag ());
f578e6468d0c use C++-0x code in str2double
Jaroslav Hajek <highegg@gmail.com>
parents: 10378
diff changeset
259 #endif
f578e6468d0c use C++-0x code in str2double
Jaroslav Hajek <highegg@gmail.com>
parents: 10378
diff changeset
260 }
f578e6468d0c use C++-0x code in str2double
Jaroslav Hajek <highegg@gmail.com>
parents: 10378
diff changeset
261
10356
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
262 static Complex
10381
1aa8b9b8f921 str2double.cc: style fix
John W. Eaton <jwe@octave.org>
parents: 10380
diff changeset
263 str2double1 (const std::string& str_arg)
10356
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
264 {
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
265 Complex val (0.0, 0.0);
10381
1aa8b9b8f921 str2double.cc: style fix
John W. Eaton <jwe@octave.org>
parents: 10380
diff changeset
266
1aa8b9b8f921 str2double.cc: style fix
John W. Eaton <jwe@octave.org>
parents: 10380
diff changeset
267 std::string str = str_arg;
1aa8b9b8f921 str2double.cc: style fix
John W. Eaton <jwe@octave.org>
parents: 10380
diff changeset
268
17379
b91f29f021a3 str2double.cc: miscellaneous simplifications to code.
Rik <rik@octave.org>
parents: 17281
diff changeset
269 // FIXME: removing all commas doesn't allow actual parsing.
b91f29f021a3 str2double.cc: miscellaneous simplifications to code.
Rik <rik@octave.org>
parents: 17281
diff changeset
270 // Example: "1,23.45" is wrong, but passes Octave.
b91f29f021a3 str2double.cc: miscellaneous simplifications to code.
Rik <rik@octave.org>
parents: 17281
diff changeset
271 str.erase (std::remove (str.begin (), str.end(), ','), str.end ());
14077
b6eeeb67fa3f str2double: return NaN for things like "1 2 3 4" (bug #34713).
John W. Eaton <jwe@octave.org>
parents: 13141
diff changeset
272 std::istringstream is (str);
10356
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
273
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
274 double num;
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
275 bool i1, i2, s1, s2;
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
276
10357
7658cd4bdcf2 handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents: 10356
diff changeset
277 if (is.eof ())
7658cd4bdcf2 handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents: 10356
diff changeset
278 val = octave_NaN;
7658cd4bdcf2 handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents: 10356
diff changeset
279 else if (! extract_num (is, num, i1, s1))
10356
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
280 val = octave_NaN;
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
281 else
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
282 {
10380
60acc47c203f configure checks for complex element setter/reference accessor methods
John W. Eaton <jwe@octave.org>
parents: 10379
diff changeset
283 set_component (val, num, i1);
10356
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
284
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
285 if (! is.eof ())
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
286 {
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
287 if (! extract_num (is, num, i2, s2) || i1 == i2 || ! s2)
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
288 val = octave_NaN;
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
289 else
10380
60acc47c203f configure checks for complex element setter/reference accessor methods
John W. Eaton <jwe@octave.org>
parents: 10379
diff changeset
290 set_component (val, num, i2);
10356
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
291 }
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
292 }
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
293
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
294 return val;
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
295 }
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
296
15039
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14854
diff changeset
297 DEFUN (str2double, args, ,
17787
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
298 "-*- texinfo -*-\n\
10356
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
299 @deftypefn {Built-in Function} {} str2double (@var{s})\n\
11414
5e6ef42a2445 Overhaul str2double function fixing bug and docstring and adding 'NA' processing.
Rik <octave@nomad.inbox5.com>
parents: 10846
diff changeset
300 Convert a string to a real or complex number.\n\
5e6ef42a2445 Overhaul str2double function fixing bug and docstring and adding 'NA' processing.
Rik <octave@nomad.inbox5.com>
parents: 10846
diff changeset
301 \n\
20207
4f45eaf83908 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19731
diff changeset
302 The string must be in one of the following formats where a and b are real\n\
4f45eaf83908 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19731
diff changeset
303 numbers and the complex unit is @qcode{'i'} or @qcode{'j'}:\n\
10356
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
304 \n\
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
305 @itemize\n\
10840
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10381
diff changeset
306 @item a + bi\n\
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10381
diff changeset
307 \n\
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10381
diff changeset
308 @item a + b*i\n\
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10381
diff changeset
309 \n\
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10381
diff changeset
310 @item a + i*b\n\
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10381
diff changeset
311 \n\
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10381
diff changeset
312 @item bi + a\n\
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10381
diff changeset
313 \n\
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10381
diff changeset
314 @item b*i + a\n\
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10381
diff changeset
315 \n\
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10381
diff changeset
316 @item i*b + a\n\
10356
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
317 @end itemize\n\
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
318 \n\
11591
1d13679b587e Use @nospell macro on certain words in docstrings.
Rik <octave@nomad.inbox5.com>
parents: 11586
diff changeset
319 If present, a and/or b are of the form @nospell{[+-]d[,.]d[[eE][+-]d]} where\n\
17281
bc924baa2c4e doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents: 15195
diff changeset
320 the brackets indicate optional arguments and @qcode{'d'} indicates zero or\n\
bc924baa2c4e doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents: 15195
diff changeset
321 more digits. The special input values @code{Inf}, @code{NaN}, and @code{NA}\n\
bc924baa2c4e doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents: 15195
diff changeset
322 are also accepted.\n\
11414
5e6ef42a2445 Overhaul str2double function fixing bug and docstring and adding 'NA' processing.
Rik <octave@nomad.inbox5.com>
parents: 10846
diff changeset
323 \n\
20207
4f45eaf83908 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19731
diff changeset
324 @var{s} may be a character string, character matrix, or cell array. For\n\
4f45eaf83908 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19731
diff changeset
325 character arrays the conversion is repeated for every row, and a double or\n\
4f45eaf83908 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19731
diff changeset
326 complex array is returned. Empty rows in @var{s} are deleted and not\n\
4f45eaf83908 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19731
diff changeset
327 returned in the numeric array. For cell arrays each character string\n\
4f45eaf83908 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19731
diff changeset
328 element is processed and a double or complex array of the same dimensions as\n\
4f45eaf83908 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19731
diff changeset
329 @var{s} is returned.\n\
11414
5e6ef42a2445 Overhaul str2double function fixing bug and docstring and adding 'NA' processing.
Rik <octave@nomad.inbox5.com>
parents: 10846
diff changeset
330 \n\
17409
3baf8e7ec3dd str2double.cc: make output more consistent and ML-compatible, update help text, add tests (bug #39838)
Ben Kurtz <hidden email>
parents: 17379
diff changeset
331 For unconvertible scalar or character string input @code{str2double} returns\n\
17410
f47cfca56eb9 doc: Rephrase a few sentences in str2double docstring.
Rik <rik@octave.org>
parents: 17409
diff changeset
332 a NaN@. Similarly, for character array input @code{str2double} returns a\n\
f47cfca56eb9 doc: Rephrase a few sentences in str2double docstring.
Rik <rik@octave.org>
parents: 17409
diff changeset
333 NaN for any row of @var{s} that could not be converted. For a cell array,\n\
17409
3baf8e7ec3dd str2double.cc: make output more consistent and ML-compatible, update help text, add tests (bug #39838)
Ben Kurtz <hidden email>
parents: 17379
diff changeset
334 @code{str2double} returns a NaN for any element of @var{s} for which\n\
17410
f47cfca56eb9 doc: Rephrase a few sentences in str2double docstring.
Rik <rik@octave.org>
parents: 17409
diff changeset
335 conversion fails. Note that numeric elements in a mixed string/numeric\n\
f47cfca56eb9 doc: Rephrase a few sentences in str2double docstring.
Rik <rik@octave.org>
parents: 17409
diff changeset
336 cell array are not strings and the conversion will fail for these elements\n\
f47cfca56eb9 doc: Rephrase a few sentences in str2double docstring.
Rik <rik@octave.org>
parents: 17409
diff changeset
337 and return NaN.\n\
12703
d326be468b13 doc: str2double() inputs which cannot be parsed return NaN.
Rik <octave@nomad.inbox5.com>
parents: 11591
diff changeset
338 \n\
d326be468b13 doc: str2double() inputs which cannot be parsed return NaN.
Rik <octave@nomad.inbox5.com>
parents: 11591
diff changeset
339 @code{str2double} can replace @code{str2num}, and it avoids the security\n\
d326be468b13 doc: str2double() inputs which cannot be parsed return NaN.
Rik <octave@nomad.inbox5.com>
parents: 11591
diff changeset
340 risk of using @code{eval} on unknown data.\n\
11414
5e6ef42a2445 Overhaul str2double function fixing bug and docstring and adding 'NA' processing.
Rik <octave@nomad.inbox5.com>
parents: 10846
diff changeset
341 @seealso{str2num}\n\
10356
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
342 @end deftypefn")
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
343 {
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
344 octave_value retval;
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
345
12735
76e2c55906de str2double.cc: Return NaN for invalid inputs rather than printing error.
Rik <octave@nomad.inbox5.com>
parents: 12703
diff changeset
346 if (args.length () != 1)
76e2c55906de str2double.cc: Return NaN for invalid inputs rather than printing error.
Rik <octave@nomad.inbox5.com>
parents: 12703
diff changeset
347 print_usage ();
76e2c55906de str2double.cc: Return NaN for invalid inputs rather than printing error.
Rik <octave@nomad.inbox5.com>
parents: 12703
diff changeset
348 else if (args(0).is_string ())
10356
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
349 {
17409
3baf8e7ec3dd str2double.cc: make output more consistent and ML-compatible, update help text, add tests (bug #39838)
Ben Kurtz <hidden email>
parents: 17379
diff changeset
350 if (args(0).rows () == 0 || args(0).columns () == 0)
3baf8e7ec3dd str2double.cc: make output more consistent and ML-compatible, update help text, add tests (bug #39838)
Ben Kurtz <hidden email>
parents: 17379
diff changeset
351 {
3baf8e7ec3dd str2double.cc: make output more consistent and ML-compatible, update help text, add tests (bug #39838)
Ben Kurtz <hidden email>
parents: 17379
diff changeset
352 retval = Matrix (1, 1, octave_NaN);
3baf8e7ec3dd str2double.cc: make output more consistent and ML-compatible, update help text, add tests (bug #39838)
Ben Kurtz <hidden email>
parents: 17379
diff changeset
353 }
3baf8e7ec3dd str2double.cc: make output more consistent and ML-compatible, update help text, add tests (bug #39838)
Ben Kurtz <hidden email>
parents: 17379
diff changeset
354 else if (args(0).rows () == 1 && args(0).ndims () == 2)
10356
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
355 {
12735
76e2c55906de str2double.cc: Return NaN for invalid inputs rather than printing error.
Rik <octave@nomad.inbox5.com>
parents: 12703
diff changeset
356 retval = str2double1 (args(0).string_value ());
10356
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
357 }
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
358 else
12735
76e2c55906de str2double.cc: Return NaN for invalid inputs rather than printing error.
Rik <octave@nomad.inbox5.com>
parents: 12703
diff changeset
359 {
76e2c55906de str2double.cc: Return NaN for invalid inputs rather than printing error.
Rik <octave@nomad.inbox5.com>
parents: 12703
diff changeset
360 const string_vector sv = args(0).all_strings ();
20587
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20207
diff changeset
361
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20207
diff changeset
362 retval = sv.map<Complex> (str2double1);
12735
76e2c55906de str2double.cc: Return NaN for invalid inputs rather than printing error.
Rik <octave@nomad.inbox5.com>
parents: 12703
diff changeset
363 }
76e2c55906de str2double.cc: Return NaN for invalid inputs rather than printing error.
Rik <octave@nomad.inbox5.com>
parents: 12703
diff changeset
364 }
76e2c55906de str2double.cc: Return NaN for invalid inputs rather than printing error.
Rik <octave@nomad.inbox5.com>
parents: 12703
diff changeset
365 else if (args(0).is_cell ())
76e2c55906de str2double.cc: Return NaN for invalid inputs rather than printing error.
Rik <octave@nomad.inbox5.com>
parents: 12703
diff changeset
366 {
13141
e81ddf9cacd5 maint: untabify and remove trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 12735
diff changeset
367 const Cell cell = args(0).cell_value ();
12735
76e2c55906de str2double.cc: Return NaN for invalid inputs rather than printing error.
Rik <octave@nomad.inbox5.com>
parents: 12703
diff changeset
368
20587
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20207
diff changeset
369 ComplexNDArray output (cell.dims (), octave_NaN);
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20207
diff changeset
370 for (octave_idx_type i = 0; i < cell.numel (); i++)
12735
76e2c55906de str2double.cc: Return NaN for invalid inputs rather than printing error.
Rik <octave@nomad.inbox5.com>
parents: 12703
diff changeset
371 {
20587
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20207
diff changeset
372 if (cell(i).is_string ())
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20207
diff changeset
373 output(i) = str2double1 (cell(i).string_value ());
12735
76e2c55906de str2double.cc: Return NaN for invalid inputs rather than printing error.
Rik <octave@nomad.inbox5.com>
parents: 12703
diff changeset
374 }
20587
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20207
diff changeset
375 retval = output;
10356
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
376 }
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
377 else
17409
3baf8e7ec3dd str2double.cc: make output more consistent and ML-compatible, update help text, add tests (bug #39838)
Ben Kurtz <hidden email>
parents: 17379
diff changeset
378 retval = Matrix (1, 1, octave_NaN);
13141
e81ddf9cacd5 maint: untabify and remove trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 12735
diff changeset
379
10356
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
380
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
381 return retval;
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
382 }
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
383
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
384 /*
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
385 %!assert (str2double ("1"), 1)
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
386 %!assert (str2double ("-.1e-5"), -1e-6)
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
387 %!assert (str2double (char ("1", "2 3", "4i")), [1; NaN; 4i])
10356
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
388 %!assert (str2double ("1,222.5"), 1222.5)
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
389 %!assert (str2double ("i"), i)
11414
5e6ef42a2445 Overhaul str2double function fixing bug and docstring and adding 'NA' processing.
Rik <octave@nomad.inbox5.com>
parents: 10846
diff changeset
390 %!assert (str2double ("2j"), 2i)
10356
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
391 %!assert (str2double ("2 + j"), 2+j)
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
392 %!assert (str2double ("i*2 + 3"), 3+2i)
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
393 %!assert (str2double (".5*i + 3.5"), 3.5+0.5i)
11414
5e6ef42a2445 Overhaul str2double function fixing bug and docstring and adding 'NA' processing.
Rik <octave@nomad.inbox5.com>
parents: 10846
diff changeset
394 %!assert (str2double ("1e-3 + i*.25"), 1e-3 + 0.25i)
5e6ef42a2445 Overhaul str2double function fixing bug and docstring and adding 'NA' processing.
Rik <octave@nomad.inbox5.com>
parents: 10846
diff changeset
395 %!assert (str2double (["2 + j";"1.25e-3";"-05"]), [2+i; 1.25e-3; -5])
5e6ef42a2445 Overhaul str2double function fixing bug and docstring and adding 'NA' processing.
Rik <octave@nomad.inbox5.com>
parents: 10846
diff changeset
396 %!assert (str2double ({"2 + j","1.25e-3","-05"}), [2+i, 1.25e-3, -5])
12735
76e2c55906de str2double.cc: Return NaN for invalid inputs rather than printing error.
Rik <octave@nomad.inbox5.com>
parents: 12703
diff changeset
397 %!assert (str2double (1), NaN)
14077
b6eeeb67fa3f str2double: return NaN for things like "1 2 3 4" (bug #34713).
John W. Eaton <jwe@octave.org>
parents: 13141
diff changeset
398 %!assert (str2double ("1 2 3 4"), NaN)
11414
5e6ef42a2445 Overhaul str2double function fixing bug and docstring and adding 'NA' processing.
Rik <octave@nomad.inbox5.com>
parents: 10846
diff changeset
399 %!assert (str2double ("Hello World"), NaN)
10357
7658cd4bdcf2 handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents: 10356
diff changeset
400 %!assert (str2double ("NaN"), NaN)
11414
5e6ef42a2445 Overhaul str2double function fixing bug and docstring and adding 'NA' processing.
Rik <octave@nomad.inbox5.com>
parents: 10846
diff changeset
401 %!assert (str2double ("NA"), NA)
10357
7658cd4bdcf2 handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents: 10356
diff changeset
402 %!assert (str2double ("Inf"), Inf)
14702
a08f6e17336e str2double.cc: Case-insensitive detection of 'Inf' (Bug #36536).
Rik <octave@nomad.inbox5.com>
parents: 14501
diff changeset
403 %!assert (str2double ("iNF"), Inf)
10357
7658cd4bdcf2 handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents: 10356
diff changeset
404 %!assert (str2double ("-Inf"), -Inf)
11414
5e6ef42a2445 Overhaul str2double function fixing bug and docstring and adding 'NA' processing.
Rik <octave@nomad.inbox5.com>
parents: 10846
diff changeset
405 %!assert (str2double ("Inf*i"), complex (0, Inf))
14702
a08f6e17336e str2double.cc: Case-insensitive detection of 'Inf' (Bug #36536).
Rik <octave@nomad.inbox5.com>
parents: 14501
diff changeset
406 %!assert (str2double ("iNF*i"), complex (0, Inf))
10357
7658cd4bdcf2 handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents: 10356
diff changeset
407 %!assert (str2double ("NaN + Inf*i"), complex (NaN, Inf))
7658cd4bdcf2 handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents: 10356
diff changeset
408 %!assert (str2double ("Inf - Inf*i"), complex (Inf, -Inf))
7658cd4bdcf2 handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents: 10356
diff changeset
409 %!assert (str2double ("-i*NaN - Inf"), complex (-Inf, -NaN))
12735
76e2c55906de str2double.cc: Return NaN for invalid inputs rather than printing error.
Rik <octave@nomad.inbox5.com>
parents: 12703
diff changeset
410 %!assert (str2double ({"abc", "4i"}), [NaN + 0i, 4i])
76e2c55906de str2double.cc: Return NaN for invalid inputs rather than printing error.
Rik <octave@nomad.inbox5.com>
parents: 12703
diff changeset
411 %!assert (str2double ({2, "4i"}), [NaN + 0i, 4i])
17409
3baf8e7ec3dd str2double.cc: make output more consistent and ML-compatible, update help text, add tests (bug #39838)
Ben Kurtz <hidden email>
parents: 17379
diff changeset
412 %!assert (str2double (zeros (3,1,2)), NaN)
3baf8e7ec3dd str2double.cc: make output more consistent and ML-compatible, update help text, add tests (bug #39838)
Ben Kurtz <hidden email>
parents: 17379
diff changeset
413 %!assert (str2double (''), NaN)
3baf8e7ec3dd str2double.cc: make output more consistent and ML-compatible, update help text, add tests (bug #39838)
Ben Kurtz <hidden email>
parents: 17379
diff changeset
414 %!assert (str2double ([]), NaN)
3baf8e7ec3dd str2double.cc: make output more consistent and ML-compatible, update help text, add tests (bug #39838)
Ben Kurtz <hidden email>
parents: 17379
diff changeset
415 %!assert (str2double (char(zeros(3,0))), NaN)
3baf8e7ec3dd str2double.cc: make output more consistent and ML-compatible, update help text, add tests (bug #39838)
Ben Kurtz <hidden email>
parents: 17379
diff changeset
416 */