annotate libinterp/corefcn/str2double.cc @ 17281:bc924baa2c4e

doc: Add new @qcode macro for code samples which are quoted. Macro handles options ("on") or properties ("position") more elegantly than @code{"text"}. * doc/interpreter/macros.texi: Add new @qcode macro. * doc/interpreter/tips.txi: Add documentation about @qcode macro. * doc/interpreter/basics.txi, doc/interpreter/container.txi, doc/interpreter/emacs.txi, doc/interpreter/errors.txi, doc/interpreter/eval.txi, doc/interpreter/expr.txi, doc/interpreter/external.txi, doc/interpreter/func.txi, doc/interpreter/grammar.txi, doc/interpreter/image.txi, doc/interpreter/install.txi, doc/interpreter/interp.txi, doc/interpreter/io.txi, doc/interpreter/matrix.txi, doc/interpreter/numbers.txi, doc/interpreter/oop.txi, doc/interpreter/package.txi, doc/interpreter/plot.txi, doc/interpreter/quad.txi, doc/interpreter/sparse.txi, doc/interpreter/strings.txi, doc/interpreter/system.txi, doc/interpreter/vectorize.txi, libinterp/corefcn/balance.cc, libinterp/corefcn/bitfcns.cc, libinterp/corefcn/cellfun.cc, libinterp/corefcn/conv2.cc, libinterp/corefcn/data.cc, libinterp/corefcn/debug.cc, libinterp/corefcn/defaults.cc, libinterp/corefcn/dirfns.cc, libinterp/corefcn/dlmread.cc, libinterp/corefcn/error.cc, libinterp/corefcn/file-io.cc, libinterp/corefcn/find.cc, libinterp/corefcn/gammainc.cc, libinterp/corefcn/graphics.cc, libinterp/corefcn/help.cc, libinterp/corefcn/hex2num.cc, libinterp/corefcn/input.cc, libinterp/corefcn/load-path.cc, libinterp/corefcn/load-save.cc, libinterp/corefcn/ls-oct-ascii.cc, libinterp/corefcn/lu.cc, libinterp/corefcn/luinc.cc, libinterp/corefcn/matrix_type.cc, libinterp/corefcn/oct-hist.cc, libinterp/corefcn/pager.cc, libinterp/corefcn/pr-output.cc, libinterp/corefcn/pt-jit.cc, libinterp/corefcn/qz.cc, libinterp/corefcn/rand.cc, libinterp/corefcn/regexp.cc, libinterp/corefcn/schur.cc, libinterp/corefcn/sighandlers.cc, libinterp/corefcn/sparse.cc, libinterp/corefcn/spparms.cc, libinterp/corefcn/str2double.cc, libinterp/corefcn/svd.cc, libinterp/corefcn/symtab.cc, libinterp/corefcn/syscalls.cc, libinterp/corefcn/toplev.cc, libinterp/corefcn/tril.cc, libinterp/corefcn/typecast.cc, libinterp/corefcn/utils.cc, libinterp/corefcn/variables.cc, libinterp/dldfcn/__init_fltk__.cc, libinterp/dldfcn/chol.cc, libinterp/dldfcn/colamd.cc, libinterp/dldfcn/fftw.cc, libinterp/dldfcn/qr.cc, libinterp/dldfcn/symbfact.cc, libinterp/octave-value/ov-base.cc, libinterp/octave-value/ov-fcn-handle.cc, libinterp/octave-value/ov-fcn-inline.cc, libinterp/octave-value/ov-java.cc, libinterp/octave-value/ov-range.cc, libinterp/octave-value/ov-struct.cc, libinterp/octave-value/ov-usr-fcn.cc, libinterp/parse-tree/oct-parse.in.yy, libinterp/parse-tree/pt-binop.cc, libinterp/parse-tree/pt-eval.cc, libinterp/parse-tree/pt-mat.cc, scripts/@ftp/ftp.m, scripts/deprecated/java_convert_matrix.m, scripts/deprecated/java_debug.m, scripts/deprecated/java_unsigned_conversion.m, scripts/deprecated/shell_cmd.m, scripts/general/dblquad.m, scripts/general/display.m, scripts/general/genvarname.m, scripts/general/idivide.m, scripts/general/interp1.m, scripts/general/interp2.m, scripts/general/interp3.m, scripts/general/interpn.m, scripts/general/isa.m, scripts/general/profexplore.m, scripts/general/profile.m, scripts/general/quadgk.m, scripts/general/randi.m, scripts/general/structfun.m, scripts/general/subsindex.m, scripts/general/triplequad.m, scripts/geometry/griddata.m, scripts/geometry/griddata3.m, scripts/geometry/griddatan.m, scripts/geometry/voronoi.m, scripts/help/help.m, scripts/help/lookfor.m, scripts/image/cmpermute.m, scripts/image/colormap.m, scripts/image/image.m, scripts/image/imagesc.m, scripts/image/imfinfo.m, scripts/image/imformats.m, scripts/image/imread.m, scripts/image/imshow.m, scripts/image/imwrite.m, scripts/image/ind2gray.m, scripts/image/lines.m, scripts/image/rgb2ind.m, scripts/image/spinmap.m, scripts/io/dlmwrite.m, scripts/io/strread.m, scripts/io/textread.m, scripts/io/textscan.m, scripts/java/javaclasspath.m, scripts/java/usejava.m, scripts/miscellaneous/bzip2.m, scripts/miscellaneous/computer.m, scripts/miscellaneous/copyfile.m, scripts/miscellaneous/debug.m, scripts/miscellaneous/dos.m, scripts/miscellaneous/edit.m, scripts/miscellaneous/gzip.m, scripts/miscellaneous/license.m, scripts/miscellaneous/mkoctfile.m, scripts/miscellaneous/movefile.m, scripts/miscellaneous/parseparams.m, scripts/miscellaneous/unix.m, scripts/optimization/fminbnd.m, scripts/optimization/fminsearch.m, scripts/optimization/fminunc.m, scripts/optimization/fsolve.m, scripts/optimization/fzero.m, scripts/optimization/glpk.m, scripts/optimization/lsqnonneg.m, scripts/optimization/optimset.m, scripts/optimization/pqpnonneg.m, scripts/pkg/pkg.m, scripts/plot/allchild.m, scripts/plot/ancestor.m, scripts/plot/area.m, scripts/plot/axis.m, scripts/plot/bar.m, scripts/plot/barh.m, scripts/plot/box.m, scripts/plot/caxis.m, scripts/plot/cla.m, scripts/plot/clabel.m, scripts/plot/clf.m, scripts/plot/close.m, scripts/plot/colorbar.m, scripts/plot/daspect.m, scripts/plot/ezmesh.m, scripts/plot/ezmeshc.m, scripts/plot/ezsurf.m, scripts/plot/ezsurfc.m, scripts/plot/findall.m, scripts/plot/findobj.m, scripts/plot/gcbo.m, scripts/plot/gcf.m, scripts/plot/gco.m, scripts/plot/grid.m, scripts/plot/guihandles.m, scripts/plot/hdl2struct.m, scripts/plot/hidden.m, scripts/plot/hold.m, scripts/plot/isonormals.m, scripts/plot/isosurface.m, scripts/plot/legend.m, scripts/plot/mesh.m, scripts/plot/meshc.m, scripts/plot/meshz.m, scripts/plot/newplot.m, scripts/plot/orient.m, scripts/plot/pareto.m, scripts/plot/patch.m, scripts/plot/pbaspect.m, scripts/plot/pcolor.m, scripts/plot/plot.m, scripts/plot/print.m, scripts/plot/private/__add_default_menu__.m, scripts/plot/quiver.m, scripts/plot/quiver3.m, scripts/plot/refreshdata.m, scripts/plot/saveas.m, scripts/plot/scatter.m, scripts/plot/scatter3.m, scripts/plot/shading.m, scripts/plot/shrinkfaces.m, scripts/plot/slice.m, scripts/plot/stem.m, scripts/plot/stem3.m, scripts/plot/struct2hdl.m, scripts/plot/subplot.m, scripts/plot/surf.m, scripts/plot/surfc.m, scripts/plot/surfl.m, scripts/plot/tetramesh.m, scripts/plot/uigetfile.m, scripts/plot/uimenu.m, scripts/plot/uiputfile.m, scripts/plot/waterfall.m, scripts/plot/whitebg.m, scripts/plot/xlim.m, scripts/plot/ylim.m, scripts/plot/zlim.m, scripts/polynomial/conv.m, scripts/polynomial/polyout.m, scripts/polynomial/splinefit.m, scripts/set/ismember.m, scripts/set/powerset.m, scripts/set/setdiff.m, scripts/set/union.m, scripts/set/unique.m, scripts/signal/detrend.m, scripts/signal/filter2.m, scripts/signal/freqz.m, scripts/signal/periodogram.m, scripts/signal/spectral_adf.m, scripts/signal/spectral_xdf.m, scripts/sparse/eigs.m, scripts/sparse/svds.m, scripts/specfun/legendre.m, scripts/special-matrix/gallery.m, scripts/statistics/base/mean.m, scripts/statistics/base/moment.m, scripts/statistics/tests/cor_test.m, scripts/statistics/tests/kolmogorov_smirnov_test.m, scripts/statistics/tests/kolmogorov_smirnov_test_2.m, scripts/statistics/tests/kruskal_wallis_test.m, scripts/statistics/tests/prop_test_2.m, scripts/statistics/tests/sign_test.m, scripts/statistics/tests/t_test.m, scripts/statistics/tests/t_test_2.m, scripts/statistics/tests/t_test_regression.m, scripts/statistics/tests/u_test.m, scripts/statistics/tests/var_test.m, scripts/statistics/tests/welch_test.m, scripts/statistics/tests/wilcoxon_test.m, scripts/statistics/tests/z_test.m, scripts/statistics/tests/z_test_2.m, scripts/strings/base2dec.m, scripts/strings/index.m, scripts/strings/isstrprop.m, scripts/strings/mat2str.m, scripts/strings/regexptranslate.m, scripts/strings/rindex.m, scripts/strings/str2num.m, scripts/strings/strcat.m, scripts/strings/strjust.m, scripts/strings/strmatch.m, scripts/strings/validatestring.m, scripts/testfun/demo.m, scripts/testfun/example.m, scripts/testfun/test.m, scripts/time/addtodate.m, scripts/time/asctime.m, scripts/time/datestr.m, scripts/time/datetick.m, scripts/time/weekday.m, scripts/ui/errordlg.m, scripts/ui/helpdlg.m, scripts/ui/inputdlg.m, scripts/ui/listdlg.m, scripts/ui/msgbox.m, scripts/ui/questdlg.m, scripts/ui/warndlg.m: Use new @qcode macro.
author Rik <rik@octave.org>
date Mon, 19 Aug 2013 20:46:38 -0700
parents 2fc554ffbc28
children b91f29f021a3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
10356
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
1 /*
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
2
14138
72c96de7a403 maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents: 14091
diff changeset
3 Copyright (C) 2010-2012 Jaroslav Hajek
10356
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
4 Copyright (C) 2010 VZLU Prague
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
5
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
6 This file is part of Octave.
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
7
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
8 Octave is free software; you can redistribute it and/or modify it
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
9 under the terms of the GNU General Public License as published by the
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
10 Free Software Foundation; either version 3 of the License, or (at your
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
11 option) any later version.
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
12
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
13 Octave is distributed in the hope that it will be useful, but WITHOUT
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
16 for more details.
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
17
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
18 You should have received a copy of the GNU General Public License
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
19 along with Octave; see the file COPYING. If not, see
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
20 <http://www.gnu.org/licenses/>.
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
21
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
22 */
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
23
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
24 #ifdef HAVE_CONFIG_H
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
25 #include <config.h>
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
26 #endif
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
27
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
28 #include <string>
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
29 #include <cctype>
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
30 #include <sstream>
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
31 #include <algorithm>
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
32
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
33 #include "lo-ieee.h"
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
34
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
35 #include "Cell.h"
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
36 #include "ov.h"
15039
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14854
diff changeset
37 #include "defun.h"
10356
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
38 #include "gripes.h"
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
39 #include "utils.h"
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
40
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11553
diff changeset
41 static inline bool
10356
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
42 is_imag_unit (int c)
10357
7658cd4bdcf2 handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents: 10356
diff changeset
43 { return c == 'i' || c == 'j'; }
7658cd4bdcf2 handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents: 10356
diff changeset
44
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
14702
a08f6e17336e str2double.cc: Case-insensitive detection of 'Inf' (Bug #36536).
Rik <octave@nomad.inbox5.com>
parents: 14501
diff changeset
57 if (std::toupper (c) == 'I')
10357
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 ();
14702
a08f6e17336e str2double.cc: Case-insensitive detection of 'Inf' (Bug #36536).
Rik <octave@nomad.inbox5.com>
parents: 14501
diff changeset
62 if (std::tolower (c1) == 'n' && std::tolower (c2) == 'f')
10357
7658cd4bdcf2 handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents: 10356
diff changeset
63 {
7658cd4bdcf2 handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents: 10356
diff changeset
64 num = octave_Inf;
14702
a08f6e17336e str2double.cc: Case-insensitive detection of 'Inf' (Bug #36536).
Rik <octave@nomad.inbox5.com>
parents: 14501
diff changeset
65 is.peek (); // May set EOF bit.
10357
7658cd4bdcf2 handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents: 10356
diff changeset
66 }
7658cd4bdcf2 handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents: 10356
diff changeset
67 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;
14091
c3ea07298af4 str2double: correctly handle things like ["1"; "2 3"; "4i"]
John W. Eaton <jwe@octave.org>
parents: 14077
diff changeset
78 is.peek (); // May set EOF bit.
10357
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;
14091
c3ea07298af4 str2double: correctly handle things like ["1"; "2 3"; "4i"]
John W. Eaton <jwe@octave.org>
parents: 14077
diff changeset
86 is.peek (); // May set EOF bit.
11414
5e6ef42a2445 Overhaul str2double function fixing bug and docstring and adding 'NA' processing.
Rik <octave@nomad.inbox5.com>
parents: 10846
diff changeset
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
14702
a08f6e17336e str2double.cc: Case-insensitive detection of 'Inf' (Bug #36536).
Rik <octave@nomad.inbox5.com>
parents: 14501
diff changeset
130 // Imaginary number (i*num or just i), or maybe 'inf'.
a08f6e17336e str2double.cc: Case-insensitive detection of 'Inf' (Bug #36536).
Rik <octave@nomad.inbox5.com>
parents: 14501
diff changeset
131 if (c == 'i')
10356
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
132 {
14702
a08f6e17336e str2double.cc: Case-insensitive detection of 'Inf' (Bug #36536).
Rik <octave@nomad.inbox5.com>
parents: 14501
diff changeset
133 // possible infinity.
14091
c3ea07298af4 str2double: correctly handle things like ["1"; "2 3"; "4i"]
John W. Eaton <jwe@octave.org>
parents: 14077
diff changeset
134 is.get ();
c3ea07298af4 str2double: correctly handle things like ["1"; "2 3"; "4i"]
John W. Eaton <jwe@octave.org>
parents: 14077
diff changeset
135 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
136
14702
a08f6e17336e str2double.cc: Case-insensitive detection of 'Inf' (Bug #36536).
Rik <octave@nomad.inbox5.com>
parents: 14501
diff changeset
137 if (is.eof ())
a08f6e17336e str2double.cc: Case-insensitive detection of 'Inf' (Bug #36536).
Rik <octave@nomad.inbox5.com>
parents: 14501
diff changeset
138 {
a08f6e17336e str2double.cc: Case-insensitive detection of 'Inf' (Bug #36536).
Rik <octave@nomad.inbox5.com>
parents: 14501
diff changeset
139 // just 'i' and string is finished. Return immediately.
a08f6e17336e str2double.cc: Case-insensitive detection of 'Inf' (Bug #36536).
Rik <octave@nomad.inbox5.com>
parents: 14501
diff changeset
140 imag = true;
a08f6e17336e str2double.cc: Case-insensitive detection of 'Inf' (Bug #36536).
Rik <octave@nomad.inbox5.com>
parents: 14501
diff changeset
141 num = 1.0;
a08f6e17336e str2double.cc: Case-insensitive detection of 'Inf' (Bug #36536).
Rik <octave@nomad.inbox5.com>
parents: 14501
diff changeset
142 if (negative)
a08f6e17336e str2double.cc: Case-insensitive detection of 'Inf' (Bug #36536).
Rik <octave@nomad.inbox5.com>
parents: 14501
diff changeset
143 num = -num;
a08f6e17336e str2double.cc: Case-insensitive detection of 'Inf' (Bug #36536).
Rik <octave@nomad.inbox5.com>
parents: 14501
diff changeset
144 return is;
a08f6e17336e str2double.cc: Case-insensitive detection of 'Inf' (Bug #36536).
Rik <octave@nomad.inbox5.com>
parents: 14501
diff changeset
145 }
a08f6e17336e str2double.cc: Case-insensitive detection of 'Inf' (Bug #36536).
Rik <octave@nomad.inbox5.com>
parents: 14501
diff changeset
146 else
a08f6e17336e str2double.cc: Case-insensitive detection of 'Inf' (Bug #36536).
Rik <octave@nomad.inbox5.com>
parents: 14501
diff changeset
147 {
a08f6e17336e str2double.cc: Case-insensitive detection of 'Inf' (Bug #36536).
Rik <octave@nomad.inbox5.com>
parents: 14501
diff changeset
148 if (std::tolower (c) != 'n')
a08f6e17336e str2double.cc: Case-insensitive detection of 'Inf' (Bug #36536).
Rik <octave@nomad.inbox5.com>
parents: 14501
diff changeset
149 imag = true;
a08f6e17336e str2double.cc: Case-insensitive detection of 'Inf' (Bug #36536).
Rik <octave@nomad.inbox5.com>
parents: 14501
diff changeset
150 is.unget ();
a08f6e17336e str2double.cc: Case-insensitive detection of 'Inf' (Bug #36536).
Rik <octave@nomad.inbox5.com>
parents: 14501
diff changeset
151 }
a08f6e17336e str2double.cc: Case-insensitive detection of 'Inf' (Bug #36536).
Rik <octave@nomad.inbox5.com>
parents: 14501
diff changeset
152 }
a08f6e17336e str2double.cc: Case-insensitive detection of 'Inf' (Bug #36536).
Rik <octave@nomad.inbox5.com>
parents: 14501
diff changeset
153 else if (c == 'j')
a08f6e17336e str2double.cc: Case-insensitive detection of 'Inf' (Bug #36536).
Rik <octave@nomad.inbox5.com>
parents: 14501
diff changeset
154 imag = true;
a08f6e17336e str2double.cc: Case-insensitive detection of 'Inf' (Bug #36536).
Rik <octave@nomad.inbox5.com>
parents: 14501
diff changeset
155
a08f6e17336e str2double.cc: Case-insensitive detection of 'Inf' (Bug #36536).
Rik <octave@nomad.inbox5.com>
parents: 14501
diff changeset
156 // It's i*num or just i
a08f6e17336e str2double.cc: Case-insensitive detection of 'Inf' (Bug #36536).
Rik <octave@nomad.inbox5.com>
parents: 14501
diff changeset
157 if (imag)
a08f6e17336e str2double.cc: Case-insensitive detection of 'Inf' (Bug #36536).
Rik <octave@nomad.inbox5.com>
parents: 14501
diff changeset
158 {
a08f6e17336e str2double.cc: Case-insensitive detection of 'Inf' (Bug #36536).
Rik <octave@nomad.inbox5.com>
parents: 14501
diff changeset
159 is.get ();
a08f6e17336e str2double.cc: Case-insensitive detection of 'Inf' (Bug #36536).
Rik <octave@nomad.inbox5.com>
parents: 14501
diff changeset
160 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
161 // Skip spaces after imaginary unit.
14091
c3ea07298af4 str2double: correctly handle things like ["1"; "2 3"; "4i"]
John W. Eaton <jwe@octave.org>
parents: 14077
diff changeset
162 while (isspace (c))
14077
b6eeeb67fa3f str2double: return NaN for things like "1 2 3 4" (bug #34713).
John W. Eaton <jwe@octave.org>
parents: 13141
diff changeset
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 ();
14091
c3ea07298af4 str2double: correctly handle things like ["1"; "2 3"; "4i"]
John W. Eaton <jwe@octave.org>
parents: 14077
diff changeset
165 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
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
14091
c3ea07298af4 str2double: correctly handle things like ["1"; "2 3"; "4i"]
John W. Eaton <jwe@octave.org>
parents: 14077
diff changeset
168 if (c == '*')
10356
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 // Multiplier follows, we extract it as a number.
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
171 is.get ();
10357
7658cd4bdcf2 handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents: 10356
diff changeset
172 single_num (is, num);
14091
c3ea07298af4 str2double: correctly handle things like ["1"; "2 3"; "4i"]
John W. Eaton <jwe@octave.org>
parents: 14077
diff changeset
173 if (is.good ())
c3ea07298af4 str2double: correctly handle things like ["1"; "2 3"; "4i"]
John W. Eaton <jwe@octave.org>
parents: 14077
diff changeset
174 c = is.peek ();
10356
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
175 }
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
176 else
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
177 num = 1.0;
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
178 }
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
179 else
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
180 {
11414
5e6ef42a2445 Overhaul str2double function fixing bug and docstring and adding 'NA' processing.
Rik <octave@nomad.inbox5.com>
parents: 10846
diff changeset
181 // It's num, num*i, or numi.
10357
7658cd4bdcf2 handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents: 10356
diff changeset
182 single_num (is, num);
10356
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
183 if (is.good ())
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
184 {
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
185 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
186
b6eeeb67fa3f str2double: return NaN for things like "1 2 3 4" (bug #34713).
John W. Eaton <jwe@octave.org>
parents: 13141
diff changeset
187 // 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
188 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
189 {
b6eeeb67fa3f str2double: return NaN for things like "1 2 3 4" (bug #34713).
John W. Eaton <jwe@octave.org>
parents: 13141
diff changeset
190 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
191 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
192 }
b6eeeb67fa3f str2double: return NaN for things like "1 2 3 4" (bug #34713).
John W. Eaton <jwe@octave.org>
parents: 13141
diff changeset
193
10356
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
194 if (c == '*')
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 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
197 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
198
b6eeeb67fa3f str2double: return NaN for things like "1 2 3 4" (bug #34713).
John W. Eaton <jwe@octave.org>
parents: 13141
diff changeset
199 // 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
200 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
201 {
b6eeeb67fa3f str2double: return NaN for things like "1 2 3 4" (bug #34713).
John W. Eaton <jwe@octave.org>
parents: 13141
diff changeset
202 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
203 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
204 }
b6eeeb67fa3f str2double: return NaN for things like "1 2 3 4" (bug #34713).
John W. Eaton <jwe@octave.org>
parents: 13141
diff changeset
205
10356
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
206 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
207 {
5e6ef42a2445 Overhaul str2double function fixing bug and docstring and adding 'NA' processing.
Rik <octave@nomad.inbox5.com>
parents: 10846
diff changeset
208 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
209 is.get ();
14091
c3ea07298af4 str2double: correctly handle things like ["1"; "2 3"; "4i"]
John W. Eaton <jwe@octave.org>
parents: 14077
diff changeset
210 c = is.peek ();
11414
5e6ef42a2445 Overhaul str2double function fixing bug and docstring and adding 'NA' processing.
Rik <octave@nomad.inbox5.com>
parents: 10846
diff changeset
211 }
10356
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
212 else
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
213 is.setstate (std::ios::failbit); // indicate that read has failed.
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
214 }
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
215 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
216 {
5e6ef42a2445 Overhaul str2double function fixing bug and docstring and adding 'NA' processing.
Rik <octave@nomad.inbox5.com>
parents: 10846
diff changeset
217 imag = true;
5e6ef42a2445 Overhaul str2double function fixing bug and docstring and adding 'NA' processing.
Rik <octave@nomad.inbox5.com>
parents: 10846
diff changeset
218 is.get ();
14091
c3ea07298af4 str2double: correctly handle things like ["1"; "2 3"; "4i"]
John W. Eaton <jwe@octave.org>
parents: 14077
diff changeset
219 c = is.peek ();
11414
5e6ef42a2445 Overhaul str2double function fixing bug and docstring and adding 'NA' processing.
Rik <octave@nomad.inbox5.com>
parents: 10846
diff changeset
220 }
10356
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
221 }
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
222 }
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
223
14091
c3ea07298af4 str2double: correctly handle things like ["1"; "2 3"; "4i"]
John W. Eaton <jwe@octave.org>
parents: 14077
diff changeset
224 if (is.good ())
c3ea07298af4 str2double: correctly handle things like ["1"; "2 3"; "4i"]
John W. Eaton <jwe@octave.org>
parents: 14077
diff changeset
225 {
c3ea07298af4 str2double: correctly handle things like ["1"; "2 3"; "4i"]
John W. Eaton <jwe@octave.org>
parents: 14077
diff changeset
226 // Skip trailing spaces.
c3ea07298af4 str2double: correctly handle things like ["1"; "2 3"; "4i"]
John W. Eaton <jwe@octave.org>
parents: 14077
diff changeset
227 while (isspace (c))
c3ea07298af4 str2double: correctly handle things like ["1"; "2 3"; "4i"]
John W. Eaton <jwe@octave.org>
parents: 14077
diff changeset
228 {
c3ea07298af4 str2double: correctly handle things like ["1"; "2 3"; "4i"]
John W. Eaton <jwe@octave.org>
parents: 14077
diff changeset
229 is.get ();
c3ea07298af4 str2double: correctly handle things like ["1"; "2 3"; "4i"]
John W. Eaton <jwe@octave.org>
parents: 14077
diff changeset
230 c = is.peek ();
c3ea07298af4 str2double: correctly handle things like ["1"; "2 3"; "4i"]
John W. Eaton <jwe@octave.org>
parents: 14077
diff changeset
231 }
c3ea07298af4 str2double: correctly handle things like ["1"; "2 3"; "4i"]
John W. Eaton <jwe@octave.org>
parents: 14077
diff changeset
232 }
c3ea07298af4 str2double: correctly handle things like ["1"; "2 3"; "4i"]
John W. Eaton <jwe@octave.org>
parents: 14077
diff changeset
233
10356
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
234 if (negative)
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
235 num = -num;
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 return is;
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
10379
f578e6468d0c use C++-0x code in str2double
Jaroslav Hajek <highegg@gmail.com>
parents: 10378
diff changeset
240 static inline void
f578e6468d0c use C++-0x code in str2double
Jaroslav Hajek <highegg@gmail.com>
parents: 10378
diff changeset
241 set_component (Complex& c, double num, bool imag)
f578e6468d0c use C++-0x code in str2double
Jaroslav Hajek <highegg@gmail.com>
parents: 10378
diff changeset
242 {
10380
60acc47c203f configure checks for complex element setter/reference accessor methods
John W. Eaton <jwe@octave.org>
parents: 10379
diff changeset
243 #if defined (HAVE_CXX_COMPLEX_SETTERS)
10379
f578e6468d0c use C++-0x code in str2double
Jaroslav Hajek <highegg@gmail.com>
parents: 10378
diff changeset
244 if (imag)
10380
60acc47c203f configure checks for complex element setter/reference accessor methods
John W. Eaton <jwe@octave.org>
parents: 10379
diff changeset
245 c.imag (num);
10379
f578e6468d0c use C++-0x code in str2double
Jaroslav Hajek <highegg@gmail.com>
parents: 10378
diff changeset
246 else
10380
60acc47c203f configure checks for complex element setter/reference accessor methods
John W. Eaton <jwe@octave.org>
parents: 10379
diff changeset
247 c.real (num);
60acc47c203f configure checks for complex element setter/reference accessor methods
John W. Eaton <jwe@octave.org>
parents: 10379
diff changeset
248 #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
249 if (imag)
60acc47c203f configure checks for complex element setter/reference accessor methods
John W. Eaton <jwe@octave.org>
parents: 10379
diff changeset
250 c.imag () = num;
60acc47c203f configure checks for complex element setter/reference accessor methods
John W. Eaton <jwe@octave.org>
parents: 10379
diff changeset
251 else
60acc47c203f configure checks for complex element setter/reference accessor methods
John W. Eaton <jwe@octave.org>
parents: 10379
diff changeset
252 c.real () = num;
10379
f578e6468d0c use C++-0x code in str2double
Jaroslav Hajek <highegg@gmail.com>
parents: 10378
diff changeset
253 #else
f578e6468d0c use C++-0x code in str2double
Jaroslav Hajek <highegg@gmail.com>
parents: 10378
diff changeset
254 if (imag)
f578e6468d0c use C++-0x code in str2double
Jaroslav Hajek <highegg@gmail.com>
parents: 10378
diff changeset
255 c = Complex (c.real (), num);
f578e6468d0c use C++-0x code in str2double
Jaroslav Hajek <highegg@gmail.com>
parents: 10378
diff changeset
256 else
f578e6468d0c use C++-0x code in str2double
Jaroslav Hajek <highegg@gmail.com>
parents: 10378
diff changeset
257 c = Complex (num, c.imag ());
f578e6468d0c use C++-0x code in str2double
Jaroslav Hajek <highegg@gmail.com>
parents: 10378
diff changeset
258 #endif
f578e6468d0c use C++-0x code in str2double
Jaroslav Hajek <highegg@gmail.com>
parents: 10378
diff changeset
259 }
f578e6468d0c use C++-0x code in str2double
Jaroslav Hajek <highegg@gmail.com>
parents: 10378
diff changeset
260
10356
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
261 static Complex
10381
1aa8b9b8f921 str2double.cc: style fix
John W. Eaton <jwe@octave.org>
parents: 10380
diff changeset
262 str2double1 (const std::string& str_arg)
10356
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
263 {
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
264 Complex val (0.0, 0.0);
10381
1aa8b9b8f921 str2double.cc: style fix
John W. Eaton <jwe@octave.org>
parents: 10380
diff changeset
265
1aa8b9b8f921 str2double.cc: style fix
John W. Eaton <jwe@octave.org>
parents: 10380
diff changeset
266 std::string str = str_arg;
1aa8b9b8f921 str2double.cc: style fix
John W. Eaton <jwe@octave.org>
parents: 10380
diff changeset
267
14077
b6eeeb67fa3f str2double: return NaN for things like "1 2 3 4" (bug #34713).
John W. Eaton <jwe@octave.org>
parents: 13141
diff changeset
268 // FIXME -- removing all commas does too much...
10356
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
269 std::string::iterator se = str.end ();
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
270 se = std::remove (str.begin (), se, ',');
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
271 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
272 std::istringstream is (str);
10356
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
273
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
274 double num;
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
275 bool i1, i2, s1, s2;
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
276
10357
7658cd4bdcf2 handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents: 10356
diff changeset
277 if (is.eof ())
7658cd4bdcf2 handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents: 10356
diff changeset
278 val = octave_NaN;
7658cd4bdcf2 handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents: 10356
diff changeset
279 else if (! extract_num (is, num, i1, s1))
10356
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
280 val = octave_NaN;
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
281 else
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
282 {
10380
60acc47c203f configure checks for complex element setter/reference accessor methods
John W. Eaton <jwe@octave.org>
parents: 10379
diff changeset
283 set_component (val, num, i1);
10356
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
284
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
285 if (! is.eof ())
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
286 {
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
287 if (! extract_num (is, num, i2, s2) || i1 == i2 || ! s2)
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
288 val = octave_NaN;
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
289 else
10380
60acc47c203f configure checks for complex element setter/reference accessor methods
John W. Eaton <jwe@octave.org>
parents: 10379
diff changeset
290 set_component (val, num, i2);
10356
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
291 }
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
292 }
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
293
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
294 return val;
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
295 }
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
296
15039
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14854
diff changeset
297 DEFUN (str2double, args, ,
10356
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
298 "-*- texinfo -*-\n\
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
299 @deftypefn {Built-in Function} {} str2double (@var{s})\n\
11414
5e6ef42a2445 Overhaul str2double function fixing bug and docstring and adding 'NA' processing.
Rik <octave@nomad.inbox5.com>
parents: 10846
diff changeset
300 Convert a string to a real or complex number.\n\
5e6ef42a2445 Overhaul str2double function fixing bug and docstring and adding 'NA' processing.
Rik <octave@nomad.inbox5.com>
parents: 10846
diff changeset
301 \n\
5e6ef42a2445 Overhaul str2double function fixing bug and docstring and adding 'NA' processing.
Rik <octave@nomad.inbox5.com>
parents: 10846
diff changeset
302 The string must be in one of the following formats where\n\
17281
bc924baa2c4e doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents: 15195
diff changeset
303 a and b are real numbers and the complex unit is @qcode{'i'} or @qcode{'j'}:\n\
10356
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
304 \n\
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
305 @itemize\n\
10840
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10381
diff changeset
306 @item a + bi\n\
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10381
diff changeset
307 \n\
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10381
diff changeset
308 @item a + b*i\n\
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10381
diff changeset
309 \n\
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10381
diff changeset
310 @item a + i*b\n\
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10381
diff changeset
311 \n\
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10381
diff changeset
312 @item bi + a\n\
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10381
diff changeset
313 \n\
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10381
diff changeset
314 @item b*i + a\n\
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10381
diff changeset
315 \n\
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10381
diff changeset
316 @item i*b + a\n\
10356
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
317 @end itemize\n\
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
318 \n\
11591
1d13679b587e Use @nospell macro on certain words in docstrings.
Rik <octave@nomad.inbox5.com>
parents: 11586
diff changeset
319 If present, a and/or b are of the form @nospell{[+-]d[,.]d[[eE][+-]d]} where\n\
17281
bc924baa2c4e doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents: 15195
diff changeset
320 the brackets indicate optional arguments and @qcode{'d'} indicates zero or\n\
bc924baa2c4e doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents: 15195
diff changeset
321 more digits. The special input values @code{Inf}, @code{NaN}, and @code{NA}\n\
bc924baa2c4e doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents: 15195
diff changeset
322 are also accepted.\n\
11414
5e6ef42a2445 Overhaul str2double function fixing bug and docstring and adding 'NA' processing.
Rik <octave@nomad.inbox5.com>
parents: 10846
diff changeset
323 \n\
5e6ef42a2445 Overhaul str2double function fixing bug and docstring and adding 'NA' processing.
Rik <octave@nomad.inbox5.com>
parents: 10846
diff changeset
324 @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
325 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
326 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
327 returned.\n\
5e6ef42a2445 Overhaul str2double function fixing bug and docstring and adding 'NA' processing.
Rik <octave@nomad.inbox5.com>
parents: 10846
diff changeset
328 \n\
12703
d326be468b13 doc: str2double() inputs which cannot be parsed return NaN.
Rik <octave@nomad.inbox5.com>
parents: 11591
diff changeset
329 @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
330 converted.\n\
d326be468b13 doc: str2double() inputs which cannot be parsed return NaN.
Rik <octave@nomad.inbox5.com>
parents: 11591
diff changeset
331 \n\
d326be468b13 doc: str2double() inputs which cannot be parsed return NaN.
Rik <octave@nomad.inbox5.com>
parents: 11591
diff changeset
332 @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
333 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
334 @seealso{str2num}\n\
10356
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
335 @end deftypefn")
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 octave_value retval;
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
338
12735
76e2c55906de str2double.cc: Return NaN for invalid inputs rather than printing error.
Rik <octave@nomad.inbox5.com>
parents: 12703
diff changeset
339 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
340 print_usage ();
76e2c55906de str2double.cc: Return NaN for invalid inputs rather than printing error.
Rik <octave@nomad.inbox5.com>
parents: 12703
diff changeset
341 else if (args(0).is_string ())
10356
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
342 {
12735
76e2c55906de str2double.cc: Return NaN for invalid inputs rather than printing error.
Rik <octave@nomad.inbox5.com>
parents: 12703
diff changeset
343 if (args(0).rows () == 1 && args(0).ndims () == 2)
10356
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
344 {
12735
76e2c55906de str2double.cc: Return NaN for invalid inputs rather than printing error.
Rik <octave@nomad.inbox5.com>
parents: 12703
diff changeset
345 retval = str2double1 (args(0).string_value ());
10356
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
346 }
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
347 else
12735
76e2c55906de str2double.cc: Return NaN for invalid inputs rather than printing error.
Rik <octave@nomad.inbox5.com>
parents: 12703
diff changeset
348 {
76e2c55906de str2double.cc: Return NaN for invalid inputs rather than printing error.
Rik <octave@nomad.inbox5.com>
parents: 12703
diff changeset
349 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
350 if (! error_state)
76e2c55906de str2double.cc: Return NaN for invalid inputs rather than printing error.
Rik <octave@nomad.inbox5.com>
parents: 12703
diff changeset
351 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
352 }
76e2c55906de str2double.cc: Return NaN for invalid inputs rather than printing error.
Rik <octave@nomad.inbox5.com>
parents: 12703
diff changeset
353 }
76e2c55906de str2double.cc: Return NaN for invalid inputs rather than printing error.
Rik <octave@nomad.inbox5.com>
parents: 12703
diff changeset
354 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
355 {
13141
e81ddf9cacd5 maint: untabify and remove trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 12735
diff changeset
356 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
357
76e2c55906de str2double.cc: Return NaN for invalid inputs rather than printing error.
Rik <octave@nomad.inbox5.com>
parents: 12703
diff changeset
358 if (! error_state)
76e2c55906de str2double.cc: Return NaN for invalid inputs rather than printing error.
Rik <octave@nomad.inbox5.com>
parents: 12703
diff changeset
359 {
76e2c55906de str2double.cc: Return NaN for invalid inputs rather than printing error.
Rik <octave@nomad.inbox5.com>
parents: 12703
diff changeset
360 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
361 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
362 {
76e2c55906de str2double.cc: Return NaN for invalid inputs rather than printing error.
Rik <octave@nomad.inbox5.com>
parents: 12703
diff changeset
363 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
364 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
365 }
76e2c55906de str2double.cc: Return NaN for invalid inputs rather than printing error.
Rik <octave@nomad.inbox5.com>
parents: 12703
diff changeset
366 retval = output;
76e2c55906de str2double.cc: Return NaN for invalid inputs rather than printing error.
Rik <octave@nomad.inbox5.com>
parents: 12703
diff changeset
367 }
10356
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
368 }
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
369 else
12735
76e2c55906de str2double.cc: Return NaN for invalid inputs rather than printing error.
Rik <octave@nomad.inbox5.com>
parents: 12703
diff changeset
370 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
371
10356
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
372
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
373 return retval;
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
374 }
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
375
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
376 /*
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
377 %!assert (str2double ("1"), 1)
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
378 %!assert (str2double ("-.1e-5"), -1e-6)
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
379 %!assert (str2double (char ("1", "2 3", "4i")), [1; NaN; 4i])
14091
c3ea07298af4 str2double: correctly handle things like ["1"; "2 3"; "4i"]
John W. Eaton <jwe@octave.org>
parents: 14077
diff changeset
380 %!assert (str2double ("-.1e-5"), -1e-6)
10356
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
381 %!assert (str2double ("1,222.5"), 1222.5)
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
382 %!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
383 %!assert (str2double ("2j"), 2i)
10356
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
384 %!assert (str2double ("2 + j"), 2+j)
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
385 %!assert (str2double ("i*2 + 3"), 3+2i)
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
386 %!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
387 %!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
388 %!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
389 %!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
390 %!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
391 %!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
392 %!assert (str2double ("Hello World"), NaN)
10357
7658cd4bdcf2 handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents: 10356
diff changeset
393 %!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
394 %!assert (str2double ("NA"), NA)
10357
7658cd4bdcf2 handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents: 10356
diff changeset
395 %!assert (str2double ("Inf"), Inf)
14702
a08f6e17336e str2double.cc: Case-insensitive detection of 'Inf' (Bug #36536).
Rik <octave@nomad.inbox5.com>
parents: 14501
diff changeset
396 %!assert (str2double ("iNF"), Inf)
10357
7658cd4bdcf2 handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents: 10356
diff changeset
397 %!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
398 %!assert (str2double ("Inf*i"), complex (0, Inf))
14702
a08f6e17336e str2double.cc: Case-insensitive detection of 'Inf' (Bug #36536).
Rik <octave@nomad.inbox5.com>
parents: 14501
diff changeset
399 %!assert (str2double ("iNF*i"), complex (0, Inf))
10357
7658cd4bdcf2 handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents: 10356
diff changeset
400 %!assert (str2double ("NaN + Inf*i"), complex (NaN, Inf))
7658cd4bdcf2 handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents: 10356
diff changeset
401 %!assert (str2double ("Inf - Inf*i"), complex (Inf, -Inf))
7658cd4bdcf2 handle Infs and NaNs in str2double
Jaroslav Hajek <highegg@gmail.com>
parents: 10356
diff changeset
402 %!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
403 %!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
404 %!assert (str2double ({2, "4i"}), [NaN + 0i, 4i])
14854
5ae9f0f77635 maint: Use Octave coding conventions for coddling parenthis is DLD-FUNCTIONS directory
Rik <octave@nomad.inbox5.com>
parents: 14702
diff changeset
405 %!assert (str2double (zeros (3,1,2)), NaN (3,1,2))
10356
1d7930b77ab9 implement compiled str2double
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
406 */