Mercurial > hg > octave-lyh
annotate liboctave/lo-utils.cc @ 11013:63f79f798a14
fix small typos in new tests
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Wed, 22 Sep 2010 09:04:56 +0200 |
parents | 9478b216752e |
children | 2554b4a0806e |
rev | line source |
---|---|
1993 | 1 // utils.cc |
1967 | 2 /* |
3 | |
8920 | 4 Copyright (C) 1996, 1997, 2000, 2002, 2003, 2004, 2005, 2006, 2007, 2008 |
7017 | 5 John W. Eaton |
1967 | 6 |
7 This file is part of Octave. | |
8 | |
9 Octave is free software; you can redistribute it and/or modify it | |
10 under the terms of the GNU General Public License as published by the | |
7016 | 11 Free Software Foundation; either version 3 of the License, or (at your |
12 option) any later version. | |
1967 | 13 |
14 Octave is distributed in the hope that it will be useful, but WITHOUT | |
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
17 for more details. | |
18 | |
19 You should have received a copy of the GNU General Public License | |
7016 | 20 along with Octave; see the file COPYING. If not, see |
21 <http://www.gnu.org/licenses/>. | |
1967 | 22 |
23 */ | |
24 | |
25 #ifdef HAVE_CONFIG_H | |
26 #include <config.h> | |
27 #endif | |
28 | |
6907 | 29 #include <cctype> |
10463
bbe99b2a5ba7
undo recent gnulib-related changes
John W. Eaton <jwe@octave.org>
parents:
10447
diff
changeset
|
30 #include <cstdlib> |
2926 | 31 #include <cstdio> |
7048 | 32 #include <cstring> |
11010
9478b216752e
simplify more array tests
Jaroslav Hajek <highegg@gmail.com>
parents:
10463
diff
changeset
|
33 #include <cfloat> |
1967 | 34 |
6490 | 35 #include <limits> |
2926 | 36 #include <string> |
37 | |
38 #include <sys/types.h> | |
39 #include <unistd.h> | |
40 | |
10068
ca93f583573d
handle interrupts octave_fgets
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
41 #include "quit.h" |
ca93f583573d
handle interrupts octave_fgets
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
42 |
2926 | 43 #include "lo-error.h" |
4130 | 44 #include "lo-ieee.h" |
1967 | 45 #include "lo-mappers.h" |
46 #include "lo-utils.h" | |
47 | |
48 // Convert X to the nearest integer value. Should not pass NaN to | |
49 // this function. | |
50 | |
5275 | 51 // Sometimes you need a large integer, but not always. |
52 | |
53 octave_idx_type | |
54 NINTbig (double x) | |
55 { | |
6490 | 56 if (x > std::numeric_limits<octave_idx_type>::max ()) |
57 return std::numeric_limits<octave_idx_type>::max (); | |
58 else if (x < std::numeric_limits<octave_idx_type>::min ()) | |
59 return std::numeric_limits<octave_idx_type>::min (); | |
5275 | 60 else |
61 return static_cast<octave_idx_type> ((x > 0) ? (x + 0.5) : (x - 0.5)); | |
62 } | |
63 | |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
64 octave_idx_type |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
65 NINTbig (float x) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
66 { |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
67 if (x > std::numeric_limits<octave_idx_type>::max ()) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
68 return std::numeric_limits<octave_idx_type>::max (); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
69 else if (x < std::numeric_limits<octave_idx_type>::min ()) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
70 return std::numeric_limits<octave_idx_type>::min (); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
71 else |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
72 return static_cast<octave_idx_type> ((x > 0) ? (x + 0.5) : (x - 0.5)); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
73 } |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
74 |
1967 | 75 int |
76 NINT (double x) | |
77 { | |
6490 | 78 if (x > std::numeric_limits<int>::max ()) |
79 return std::numeric_limits<int>::max (); | |
80 else if (x < std::numeric_limits<int>::min ()) | |
81 return std::numeric_limits<int>::min (); | |
1967 | 82 else |
4784 | 83 return static_cast<int> ((x > 0) ? (x + 0.5) : (x - 0.5)); |
1967 | 84 } |
85 | |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
86 int |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
87 NINT (float x) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
88 { |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
89 if (x > std::numeric_limits<int>::max ()) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
90 return std::numeric_limits<int>::max (); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
91 else if (x < std::numeric_limits<int>::min ()) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
92 return std::numeric_limits<int>::min (); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
93 else |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
94 return static_cast<int> ((x > 0) ? (x + 0.5) : (x - 0.5)); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
95 } |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
96 |
1967 | 97 double |
98 D_NINT (double x) | |
99 { | |
100 if (xisinf (x) || xisnan (x)) | |
101 return x; | |
102 else | |
103 return floor (x + 0.5); | |
104 } | |
105 | |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
106 float |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
107 F_NINT (float x) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
108 { |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
109 if (xisinf (x) || xisnan (x)) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
110 return x; |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
111 else |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
112 return floor (x + 0.5); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
113 } |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
114 |
11010
9478b216752e
simplify more array tests
Jaroslav Hajek <highegg@gmail.com>
parents:
10463
diff
changeset
|
115 bool xis_int_or_inf_or_nan (double x) |
11013
63f79f798a14
fix small typos in new tests
Jaroslav Hajek <highegg@gmail.com>
parents:
11010
diff
changeset
|
116 { return xisnan (x) || D_NINT (x) == x; } |
11010
9478b216752e
simplify more array tests
Jaroslav Hajek <highegg@gmail.com>
parents:
10463
diff
changeset
|
117 |
9478b216752e
simplify more array tests
Jaroslav Hajek <highegg@gmail.com>
parents:
10463
diff
changeset
|
118 bool xis_one_or_zero (double x) |
9478b216752e
simplify more array tests
Jaroslav Hajek <highegg@gmail.com>
parents:
10463
diff
changeset
|
119 { return x == 0 || x == 1; } |
9478b216752e
simplify more array tests
Jaroslav Hajek <highegg@gmail.com>
parents:
10463
diff
changeset
|
120 |
9478b216752e
simplify more array tests
Jaroslav Hajek <highegg@gmail.com>
parents:
10463
diff
changeset
|
121 bool xis_zero (double x) |
9478b216752e
simplify more array tests
Jaroslav Hajek <highegg@gmail.com>
parents:
10463
diff
changeset
|
122 { return x == 0; } |
9478b216752e
simplify more array tests
Jaroslav Hajek <highegg@gmail.com>
parents:
10463
diff
changeset
|
123 |
9478b216752e
simplify more array tests
Jaroslav Hajek <highegg@gmail.com>
parents:
10463
diff
changeset
|
124 bool xtoo_large_for_float (double x) |
9478b216752e
simplify more array tests
Jaroslav Hajek <highegg@gmail.com>
parents:
10463
diff
changeset
|
125 { return (! (xisnan (x) || xisinf (x)) && fabs (x) > FLT_MAX); } |
9478b216752e
simplify more array tests
Jaroslav Hajek <highegg@gmail.com>
parents:
10463
diff
changeset
|
126 |
9478b216752e
simplify more array tests
Jaroslav Hajek <highegg@gmail.com>
parents:
10463
diff
changeset
|
127 bool xis_int_or_inf_or_nan (float x) |
11013
63f79f798a14
fix small typos in new tests
Jaroslav Hajek <highegg@gmail.com>
parents:
11010
diff
changeset
|
128 { return xisnan (x) || D_NINT (x) == x; } |
11010
9478b216752e
simplify more array tests
Jaroslav Hajek <highegg@gmail.com>
parents:
10463
diff
changeset
|
129 |
9478b216752e
simplify more array tests
Jaroslav Hajek <highegg@gmail.com>
parents:
10463
diff
changeset
|
130 bool xis_one_or_zero (float x) |
9478b216752e
simplify more array tests
Jaroslav Hajek <highegg@gmail.com>
parents:
10463
diff
changeset
|
131 { return x == 0 || x == 1; } |
9478b216752e
simplify more array tests
Jaroslav Hajek <highegg@gmail.com>
parents:
10463
diff
changeset
|
132 |
9478b216752e
simplify more array tests
Jaroslav Hajek <highegg@gmail.com>
parents:
10463
diff
changeset
|
133 bool xis_zero (float x) |
9478b216752e
simplify more array tests
Jaroslav Hajek <highegg@gmail.com>
parents:
10463
diff
changeset
|
134 { return x == 0; } |
9478b216752e
simplify more array tests
Jaroslav Hajek <highegg@gmail.com>
parents:
10463
diff
changeset
|
135 |
2926 | 136 // Save a string. |
137 | |
138 char * | |
139 strsave (const char *s) | |
140 { | |
141 if (! s) | |
142 return 0; | |
143 | |
144 int len = strlen (s); | |
145 char *tmp = new char [len+1]; | |
146 tmp = strcpy (tmp, s); | |
147 return tmp; | |
148 } | |
149 | |
150 // This function was adapted from xputenv from Karl Berry's kpathsearch | |
151 // library. | |
152 | |
5775 | 153 // FIXME -- make this do the right thing if we don't have a |
2926 | 154 // SMART_PUTENV. |
155 | |
156 void | |
3504 | 157 octave_putenv (const std::string& name, const std::string& value) |
2926 | 158 { |
159 int new_len = name.length () + value.length () + 2; | |
160 | |
10411 | 161 char *new_item = static_cast<char*> (gnulib::malloc (new_len)); |
2926 | 162 |
163 sprintf (new_item, "%s=%s", name.c_str (), value.c_str ()); | |
164 | |
165 // As far as I can see there's no way to distinguish between the | |
166 // various errors; putenv doesn't have errno values. | |
167 | |
168 if (putenv (new_item) < 0) | |
169 (*current_liboctave_error_handler) ("putenv (%s) failed", new_item); | |
170 } | |
171 | |
3504 | 172 std::string |
2926 | 173 octave_fgets (FILE *f) |
174 { | |
4527 | 175 bool eof; |
176 return octave_fgets (f, eof); | |
177 } | |
178 | |
179 std::string | |
180 octave_fgets (FILE *f, bool& eof) | |
181 { | |
182 eof = false; | |
183 | |
3504 | 184 std::string retval; |
2926 | 185 |
186 int grow_size = 1024; | |
187 int max_size = grow_size; | |
188 | |
10411 | 189 char *buf = static_cast<char *> (gnulib::malloc (max_size)); |
2926 | 190 char *bufptr = buf; |
191 int len = 0; | |
192 | |
193 do | |
194 { | |
195 if (fgets (bufptr, grow_size, f)) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
196 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
197 len = strlen (bufptr); |
2926 | 198 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
199 if (len == grow_size - 1) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
200 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
201 int tmp = bufptr - buf + grow_size - 1; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
202 grow_size *= 2; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
203 max_size += grow_size; |
10411 | 204 buf = static_cast<char *> (gnulib::realloc (buf, max_size)); |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
205 bufptr = buf + tmp; |
2926 | 206 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
207 if (*(bufptr-1) == '\n') |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
208 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
209 *bufptr = '\0'; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
210 retval = buf; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
211 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
212 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
213 else if (bufptr[len-1] != '\n') |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
214 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
215 bufptr[len++] = '\n'; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
216 bufptr[len] = '\0'; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
217 retval = buf; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
218 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
219 else |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
220 retval = buf; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
221 } |
2926 | 222 else |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
223 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
224 if (len == 0) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
225 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
226 eof = true; |
4527 | 227 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
228 free (buf); |
2926 | 229 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
230 buf = 0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
231 } |
2926 | 232 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
233 break; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
234 } |
2926 | 235 } |
236 while (retval.empty ()); | |
237 | |
238 if (buf) | |
239 free (buf); | |
240 | |
10142
829e69ec3110
make OCTAVE_QUIT a function
Jaroslav Hajek <highegg@gmail.com>
parents:
10068
diff
changeset
|
241 octave_quit (); |
10068
ca93f583573d
handle interrupts octave_fgets
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
242 |
2926 | 243 return retval; |
244 } | |
245 | |
3970 | 246 std::string |
247 octave_fgetl (FILE *f) | |
248 { | |
4527 | 249 bool eof; |
250 return octave_fgetl (f, eof); | |
251 } | |
252 | |
253 std::string | |
254 octave_fgetl (FILE *f, bool& eof) | |
255 { | |
256 std::string retval = octave_fgets (f, eof); | |
3970 | 257 |
258 size_t len = retval.length (); | |
259 | |
260 if (retval[len-1] == '\n') | |
261 retval.resize (len-1); | |
262 | |
263 return retval; | |
264 } | |
265 | |
4130 | 266 static inline double |
6194 | 267 read_inf_nan_na (std::istream& is, char c, char sign = '+') |
4130 | 268 { |
269 double d = 0.0; | |
270 | |
271 switch (c) | |
272 { | |
273 case 'i': case 'I': | |
274 { | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
275 c = is.get (); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
276 if (c == 'n' || c == 'N') |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
277 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
278 c = is.get (); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
279 if (c == 'f' || c == 'F') |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
280 d = sign == '-' ? -octave_Inf : octave_Inf; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
281 else |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
282 is.putback (c); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
283 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
284 else |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
285 is.putback (c); |
4130 | 286 } |
287 break; | |
288 | |
289 case 'n': case 'N': | |
290 { | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
291 c = is.get (); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
292 if (c == 'a' || c == 'A') |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
293 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
294 c = is.get (); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
295 if (c == 'n' || c == 'N') |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
296 d = octave_NaN; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
297 else |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
298 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
299 is.putback (c); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
300 d = octave_NA; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
301 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
302 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
303 else |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
304 is.putback (c); |
4130 | 305 } |
306 break; | |
307 | |
308 default: | |
309 abort (); | |
310 } | |
311 | |
312 return d; | |
313 } | |
314 | |
9469
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
315 template <> |
4130 | 316 double |
9469
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
317 octave_read_value (std::istream& is) |
4130 | 318 { |
319 double d = 0.0; | |
320 | |
6907 | 321 char c1 = ' '; |
4130 | 322 |
6907 | 323 while (isspace (c1)) |
324 c1 = is.get (); | |
325 | |
6194 | 326 switch (c1) |
4130 | 327 { |
6194 | 328 case '-': |
329 { | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
330 char c2 = 0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
331 c2 = is.get (); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
332 if (c2 == 'i' || c2 == 'I') |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
333 d = read_inf_nan_na (is, c2, c1); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
334 else |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
335 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
336 is.putback (c2); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
337 is.putback (c1); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
338 is >> d; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
339 } |
6194 | 340 } |
341 break; | |
342 | |
343 case '+': | |
344 { | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
345 char c2 = 0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
346 c2 = is.get (); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
347 if (c2 == 'i' || c2 == 'I') |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
348 d = read_inf_nan_na (is, c2, c1); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
349 else |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
350 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
351 is.putback (c2); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
352 is.putback (c1); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
353 is >> d; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
354 } |
6194 | 355 } |
356 break; | |
357 | |
4130 | 358 case 'i': case 'I': |
359 case 'n': case 'N': | |
6194 | 360 d = read_inf_nan_na (is, c1); |
4130 | 361 break; |
362 | |
363 default: | |
6194 | 364 is.putback (c1); |
4130 | 365 is >> d; |
366 } | |
367 | |
368 return d; | |
369 } | |
370 | |
9469
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
371 template <> |
4130 | 372 Complex |
9469
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
373 octave_read_value (std::istream& is) |
4130 | 374 { |
375 double re = 0.0, im = 0.0; | |
376 | |
377 Complex cx = 0.0; | |
378 | |
6907 | 379 char ch = ' '; |
4130 | 380 |
6907 | 381 while (isspace (ch)) |
382 ch = is.get (); | |
4130 | 383 |
384 if (ch == '(') | |
385 { | |
9469
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
386 re = octave_read_value<double> (is); |
6897 | 387 ch = is.get (); |
4130 | 388 |
389 if (ch == ',') | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
390 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
391 im = octave_read_value<double> (is); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
392 ch = is.get (); |
4130 | 393 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
394 if (ch == ')') |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
395 cx = Complex (re, im); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
396 else |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
397 is.setstate (std::ios::failbit); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
398 } |
4130 | 399 else if (ch == ')') |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
400 cx = re; |
4130 | 401 else |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
402 is.setstate (std::ios::failbit); |
4130 | 403 } |
404 else | |
405 { | |
406 is.putback (ch); | |
9469
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
407 cx = octave_read_value<double> (is); |
4130 | 408 } |
409 | |
410 return cx; | |
411 | |
412 } | |
413 | |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
414 static inline float |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
415 read_float_inf_nan_na (std::istream& is, char c, char sign = '+') |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
416 { |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
417 float d = 0.0; |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
418 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
419 switch (c) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
420 { |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
421 case 'i': case 'I': |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
422 { |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
423 c = is.get (); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
424 if (c == 'n' || c == 'N') |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
425 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
426 c = is.get (); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
427 if (c == 'f' || c == 'F') |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
428 d = sign == '-' ? -octave_Inf : octave_Inf; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
429 else |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
430 is.putback (c); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
431 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
432 else |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
433 is.putback (c); |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
434 } |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
435 break; |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
436 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
437 case 'n': case 'N': |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
438 { |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
439 c = is.get (); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
440 if (c == 'a' || c == 'A') |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
441 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
442 c = is.get (); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
443 if (c == 'n' || c == 'N') |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
444 d = octave_NaN; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
445 else |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
446 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
447 is.putback (c); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
448 d = octave_NA; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
449 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
450 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
451 else |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
452 is.putback (c); |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
453 } |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
454 break; |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
455 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
456 default: |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
457 abort (); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
458 } |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
459 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
460 return d; |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
461 } |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
462 |
9469
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
463 template <> |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
464 float |
9469
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
465 octave_read_value (std::istream& is) |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
466 { |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
467 float d = 0.0; |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
468 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
469 char c1 = ' '; |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
470 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
471 while (isspace (c1)) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
472 c1 = is.get (); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
473 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
474 switch (c1) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
475 { |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
476 case '-': |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
477 { |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
478 char c2 = 0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
479 c2 = is.get (); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
480 if (c2 == 'i' || c2 == 'I') |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
481 d = read_float_inf_nan_na (is, c2, c1); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
482 else |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
483 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
484 is.putback (c2); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
485 is.putback (c1); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
486 is >> d; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
487 } |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
488 } |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
489 break; |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
490 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
491 case '+': |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
492 { |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
493 char c2 = 0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
494 c2 = is.get (); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
495 if (c2 == 'i' || c2 == 'I') |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
496 d = read_float_inf_nan_na (is, c2, c1); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
497 else |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
498 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
499 is.putback (c2); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
500 is.putback (c1); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
501 is >> d; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
502 } |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
503 } |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
504 break; |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
505 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
506 case 'i': case 'I': |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
507 case 'n': case 'N': |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
508 d = read_float_inf_nan_na (is, c1); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
509 break; |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
510 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
511 default: |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
512 is.putback (c1); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
513 is >> d; |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
514 } |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
515 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
516 return d; |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
517 } |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
518 |
9469
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
519 template <> |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
520 FloatComplex |
9469
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
521 octave_read_value (std::istream& is) |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
522 { |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
523 float re = 0.0, im = 0.0; |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
524 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
525 FloatComplex cx = 0.0; |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
526 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
527 char ch = ' '; |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
528 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
529 while (isspace (ch)) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
530 ch = is.get (); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
531 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
532 if (ch == '(') |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
533 { |
9469
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
534 re = octave_read_value<float> (is); |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
535 ch = is.get (); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
536 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
537 if (ch == ',') |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
538 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
539 im = octave_read_value<float> (is); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
540 ch = is.get (); |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
541 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
542 if (ch == ')') |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
543 cx = FloatComplex (re, im); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
544 else |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
545 is.setstate (std::ios::failbit); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
546 } |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
547 else if (ch == ')') |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
548 cx = re; |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
549 else |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
550 is.setstate (std::ios::failbit); |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
551 } |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
552 else |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
553 { |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
554 is.putback (ch); |
9469
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
555 cx = octave_read_value<float> (is); |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
556 } |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
557 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
558 return cx; |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
559 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
560 } |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
561 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
562 void |
9469
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
563 octave_write_double (std::ostream& os, double d) |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
564 { |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
565 if (lo_ieee_is_NA (d)) |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
566 os << "NA"; |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
567 else if (lo_ieee_isnan (d)) |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
568 os << "NaN"; |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
569 else if (lo_ieee_isinf (d)) |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
570 os << (d < 0 ? "-Inf" : "Inf"); |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
571 else |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
572 os << d; |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
573 } |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
574 |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
575 void |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
576 octave_write_complex (std::ostream& os, const Complex& c) |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
577 { |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
578 os << "("; |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
579 octave_write_double (os, real (c)); |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
580 os << ","; |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
581 octave_write_double (os, imag (c)); |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
582 os << ")"; |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
583 } |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
584 |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
585 void |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
586 octave_write_float (std::ostream& os, float d) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
587 { |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
588 if (lo_ieee_is_NA (d)) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
589 os << "NA"; |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
590 else if (lo_ieee_isnan (d)) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
591 os << "NaN"; |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
592 else if (lo_ieee_isinf (d)) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
593 os << (d < 0 ? "-Inf" : "Inf"); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
594 else |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
595 os << d; |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
596 } |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
597 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
598 void |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
599 octave_write_float_complex (std::ostream& os, const FloatComplex& c) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
600 { |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
601 os << "("; |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
602 octave_write_float (os, real (c)); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
603 os << ","; |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
604 octave_write_float (os, imag (c)); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
605 os << ")"; |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
606 } |