Mercurial > hg > octave-jordi
annotate src/DLD-FUNCTIONS/str2double.cc @ 14077:b6eeeb67fa3f stable
str2double: return NaN for things like "1 2 3 4" (bug #34713).
* str2double.cc (single_num, extract_num): Skip spaces as needed.
(str2double1): Don't skip all spaces.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Tue, 20 Dec 2011 18:06:07 -0500 |
parents | e81ddf9cacd5 |
children | c3ea07298af4 |
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 |
11523 | 3 Copyright (C) 2010-2011 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" |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
37 #include "defun-dld.h" |
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 |
7658cd4bdcf2
handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10356
diff
changeset
|
45 static std::istringstream& |
7658cd4bdcf2
handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10356
diff
changeset
|
46 single_num (std::istringstream& is, double& num) |
7658cd4bdcf2
handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10356
diff
changeset
|
47 { |
7658cd4bdcf2
handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10356
diff
changeset
|
48 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
|
49 |
b6eeeb67fa3f
str2double: return NaN for things like "1 2 3 4" (bug #34713).
John W. Eaton <jwe@octave.org>
parents:
13141
diff
changeset
|
50 // 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
|
51 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
|
52 { |
b6eeeb67fa3f
str2double: return NaN for things like "1 2 3 4" (bug #34713).
John W. Eaton <jwe@octave.org>
parents:
13141
diff
changeset
|
53 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
|
54 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
|
55 } |
b6eeeb67fa3f
str2double: return NaN for things like "1 2 3 4" (bug #34713).
John W. Eaton <jwe@octave.org>
parents:
13141
diff
changeset
|
56 |
10357
7658cd4bdcf2
handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10356
diff
changeset
|
57 if (c == 'I') |
7658cd4bdcf2
handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10356
diff
changeset
|
58 { |
7658cd4bdcf2
handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10356
diff
changeset
|
59 // It's infinity. |
7658cd4bdcf2
handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10356
diff
changeset
|
60 is.get (); |
7658cd4bdcf2
handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10356
diff
changeset
|
61 char c1 = is.get (), c2 = is.get (); |
7658cd4bdcf2
handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10356
diff
changeset
|
62 if (c1 == 'n' && c2 == 'f') |
7658cd4bdcf2
handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10356
diff
changeset
|
63 { |
7658cd4bdcf2
handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10356
diff
changeset
|
64 num = octave_Inf; |
7658cd4bdcf2
handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10356
diff
changeset
|
65 is.peek (); // Sets eof bit. |
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 else |
7658cd4bdcf2
handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10356
diff
changeset
|
68 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
|
69 } |
7658cd4bdcf2
handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10356
diff
changeset
|
70 else if (c == 'N') |
7658cd4bdcf2
handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10356
diff
changeset
|
71 { |
11414
5e6ef42a2445
Overhaul str2double function fixing bug and docstring and adding 'NA' processing.
Rik <octave@nomad.inbox5.com>
parents:
10846
diff
changeset
|
72 // It's NA or NaN |
10357
7658cd4bdcf2
handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10356
diff
changeset
|
73 is.get (); |
11414
5e6ef42a2445
Overhaul str2double function fixing bug and docstring and adding 'NA' processing.
Rik <octave@nomad.inbox5.com>
parents:
10846
diff
changeset
|
74 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
|
75 if (c1 == 'A') |
10357
7658cd4bdcf2
handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10356
diff
changeset
|
76 { |
11414
5e6ef42a2445
Overhaul str2double function fixing bug and docstring and adding 'NA' processing.
Rik <octave@nomad.inbox5.com>
parents:
10846
diff
changeset
|
77 num = octave_NA; |
10357
7658cd4bdcf2
handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10356
diff
changeset
|
78 is.peek (); // Sets eof bit. |
7658cd4bdcf2
handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10356
diff
changeset
|
79 } |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11553
diff
changeset
|
80 else |
11414
5e6ef42a2445
Overhaul str2double function fixing bug and docstring and adding 'NA' processing.
Rik <octave@nomad.inbox5.com>
parents:
10846
diff
changeset
|
81 { |
5e6ef42a2445
Overhaul str2double function fixing bug and docstring and adding 'NA' processing.
Rik <octave@nomad.inbox5.com>
parents:
10846
diff
changeset
|
82 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
|
83 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
|
84 { |
5e6ef42a2445
Overhaul str2double function fixing bug and docstring and adding 'NA' processing.
Rik <octave@nomad.inbox5.com>
parents:
10846
diff
changeset
|
85 num = octave_NaN; |
5e6ef42a2445
Overhaul str2double function fixing bug and docstring and adding 'NA' processing.
Rik <octave@nomad.inbox5.com>
parents:
10846
diff
changeset
|
86 is.peek (); // Sets eof bit. |
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 else |
5e6ef42a2445
Overhaul str2double function fixing bug and docstring and adding 'NA' processing.
Rik <octave@nomad.inbox5.com>
parents:
10846
diff
changeset
|
89 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
|
90 } |
10357
7658cd4bdcf2
handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10356
diff
changeset
|
91 } |
7658cd4bdcf2
handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10356
diff
changeset
|
92 else |
7658cd4bdcf2
handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10356
diff
changeset
|
93 is >> num; |
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 return is; |
7658cd4bdcf2
handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10356
diff
changeset
|
96 } |
10356
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
97 |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
98 static std::istringstream& |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
99 extract_num (std::istringstream& is, double& num, bool& imag, bool& have_sign) |
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 have_sign = imag = false; |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
102 |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
103 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
|
104 |
b6eeeb67fa3f
str2double: return NaN for things like "1 2 3 4" (bug #34713).
John W. Eaton <jwe@octave.org>
parents:
13141
diff
changeset
|
105 // 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
|
106 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
|
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 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
|
109 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
|
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 |
10356
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
112 bool negative = false; |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
113 |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
114 // Accept leading sign. |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
115 if (c == '+' || c == '-') |
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 negative = c == '-'; |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
118 is.get (); |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
119 c = is.peek (); |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
120 have_sign = true; |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
121 } |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
122 |
14077
b6eeeb67fa3f
str2double: return NaN for things like "1 2 3 4" (bug #34713).
John W. Eaton <jwe@octave.org>
parents:
13141
diff
changeset
|
123 // 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
|
124 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
|
125 { |
b6eeeb67fa3f
str2double: return NaN for things like "1 2 3 4" (bug #34713).
John W. Eaton <jwe@octave.org>
parents:
13141
diff
changeset
|
126 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
|
127 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
|
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 |
11414
5e6ef42a2445
Overhaul str2double function fixing bug and docstring and adding 'NA' processing.
Rik <octave@nomad.inbox5.com>
parents:
10846
diff
changeset
|
130 // It's i*num or just i. |
10356
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
131 if (is_imag_unit (c)) |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
132 { |
10357
7658cd4bdcf2
handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10356
diff
changeset
|
133 c = is.get (); |
10356
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
134 imag = true; |
10357
7658cd4bdcf2
handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10356
diff
changeset
|
135 char cn = 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
|
136 |
b6eeeb67fa3f
str2double: return NaN for things like "1 2 3 4" (bug #34713).
John W. Eaton <jwe@octave.org>
parents:
13141
diff
changeset
|
137 // Skip spaces after imaginary unit. |
b6eeeb67fa3f
str2double: return NaN for things like "1 2 3 4" (bug #34713).
John W. Eaton <jwe@octave.org>
parents:
13141
diff
changeset
|
138 while (isspace (cn)) |
b6eeeb67fa3f
str2double: return NaN for things like "1 2 3 4" (bug #34713).
John W. Eaton <jwe@octave.org>
parents:
13141
diff
changeset
|
139 { |
b6eeeb67fa3f
str2double: return NaN for things like "1 2 3 4" (bug #34713).
John W. Eaton <jwe@octave.org>
parents:
13141
diff
changeset
|
140 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
|
141 cn = 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
|
142 } |
b6eeeb67fa3f
str2double: return NaN for things like "1 2 3 4" (bug #34713).
John W. Eaton <jwe@octave.org>
parents:
13141
diff
changeset
|
143 |
10357
7658cd4bdcf2
handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10356
diff
changeset
|
144 if (cn == '*') |
10356
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
145 { |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
146 // Multiplier follows, we extract it as a number. |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
147 is.get (); |
10357
7658cd4bdcf2
handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10356
diff
changeset
|
148 single_num (is, num); |
10356
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
149 } |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
150 else |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
151 num = 1.0; |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
152 } |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
153 else |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
154 { |
11414
5e6ef42a2445
Overhaul str2double function fixing bug and docstring and adding 'NA' processing.
Rik <octave@nomad.inbox5.com>
parents:
10846
diff
changeset
|
155 // It's num, num*i, or numi. |
10357
7658cd4bdcf2
handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10356
diff
changeset
|
156 single_num (is, num); |
10356
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
157 if (is.good ()) |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
158 { |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
159 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
|
160 |
b6eeeb67fa3f
str2double: return NaN for things like "1 2 3 4" (bug #34713).
John W. Eaton <jwe@octave.org>
parents:
13141
diff
changeset
|
161 // 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
|
162 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
|
163 { |
b6eeeb67fa3f
str2double: return NaN for things like "1 2 3 4" (bug #34713).
John W. Eaton <jwe@octave.org>
parents:
13141
diff
changeset
|
164 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
|
165 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
|
166 } |
b6eeeb67fa3f
str2double: return NaN for things like "1 2 3 4" (bug #34713).
John W. Eaton <jwe@octave.org>
parents:
13141
diff
changeset
|
167 |
10356
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
168 if (c == '*') |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
169 { |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
170 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
|
171 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
|
172 |
b6eeeb67fa3f
str2double: return NaN for things like "1 2 3 4" (bug #34713).
John W. Eaton <jwe@octave.org>
parents:
13141
diff
changeset
|
173 // 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
|
174 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
|
175 { |
b6eeeb67fa3f
str2double: return NaN for things like "1 2 3 4" (bug #34713).
John W. Eaton <jwe@octave.org>
parents:
13141
diff
changeset
|
176 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
|
177 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
|
178 } |
b6eeeb67fa3f
str2double: return NaN for things like "1 2 3 4" (bug #34713).
John W. Eaton <jwe@octave.org>
parents:
13141
diff
changeset
|
179 |
10356
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
180 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
|
181 { |
5e6ef42a2445
Overhaul str2double function fixing bug and docstring and adding 'NA' processing.
Rik <octave@nomad.inbox5.com>
parents:
10846
diff
changeset
|
182 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
|
183 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
|
184 is.peek (); // Sets eof bit. |
11414
5e6ef42a2445
Overhaul str2double function fixing bug and docstring and adding 'NA' processing.
Rik <octave@nomad.inbox5.com>
parents:
10846
diff
changeset
|
185 } |
10356
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
186 else |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
187 is.setstate (std::ios::failbit); // indicate that read has failed. |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
188 } |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
189 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
|
190 { |
5e6ef42a2445
Overhaul str2double function fixing bug and docstring and adding 'NA' processing.
Rik <octave@nomad.inbox5.com>
parents:
10846
diff
changeset
|
191 imag = true; |
5e6ef42a2445
Overhaul str2double function fixing bug and docstring and adding 'NA' processing.
Rik <octave@nomad.inbox5.com>
parents:
10846
diff
changeset
|
192 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
|
193 is.peek (); // Sets eof bit. |
11414
5e6ef42a2445
Overhaul str2double function fixing bug and docstring and adding 'NA' processing.
Rik <octave@nomad.inbox5.com>
parents:
10846
diff
changeset
|
194 } |
10356
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
195 } |
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 |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
198 if (negative) |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
199 num = -num; |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
200 |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
201 return is; |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
202 } |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
203 |
10379
f578e6468d0c
use C++-0x code in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10378
diff
changeset
|
204 static inline void |
f578e6468d0c
use C++-0x code in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10378
diff
changeset
|
205 set_component (Complex& c, double num, bool imag) |
f578e6468d0c
use C++-0x code in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10378
diff
changeset
|
206 { |
10380
60acc47c203f
configure checks for complex element setter/reference accessor methods
John W. Eaton <jwe@octave.org>
parents:
10379
diff
changeset
|
207 #if defined (HAVE_CXX_COMPLEX_SETTERS) |
10379
f578e6468d0c
use C++-0x code in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10378
diff
changeset
|
208 if (imag) |
10380
60acc47c203f
configure checks for complex element setter/reference accessor methods
John W. Eaton <jwe@octave.org>
parents:
10379
diff
changeset
|
209 c.imag (num); |
10379
f578e6468d0c
use C++-0x code in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10378
diff
changeset
|
210 else |
10380
60acc47c203f
configure checks for complex element setter/reference accessor methods
John W. Eaton <jwe@octave.org>
parents:
10379
diff
changeset
|
211 c.real (num); |
60acc47c203f
configure checks for complex element setter/reference accessor methods
John W. Eaton <jwe@octave.org>
parents:
10379
diff
changeset
|
212 #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
|
213 if (imag) |
60acc47c203f
configure checks for complex element setter/reference accessor methods
John W. Eaton <jwe@octave.org>
parents:
10379
diff
changeset
|
214 c.imag () = num; |
60acc47c203f
configure checks for complex element setter/reference accessor methods
John W. Eaton <jwe@octave.org>
parents:
10379
diff
changeset
|
215 else |
60acc47c203f
configure checks for complex element setter/reference accessor methods
John W. Eaton <jwe@octave.org>
parents:
10379
diff
changeset
|
216 c.real () = num; |
10379
f578e6468d0c
use C++-0x code in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10378
diff
changeset
|
217 #else |
f578e6468d0c
use C++-0x code in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10378
diff
changeset
|
218 if (imag) |
f578e6468d0c
use C++-0x code in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10378
diff
changeset
|
219 c = Complex (c.real (), num); |
f578e6468d0c
use C++-0x code in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10378
diff
changeset
|
220 else |
f578e6468d0c
use C++-0x code in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10378
diff
changeset
|
221 c = Complex (num, c.imag ()); |
f578e6468d0c
use C++-0x code in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10378
diff
changeset
|
222 #endif |
f578e6468d0c
use C++-0x code in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10378
diff
changeset
|
223 } |
f578e6468d0c
use C++-0x code in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10378
diff
changeset
|
224 |
10356
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
225 static Complex |
10381 | 226 str2double1 (const std::string& str_arg) |
10356
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
227 { |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
228 Complex val (0.0, 0.0); |
10381 | 229 |
230 std::string str = str_arg; | |
231 | |
14077
b6eeeb67fa3f
str2double: return NaN for things like "1 2 3 4" (bug #34713).
John W. Eaton <jwe@octave.org>
parents:
13141
diff
changeset
|
232 // FIXME -- removing all commas does too much... |
10356
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
233 std::string::iterator se = str.end (); |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
234 se = std::remove (str.begin (), se, ','); |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
235 str.erase (se, 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
|
236 std::istringstream is (str); |
10356
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 double num; |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
239 bool i1, i2, s1, s2; |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
240 |
10357
7658cd4bdcf2
handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10356
diff
changeset
|
241 if (is.eof ()) |
7658cd4bdcf2
handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10356
diff
changeset
|
242 val = octave_NaN; |
7658cd4bdcf2
handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10356
diff
changeset
|
243 else if (! extract_num (is, num, i1, s1)) |
10356
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
244 val = octave_NaN; |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
245 else |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
246 { |
10380
60acc47c203f
configure checks for complex element setter/reference accessor methods
John W. Eaton <jwe@octave.org>
parents:
10379
diff
changeset
|
247 set_component (val, num, i1); |
10356
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
248 |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
249 if (! is.eof ()) |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
250 { |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
251 if (! extract_num (is, num, i2, s2) || i1 == i2 || ! s2) |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
252 val = octave_NaN; |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
253 else |
10380
60acc47c203f
configure checks for complex element setter/reference accessor methods
John W. Eaton <jwe@octave.org>
parents:
10379
diff
changeset
|
254 set_component (val, num, i2); |
10356
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
255 } |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
256 } |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
257 |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
258 return val; |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
259 } |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
260 |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
261 DEFUN_DLD (str2double, args, , |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
262 "-*- texinfo -*-\n\ |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
263 @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
|
264 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
|
265 \n\ |
5e6ef42a2445
Overhaul str2double function fixing bug and docstring and adding 'NA' processing.
Rik <octave@nomad.inbox5.com>
parents:
10846
diff
changeset
|
266 The string must be in one of the following formats where\n\ |
5e6ef42a2445
Overhaul str2double function fixing bug and docstring and adding 'NA' processing.
Rik <octave@nomad.inbox5.com>
parents:
10846
diff
changeset
|
267 a and b are real numbers and the complex unit is 'i' or 'j':\n\ |
10356
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
268 \n\ |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
269 @itemize\n\ |
10840 | 270 @item a + bi\n\ |
271 \n\ | |
272 @item a + b*i\n\ | |
273 \n\ | |
274 @item a + i*b\n\ | |
275 \n\ | |
276 @item bi + a\n\ | |
277 \n\ | |
278 @item b*i + a\n\ | |
279 \n\ | |
280 @item i*b + a\n\ | |
10356
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
281 @end itemize\n\ |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
282 \n\ |
11591
1d13679b587e
Use @nospell macro on certain words in docstrings.
Rik <octave@nomad.inbox5.com>
parents:
11586
diff
changeset
|
283 If present, a and/or b are of the form @nospell{[+-]d[,.]d[[eE][+-]d]} where\n\ |
1d13679b587e
Use @nospell macro on certain words in docstrings.
Rik <octave@nomad.inbox5.com>
parents:
11586
diff
changeset
|
284 the brackets indicate optional arguments and 'd' indicates zero or more\n\ |
11414
5e6ef42a2445
Overhaul str2double function fixing bug and docstring and adding 'NA' processing.
Rik <octave@nomad.inbox5.com>
parents:
10846
diff
changeset
|
285 digits. The special input values @code{Inf}, @code{NaN}, and @code{NA} are\n\ |
5e6ef42a2445
Overhaul str2double function fixing bug and docstring and adding 'NA' processing.
Rik <octave@nomad.inbox5.com>
parents:
10846
diff
changeset
|
286 also accepted.\n\ |
5e6ef42a2445
Overhaul str2double function fixing bug and docstring and adding 'NA' processing.
Rik <octave@nomad.inbox5.com>
parents:
10846
diff
changeset
|
287 \n\ |
5e6ef42a2445
Overhaul str2double function fixing bug and docstring and adding 'NA' processing.
Rik <octave@nomad.inbox5.com>
parents:
10846
diff
changeset
|
288 @var{s} may also be a character matrix, in which case the conversion is\n\ |
5e6ef42a2445
Overhaul str2double function fixing bug and docstring and adding 'NA' processing.
Rik <octave@nomad.inbox5.com>
parents:
10846
diff
changeset
|
289 repeated for each row. Or @var{s} may be a cell array of strings, in which\n\ |
5e6ef42a2445
Overhaul str2double function fixing bug and docstring and adding 'NA' processing.
Rik <octave@nomad.inbox5.com>
parents:
10846
diff
changeset
|
290 case each element is converted and an array of the same dimensions is\n\ |
5e6ef42a2445
Overhaul str2double function fixing bug and docstring and adding 'NA' processing.
Rik <octave@nomad.inbox5.com>
parents:
10846
diff
changeset
|
291 returned.\n\ |
5e6ef42a2445
Overhaul str2double function fixing bug and docstring and adding 'NA' processing.
Rik <octave@nomad.inbox5.com>
parents:
10846
diff
changeset
|
292 \n\ |
12703
d326be468b13
doc: str2double() inputs which cannot be parsed return NaN.
Rik <octave@nomad.inbox5.com>
parents:
11591
diff
changeset
|
293 @code{str2double} returns NaN for elements of @var{s} which cannot be\n\ |
d326be468b13
doc: str2double() inputs which cannot be parsed return NaN.
Rik <octave@nomad.inbox5.com>
parents:
11591
diff
changeset
|
294 converted.\n\ |
d326be468b13
doc: str2double() inputs which cannot be parsed return NaN.
Rik <octave@nomad.inbox5.com>
parents:
11591
diff
changeset
|
295 \n\ |
d326be468b13
doc: str2double() inputs which cannot be parsed return NaN.
Rik <octave@nomad.inbox5.com>
parents:
11591
diff
changeset
|
296 @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
|
297 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
|
298 @seealso{str2num}\n\ |
10356
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
299 @end deftypefn") |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
300 { |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
301 octave_value retval; |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
302 |
12735
76e2c55906de
str2double.cc: Return NaN for invalid inputs rather than printing error.
Rik <octave@nomad.inbox5.com>
parents:
12703
diff
changeset
|
303 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
|
304 print_usage (); |
76e2c55906de
str2double.cc: Return NaN for invalid inputs rather than printing error.
Rik <octave@nomad.inbox5.com>
parents:
12703
diff
changeset
|
305 else if (args(0).is_string ()) |
10356
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
306 { |
12735
76e2c55906de
str2double.cc: Return NaN for invalid inputs rather than printing error.
Rik <octave@nomad.inbox5.com>
parents:
12703
diff
changeset
|
307 if (args(0).rows () == 1 && args(0).ndims () == 2) |
10356
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
308 { |
12735
76e2c55906de
str2double.cc: Return NaN for invalid inputs rather than printing error.
Rik <octave@nomad.inbox5.com>
parents:
12703
diff
changeset
|
309 retval = str2double1 (args(0).string_value ()); |
10356
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
310 } |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
311 else |
12735
76e2c55906de
str2double.cc: Return NaN for invalid inputs rather than printing error.
Rik <octave@nomad.inbox5.com>
parents:
12703
diff
changeset
|
312 { |
76e2c55906de
str2double.cc: Return NaN for invalid inputs rather than printing error.
Rik <octave@nomad.inbox5.com>
parents:
12703
diff
changeset
|
313 const string_vector sv = args(0).all_strings (); |
76e2c55906de
str2double.cc: Return NaN for invalid inputs rather than printing error.
Rik <octave@nomad.inbox5.com>
parents:
12703
diff
changeset
|
314 if (! error_state) |
76e2c55906de
str2double.cc: Return NaN for invalid inputs rather than printing error.
Rik <octave@nomad.inbox5.com>
parents:
12703
diff
changeset
|
315 retval = sv.map<Complex> (str2double1); |
76e2c55906de
str2double.cc: Return NaN for invalid inputs rather than printing error.
Rik <octave@nomad.inbox5.com>
parents:
12703
diff
changeset
|
316 } |
76e2c55906de
str2double.cc: Return NaN for invalid inputs rather than printing error.
Rik <octave@nomad.inbox5.com>
parents:
12703
diff
changeset
|
317 } |
76e2c55906de
str2double.cc: Return NaN for invalid inputs rather than printing error.
Rik <octave@nomad.inbox5.com>
parents:
12703
diff
changeset
|
318 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
|
319 { |
13141
e81ddf9cacd5
maint: untabify and remove trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
12735
diff
changeset
|
320 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
|
321 |
76e2c55906de
str2double.cc: Return NaN for invalid inputs rather than printing error.
Rik <octave@nomad.inbox5.com>
parents:
12703
diff
changeset
|
322 if (! error_state) |
76e2c55906de
str2double.cc: Return NaN for invalid inputs rather than printing error.
Rik <octave@nomad.inbox5.com>
parents:
12703
diff
changeset
|
323 { |
76e2c55906de
str2double.cc: Return NaN for invalid inputs rather than printing error.
Rik <octave@nomad.inbox5.com>
parents:
12703
diff
changeset
|
324 ComplexNDArray output (cell.dims (), octave_NaN); |
76e2c55906de
str2double.cc: Return NaN for invalid inputs rather than printing error.
Rik <octave@nomad.inbox5.com>
parents:
12703
diff
changeset
|
325 for (octave_idx_type i = 0; i < cell.numel (); i++) |
76e2c55906de
str2double.cc: Return NaN for invalid inputs rather than printing error.
Rik <octave@nomad.inbox5.com>
parents:
12703
diff
changeset
|
326 { |
76e2c55906de
str2double.cc: Return NaN for invalid inputs rather than printing error.
Rik <octave@nomad.inbox5.com>
parents:
12703
diff
changeset
|
327 if (cell(i).is_string ()) |
76e2c55906de
str2double.cc: Return NaN for invalid inputs rather than printing error.
Rik <octave@nomad.inbox5.com>
parents:
12703
diff
changeset
|
328 output(i) = str2double1 (cell(i).string_value ()); |
76e2c55906de
str2double.cc: Return NaN for invalid inputs rather than printing error.
Rik <octave@nomad.inbox5.com>
parents:
12703
diff
changeset
|
329 } |
76e2c55906de
str2double.cc: Return NaN for invalid inputs rather than printing error.
Rik <octave@nomad.inbox5.com>
parents:
12703
diff
changeset
|
330 retval = output; |
76e2c55906de
str2double.cc: Return NaN for invalid inputs rather than printing error.
Rik <octave@nomad.inbox5.com>
parents:
12703
diff
changeset
|
331 } |
10356
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
332 } |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
333 else |
12735
76e2c55906de
str2double.cc: Return NaN for invalid inputs rather than printing error.
Rik <octave@nomad.inbox5.com>
parents:
12703
diff
changeset
|
334 retval = NDArray (args(0).dims (), octave_NaN); |
13141
e81ddf9cacd5
maint: untabify and remove trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
12735
diff
changeset
|
335 |
10356
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
336 |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
337 return retval; |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
338 } |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
339 |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
340 /* |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
341 |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
342 %!assert (str2double ("1"), 1) |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
343 %!assert (str2double ("-.1e-5"), -1e-6) |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
344 %!assert (str2double ("1,222.5"), 1222.5) |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
345 %!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
|
346 %!assert (str2double ("2j"), 2i) |
10356
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
347 %!assert (str2double ("2 + j"), 2+j) |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
348 %!assert (str2double ("i*2 + 3"), 3+2i) |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
349 %!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
|
350 %!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
|
351 %!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
|
352 %!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
|
353 %!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
|
354 %!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
|
355 %!assert (str2double ("Hello World"), NaN) |
10357
7658cd4bdcf2
handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10356
diff
changeset
|
356 %!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
|
357 %!assert (str2double ("NA"), NA) |
10357
7658cd4bdcf2
handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10356
diff
changeset
|
358 %!assert (str2double ("Inf"), Inf) |
7658cd4bdcf2
handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10356
diff
changeset
|
359 %!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
|
360 %!assert (str2double ("Inf*i"), complex (0, Inf)) |
10357
7658cd4bdcf2
handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10356
diff
changeset
|
361 %!assert (str2double ("NaN + Inf*i"), complex (NaN, Inf)) |
7658cd4bdcf2
handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10356
diff
changeset
|
362 %!assert (str2double ("Inf - Inf*i"), complex (Inf, -Inf)) |
7658cd4bdcf2
handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10356
diff
changeset
|
363 %!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
|
364 %!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
|
365 %!assert (str2double ({2, "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
|
366 %!assert (str2double (zeros(3,1,2)), NaN (3,1,2)) |
10356
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
367 |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
368 */ |