Mercurial > octave-nkf
annotate src/DLD-FUNCTIONS/str2double.cc @ 10846:a4f482e66b65
Grammarcheck more of the documentation.
Use @noindent macro appropriately.
Limit line length to 80 characters.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Sun, 01 Aug 2010 20:22:17 -0700 |
parents | 89f4d7e294cc |
children | 5e6ef42a2445 |
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 |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
3 Copyright (C) 2010 Jaroslav Hajek |
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 |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
41 static inline bool |
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 (); |
7658cd4bdcf2
handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10356
diff
changeset
|
49 if (c == 'I') |
7658cd4bdcf2
handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10356
diff
changeset
|
50 { |
7658cd4bdcf2
handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10356
diff
changeset
|
51 // It's infinity. |
7658cd4bdcf2
handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10356
diff
changeset
|
52 is.get (); |
7658cd4bdcf2
handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10356
diff
changeset
|
53 char c1 = is.get (), c2 = is.get (); |
7658cd4bdcf2
handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10356
diff
changeset
|
54 if (c1 == 'n' && c2 == 'f') |
7658cd4bdcf2
handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10356
diff
changeset
|
55 { |
7658cd4bdcf2
handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10356
diff
changeset
|
56 num = octave_Inf; |
7658cd4bdcf2
handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10356
diff
changeset
|
57 is.peek (); // Sets eof bit. |
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 else |
7658cd4bdcf2
handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10356
diff
changeset
|
60 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
|
61 } |
7658cd4bdcf2
handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10356
diff
changeset
|
62 else if (c == 'N') |
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 // It's NaN. |
7658cd4bdcf2
handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10356
diff
changeset
|
65 is.get (); |
7658cd4bdcf2
handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10356
diff
changeset
|
66 char c1 = is.get (), c2 = is.get (); |
7658cd4bdcf2
handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10356
diff
changeset
|
67 if (c1 == 'a' && c2 == 'N') |
7658cd4bdcf2
handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10356
diff
changeset
|
68 { |
7658cd4bdcf2
handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10356
diff
changeset
|
69 num = octave_NaN; |
7658cd4bdcf2
handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10356
diff
changeset
|
70 is.peek (); // Sets eof bit. |
7658cd4bdcf2
handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10356
diff
changeset
|
71 } |
7658cd4bdcf2
handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10356
diff
changeset
|
72 else |
7658cd4bdcf2
handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10356
diff
changeset
|
73 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
|
74 } |
7658cd4bdcf2
handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10356
diff
changeset
|
75 else |
7658cd4bdcf2
handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10356
diff
changeset
|
76 is >> num; |
7658cd4bdcf2
handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10356
diff
changeset
|
77 |
7658cd4bdcf2
handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10356
diff
changeset
|
78 return is; |
7658cd4bdcf2
handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10356
diff
changeset
|
79 } |
10356
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
80 |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
81 static std::istringstream& |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
82 extract_num (std::istringstream& is, double& num, bool& imag, bool& have_sign) |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
83 { |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
84 have_sign = imag = false; |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
85 |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
86 char c = is.peek (); |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
87 bool negative = false; |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
88 |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
89 // Accept leading sign. |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
90 if (c == '+' || c == '-') |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
91 { |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
92 negative = c == '-'; |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
93 is.get (); |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
94 c = is.peek (); |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
95 have_sign = true; |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
96 } |
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 if (is_imag_unit (c)) |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
99 { |
10357
7658cd4bdcf2
handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10356
diff
changeset
|
100 c = is.get (); |
10356
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
101 // It's i*num or just i. |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
102 imag = true; |
10357
7658cd4bdcf2
handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10356
diff
changeset
|
103 char cn = is.peek (); |
7658cd4bdcf2
handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10356
diff
changeset
|
104 if (cn == '*') |
10356
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 // Multiplier follows, we extract it as a number. |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
107 is.get (); |
10357
7658cd4bdcf2
handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10356
diff
changeset
|
108 single_num (is, num); |
10356
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
109 } |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
110 else |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
111 num = 1.0; |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
112 } |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
113 else |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
114 { |
10357
7658cd4bdcf2
handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10356
diff
changeset
|
115 single_num (is, num); |
10356
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
116 if (is.good ()) |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
117 { |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
118 c = is.peek (); |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
119 if (c == '*') |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
120 { |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
121 is.get (); |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
122 c = is.get (); |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
123 if (is_imag_unit (c)) |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
124 imag = true; |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
125 else |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
126 is.setstate (std::ios::failbit); // indicate that read has failed. |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
127 } |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
128 else if (is_imag_unit (c)) |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
129 imag = true; |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
130 } |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
131 } |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
132 |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
133 if (negative) |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
134 num = -num; |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
135 |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
136 return is; |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
137 } |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
138 |
10379
f578e6468d0c
use C++-0x code in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10378
diff
changeset
|
139 static inline void |
f578e6468d0c
use C++-0x code in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10378
diff
changeset
|
140 set_component (Complex& c, double num, bool imag) |
f578e6468d0c
use C++-0x code in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10378
diff
changeset
|
141 { |
10380
60acc47c203f
configure checks for complex element setter/reference accessor methods
John W. Eaton <jwe@octave.org>
parents:
10379
diff
changeset
|
142 #if defined (HAVE_CXX_COMPLEX_SETTERS) |
10379
f578e6468d0c
use C++-0x code in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10378
diff
changeset
|
143 if (imag) |
10380
60acc47c203f
configure checks for complex element setter/reference accessor methods
John W. Eaton <jwe@octave.org>
parents:
10379
diff
changeset
|
144 c.imag (num); |
10379
f578e6468d0c
use C++-0x code in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10378
diff
changeset
|
145 else |
10380
60acc47c203f
configure checks for complex element setter/reference accessor methods
John W. Eaton <jwe@octave.org>
parents:
10379
diff
changeset
|
146 c.real (num); |
60acc47c203f
configure checks for complex element setter/reference accessor methods
John W. Eaton <jwe@octave.org>
parents:
10379
diff
changeset
|
147 #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
|
148 if (imag) |
60acc47c203f
configure checks for complex element setter/reference accessor methods
John W. Eaton <jwe@octave.org>
parents:
10379
diff
changeset
|
149 c.imag () = num; |
60acc47c203f
configure checks for complex element setter/reference accessor methods
John W. Eaton <jwe@octave.org>
parents:
10379
diff
changeset
|
150 else |
60acc47c203f
configure checks for complex element setter/reference accessor methods
John W. Eaton <jwe@octave.org>
parents:
10379
diff
changeset
|
151 c.real () = num; |
10379
f578e6468d0c
use C++-0x code in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10378
diff
changeset
|
152 #else |
f578e6468d0c
use C++-0x code in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10378
diff
changeset
|
153 if (imag) |
f578e6468d0c
use C++-0x code in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10378
diff
changeset
|
154 c = Complex (c.real (), num); |
f578e6468d0c
use C++-0x code in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10378
diff
changeset
|
155 else |
f578e6468d0c
use C++-0x code in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10378
diff
changeset
|
156 c = Complex (num, c.imag ()); |
f578e6468d0c
use C++-0x code in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10378
diff
changeset
|
157 #endif |
f578e6468d0c
use C++-0x code in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10378
diff
changeset
|
158 } |
f578e6468d0c
use C++-0x code in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10378
diff
changeset
|
159 |
10356
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
160 static Complex |
10381 | 161 str2double1 (const std::string& str_arg) |
10356
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
162 { |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
163 Complex val (0.0, 0.0); |
10381 | 164 |
165 std::string str = str_arg; | |
166 | |
10356
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
167 std::string::iterator se = str.end (); |
10381 | 168 |
10356
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
169 // Remove commas (thousand separators) and spaces. |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
170 se = std::remove (str.begin (), se, ','); |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
171 se = std::remove (str.begin (), se, ' '); |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
172 str.erase (se, str.end ()); |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
173 |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
174 std::istringstream is (str); |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
175 double num; |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
176 bool i1, i2, s1, s2; |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
177 |
10357
7658cd4bdcf2
handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10356
diff
changeset
|
178 if (is.eof ()) |
7658cd4bdcf2
handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10356
diff
changeset
|
179 val = octave_NaN; |
7658cd4bdcf2
handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10356
diff
changeset
|
180 else if (! extract_num (is, num, i1, s1)) |
10356
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
181 val = octave_NaN; |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
182 else |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
183 { |
10380
60acc47c203f
configure checks for complex element setter/reference accessor methods
John W. Eaton <jwe@octave.org>
parents:
10379
diff
changeset
|
184 set_component (val, num, i1); |
10356
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 if (! is.eof ()) |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
187 { |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
188 if (! extract_num (is, num, i2, s2) || i1 == i2 || ! s2) |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
189 val = octave_NaN; |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
190 else |
10380
60acc47c203f
configure checks for complex element setter/reference accessor methods
John W. Eaton <jwe@octave.org>
parents:
10379
diff
changeset
|
191 set_component (val, num, i2); |
10356
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
192 } |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
193 } |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
194 |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
195 return val; |
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 DEFUN_DLD (str2double, args, , |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
199 "-*- texinfo -*-\n\ |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
200 @deftypefn {Built-in Function} {} str2double (@var{s})\n\ |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
201 Converts a string to real or complex number.\n\ |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
202 A complex number should be in one of the formats:\n\ |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
203 \n\ |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
204 @itemize\n\ |
10840 | 205 @item a + bi\n\ |
206 \n\ | |
207 @item a + b*i\n\ | |
208 \n\ | |
209 @item a + i*b\n\ | |
210 \n\ | |
211 @item bi + a\n\ | |
212 \n\ | |
213 @item b*i + a\n\ | |
214 \n\ | |
215 @item i*b + a\n\ | |
10356
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
216 @end itemize\n\ |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
217 \n\ |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
218 It is also possible to use @code{j} instead of @code{i}, or write just\n\ |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
219 @code{i} instead of @code{1*i}.\n\ |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
220 @code{a} and @code{b} should be real numbers\n\ |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
221 in a standard format.\n\ |
10846
a4f482e66b65
Grammarcheck more of the documentation.
Rik <octave@nomad.inbox5.com>
parents:
10840
diff
changeset
|
222 @var{s} can also be a character matrix, in which case the conversion is\n\ |
a4f482e66b65
Grammarcheck more of the documentation.
Rik <octave@nomad.inbox5.com>
parents:
10840
diff
changeset
|
223 repeated for each row, or a cell array of strings, in which case each element\n\ |
a4f482e66b65
Grammarcheck more of the documentation.
Rik <octave@nomad.inbox5.com>
parents:
10840
diff
changeset
|
224 is converted and an array of the same dimensions is returned.\n\ |
10356
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
225 @end deftypefn") |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
226 { |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
227 octave_value retval; |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
228 |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
229 if (args.length () == 1) |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
230 { |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
231 if (args(0).is_string ()) |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
232 { |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
233 if (args(0).rows () == 1 && args(0).ndims () == 2) |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
234 { |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
235 retval = str2double1 (args(0).string_value ()); |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
236 } |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
237 else |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
238 { |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
239 const string_vector sv = args(0).all_strings (); |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
240 if (! error_state) |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
241 retval = sv.map<Complex> (str2double1); |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
242 } |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
243 } |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
244 else if (args(0).is_cellstr ()) |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
245 { |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
246 Array<std::string> sa = args(0).cellstr_value (); |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
247 retval = sa.map<Complex> (str2double1); |
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 else |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
250 gripe_wrong_type_arg ("str2double", args(0)); |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
251 } |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
252 else |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
253 print_usage (); |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
254 |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
255 return retval; |
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 /* |
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 %!assert (str2double ("1"), 1) |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
261 %!assert (str2double ("-.1e-5"), -1e-6) |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
262 %!assert (str2double ("1,222.5"), 1222.5) |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
263 %!assert (str2double ("i"), i) |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
264 %!assert (str2double ("2 + j"), 2+j) |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
265 %!assert (str2double ("i*2 + 3"), 3+2i) |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
266 %!assert (str2double (".5*i + 3.5"), 3.5+0.5i) |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
267 %!assert (str2double ("1e-3 + i*.25"), 1e-3 + i*.25) |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
268 %!assert (str2double (["2 + j";"1.25e-3";"-05"]), [2+i; 1.25e-3; -05]) |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
269 %!assert (str2double ({"2 + j","1.25e-3","-05"}), [2+i, 1.25e-3, -05]) |
10357
7658cd4bdcf2
handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10356
diff
changeset
|
270 %!assert (str2double ("NaN"), NaN) |
7658cd4bdcf2
handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10356
diff
changeset
|
271 %!assert (str2double ("Inf"), Inf) |
7658cd4bdcf2
handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10356
diff
changeset
|
272 %!assert (str2double ("-Inf"), -Inf) |
7658cd4bdcf2
handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10356
diff
changeset
|
273 %!assert (str2double ("i*Inf"), complex (0, Inf)) |
7658cd4bdcf2
handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10356
diff
changeset
|
274 %!assert (str2double ("NaN + Inf*i"), complex (NaN, Inf)) |
7658cd4bdcf2
handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10356
diff
changeset
|
275 %!assert (str2double ("Inf - Inf*i"), complex (Inf, -Inf)) |
7658cd4bdcf2
handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
10356
diff
changeset
|
276 %!assert (str2double ("-i*NaN - Inf"), complex (-Inf, -NaN)) |
10356
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
277 |
1d7930b77ab9
implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
278 */ |