Mercurial > octave
annotate libinterp/corefcn/str2double.cc @ 23450:855122b993da
maint: Wrap tertiary operator in parentheses "(COND ? x : y)".
* Canvas.cc, color-picker.cc, dialog.cc, marker.cc, main-window.cc, Cell.cc,
__magick_read__.cc, __pchip_deriv__.cc, __qp__.cc, bsxfun.cc, call-stack.cc,
cellfun.cc, data.cc, defaults.cc, det.cc, eig.cc, error.cc, file-io.cc,
filter.cc, find.cc, gammainc.cc, gcd.cc, gl-render.cc, graphics.cc,
graphics.in.h, help.cc, hex2num.cc, inv.cc, load-save.cc, lookup.cc,
ls-mat4.cc, ls-mat5.cc, ls-oct-binary.cc, ls-oct-text.cc, lu.cc, max.cc,
mex.cc, oct-hist.cc, oct-map.cc, oct-procbuf.cc, oct-stream.cc, pr-output.cc,
rand.cc, regexp.cc, schur.cc, str2double.cc, strfns.cc, symtab.cc, sysdep.cc,
tril.cc, variables.cc, xdiv.cc, audiodevinfo.cc, audioread.cc, colamd.cc,
gzip.cc, ov-base-diag.cc, ov-base-mat.h, ov-base-scalar.h, ov-class.cc,
ov-classdef.cc, ov-fcn-handle.cc, ov-range.cc, ov-range.h, ov-struct.cc,
ov-usr-fcn.cc, ov.cc, octave.cc, op-class.cc, pt-eval.cc, pt-funcall.cc,
pt-idx.cc, pt-jit.cc, pt-stmt.cc, version.cc, Array-util.cc, Array.cc,
CDiagMatrix.cc, CMatrix.cc, CNDArray.cc, CSparse.cc, Range.cc, Sparse.cc,
chNDArray.cc, dDiagMatrix.cc, dMatrix.cc, dNDArray.cc, dSparse.cc,
dim-vector.cc, fCDiagMatrix.cc, fCMatrix.cc, fCNDArray.cc, fDiagMatrix.cc,
fMatrix.cc, fNDArray.cc, idx-vector.cc, idx-vector.h, Faddeeva.cc, DASPK.cc,
DASRT.cc, DASSL.cc, EIG.cc, fEIG.cc, gsvd.cc, lo-specfun.cc, oct-rand.cc,
qr.cc, qrp.cc, randgamma.cc, schur.cc, svd.cc, Sparse-diag-op-defs.h,
mx-inlines.cc, oct-env.cc, cmd-edit.cc, kpse.cc, oct-inttypes.h, oct-sort.cc:
Wrap tertiary operator in parentheses "(COND ? x : y)".
author | Rik <rik@octave.org> |
---|---|
date | Thu, 27 Apr 2017 17:33:10 -0700 |
parents | 092078913d54 |
children | 00e518162fda |
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 |
23219
3ac9f9ecfae5
maint: Update copyright dates.
John W. Eaton <jwe@octave.org>
parents:
23083
diff
changeset
|
3 Copyright (C) 2010-2017 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 |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22407
diff
changeset
|
9 under the terms of the GNU General Public License as published by |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22407
diff
changeset
|
10 the Free Software Foundation; either version 3 of the License, or |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22407
diff
changeset
|
11 (at your option) any later version. |
10356
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
12 |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22407
diff
changeset
|
13 Octave is distributed in the hope that it will be useful, but |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22407
diff
changeset
|
14 WITHOUT ANY WARRANTY; without even the implied warranty of |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22407
diff
changeset
|
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22407
diff
changeset
|
16 GNU General Public License for more details. |
10356
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 |
21724
aba2e6293dd8
use "#if ..." consistently instead of "#ifdef" and "#ifndef"
John W. Eaton <jwe@octave.org>
parents:
21723
diff
changeset
|
24 #if defined (HAVE_CONFIG_H) |
21301
40de9f8f23a6
Use '#include "config.h"' rather than <config.h>.
Rik <rik@octave.org>
parents:
21200
diff
changeset
|
25 # include "config.h" |
10356
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" |
21100
e39e05d90788
Switch gripe_XXX to either err_XXX or warn_XXX naming scheme.
Rik <rik@octave.org>
parents:
20990
diff
changeset
|
38 #include "errwarn.h" |
10356
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 { |
18676
5bd1ca29c5f0
Clean up questionable code bits identified by clang sanitize.
Rik <rik@octave.org>
parents:
18100
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 (); |
18100
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 { |
21723
bae585228161
use namespace for numeric_limits
John W. Eaton <jwe@octave.org>
parents:
21721
diff
changeset
|
67 num = octave::numeric_limits<double>::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 { |
21723
bae585228161
use namespace for numeric_limits
John W. Eaton <jwe@octave.org>
parents:
21721
diff
changeset
|
88 num = octave::numeric_limits<double>::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; |
23450
855122b993da
maint: Wrap tertiary operator in parentheses "(COND ? x : y)".
Rik <rik@octave.org>
parents:
23220
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 | 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 | 266 |
267 std::string str = str_arg; | |
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 ()) |
21723
bae585228161
use namespace for numeric_limits
John W. Eaton <jwe@octave.org>
parents:
21721
diff
changeset
|
278 val = octave::numeric_limits<double>::NaN (); |
10357
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)) |
21723
bae585228161
use namespace for numeric_limits
John W. Eaton <jwe@octave.org>
parents:
21721
diff
changeset
|
280 val = octave::numeric_limits<double>::NaN (); |
10356
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) |
21723
bae585228161
use namespace for numeric_limits
John W. Eaton <jwe@octave.org>
parents:
21721
diff
changeset
|
288 val = octave::numeric_limits<double>::NaN (); |
10356
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, , |
21966
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
298 doc: /* -*- texinfo -*- |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
299 @deftypefn {} {} str2double (@var{s}) |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
300 Convert a string to a real or complex number. |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
301 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
302 The string must be in one of the following formats where a and b are real |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
303 numbers and the complex unit is @qcode{'i'} or @qcode{'j'}: |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
304 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
305 @itemize |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
306 @item a + bi |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
307 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
308 @item a + b*i |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
309 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
310 @item a + i*b |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
311 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
312 @item bi + a |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
313 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
314 @item b*i + a |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
315 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
316 @item i*b + a |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
317 @end itemize |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
318 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
319 If present, a and/or b are of the form @nospell{[+-]d[,.]d[[eE][+-]d]} where |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
320 the brackets indicate optional arguments and @qcode{'d'} indicates zero or |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
321 more digits. The special input values @code{Inf}, @code{NaN}, and @code{NA} |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
322 are also accepted. |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
323 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
324 @var{s} may be a character string, character matrix, or cell array. For |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
325 character arrays the conversion is repeated for every row, and a double or |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
326 complex array is returned. Empty rows in @var{s} are deleted and not |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
327 returned in the numeric array. For cell arrays each character string |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
328 element is processed and a double or complex array of the same dimensions as |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
329 @var{s} is returned. |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
330 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
331 For unconvertible scalar or character string input @code{str2double} returns |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
332 a NaN@. Similarly, for character array input @code{str2double} returns a |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
333 NaN for any row of @var{s} that could not be converted. For a cell array, |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
334 @code{str2double} returns a NaN for any element of @var{s} for which |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
335 conversion fails. Note that numeric elements in a mixed string/numeric |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
336 cell array are not strings and the conversion will fail for these elements |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
337 and return NaN. |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
338 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
339 @code{str2double} can replace @code{str2num}, and it avoids the security |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
340 risk of using @code{eval} on unknown data. |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
341 @seealso{str2num} |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
342 @end deftypefn */) |
10356
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
343 { |
12735
76e2c55906de
str2double.cc: Return NaN for invalid inputs rather than printing error.
Rik <octave@nomad.inbox5.com>
parents:
12703
diff
changeset
|
344 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
|
345 print_usage (); |
20802
8bb38ba1bad6
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20555
diff
changeset
|
346 |
20939
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
347 octave_value retval; |
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
348 |
20802
8bb38ba1bad6
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20555
diff
changeset
|
349 if (args(0).is_string ()) |
10356
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
350 { |
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
|
351 if (args(0).rows () == 0 || args(0).columns () == 0) |
21723
bae585228161
use namespace for numeric_limits
John W. Eaton <jwe@octave.org>
parents:
21721
diff
changeset
|
352 retval = Matrix (1, 1, octave::numeric_limits<double>::NaN ()); |
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
|
353 else if (args(0).rows () == 1 && args(0).ndims () == 2) |
20939
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
354 retval = str2double1 (args(0).string_value ()); |
10356
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
355 else |
12735
76e2c55906de
str2double.cc: Return NaN for invalid inputs rather than printing error.
Rik <octave@nomad.inbox5.com>
parents:
12703
diff
changeset
|
356 { |
20990
fc9cca99b2de
Deprecate all_strings, replace with string_vector_value.
Rik <rik@octave.org>
parents:
20946
diff
changeset
|
357 const string_vector sv = args(0).string_vector_value (); |
20555
f90c8372b7ba
eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
358 |
f90c8372b7ba
eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
359 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
|
360 } |
76e2c55906de
str2double.cc: Return NaN for invalid inputs rather than printing error.
Rik <octave@nomad.inbox5.com>
parents:
12703
diff
changeset
|
361 } |
76e2c55906de
str2double.cc: Return NaN for invalid inputs rather than printing error.
Rik <octave@nomad.inbox5.com>
parents:
12703
diff
changeset
|
362 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
|
363 { |
13141
e81ddf9cacd5
maint: untabify and remove trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
12735
diff
changeset
|
364 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
|
365 |
21723
bae585228161
use namespace for numeric_limits
John W. Eaton <jwe@octave.org>
parents:
21721
diff
changeset
|
366 ComplexNDArray output (cell.dims (), octave::numeric_limits<double>::NaN ()); |
20939
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
367 |
20555
f90c8372b7ba
eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
368 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
|
369 { |
20555
f90c8372b7ba
eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
370 if (cell(i).is_string ()) |
f90c8372b7ba
eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
371 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
|
372 } |
20555
f90c8372b7ba
eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
373 retval = output; |
10356
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
374 } |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
375 else |
21723
bae585228161
use namespace for numeric_limits
John W. Eaton <jwe@octave.org>
parents:
21721
diff
changeset
|
376 retval = Matrix (1, 1, octave::numeric_limits<double>::NaN ()); |
13141
e81ddf9cacd5
maint: untabify and remove trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
12735
diff
changeset
|
377 |
10356
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
378 return retval; |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
379 } |
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 /* |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
382 %!assert (str2double ("1"), 1) |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
383 %!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
|
384 %!assert (str2double (char ("1", "2 3", "4i")), [1; NaN; 4i]) |
10356
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
385 %!assert (str2double ("1,222.5"), 1222.5) |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
386 %!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
|
387 %!assert (str2double ("2j"), 2i) |
10356
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
388 %!assert (str2double ("2 + j"), 2+j) |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
389 %!assert (str2double ("i*2 + 3"), 3+2i) |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
390 %!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
|
391 %!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
|
392 %!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
|
393 %!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
|
394 %!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
|
395 %!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
|
396 %!assert (str2double ("Hello World"), NaN) |
10357
7658cd4bdcf2
handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10356
diff
changeset
|
397 %!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
|
398 %!assert (str2double ("NA"), NA) |
10357
7658cd4bdcf2
handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10356
diff
changeset
|
399 %!assert (str2double ("Inf"), Inf) |
14702
a08f6e17336e
str2double.cc: Case-insensitive detection of 'Inf' (Bug #36536).
Rik <octave@nomad.inbox5.com>
parents:
14501
diff
changeset
|
400 %!assert (str2double ("iNF"), Inf) |
10357
7658cd4bdcf2
handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10356
diff
changeset
|
401 %!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
|
402 %!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
|
403 %!assert (str2double ("iNF*i"), complex (0, Inf)) |
10357
7658cd4bdcf2
handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10356
diff
changeset
|
404 %!assert (str2double ("NaN + Inf*i"), complex (NaN, Inf)) |
7658cd4bdcf2
handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10356
diff
changeset
|
405 %!assert (str2double ("Inf - Inf*i"), complex (Inf, -Inf)) |
7658cd4bdcf2
handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10356
diff
changeset
|
406 %!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
|
407 %!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
|
408 %!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
|
409 %!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
|
410 %!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
|
411 %!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
|
412 %!assert (str2double (char(zeros(3,0))), NaN) |
20946 | 413 */ |