Mercurial > hg > octave-terminal
changeset 12735:76e2c55906de stable
str2double.cc: Return NaN for invalid inputs rather than printing error.
* str2double.cc: Return NaN for invalid inputs rather than printing error. Add
tests for new behavior.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Mon, 06 Jun 2011 15:26:50 -0700 |
parents | b67c2d580a25 |
children | dd9b46dff1ac |
files | src/DLD-FUNCTIONS/str2double.cc |
diffstat | 1 files changed, 31 insertions(+), 19 deletions(-) [+] |
line wrap: on
line diff
--- a/src/DLD-FUNCTIONS/str2double.cc +++ b/src/DLD-FUNCTIONS/str2double.cc @@ -254,31 +254,39 @@ { octave_value retval; - if (args.length () == 1) + if (args.length () != 1) + print_usage (); + else if (args(0).is_string ()) { - if (args(0).is_string ()) + if (args(0).rows () == 1 && args(0).ndims () == 2) { - if (args(0).rows () == 1 && args(0).ndims () == 2) - { - retval = str2double1 (args(0).string_value ()); - } - else - { - const string_vector sv = args(0).all_strings (); - if (! error_state) - retval = sv.map<Complex> (str2double1); - } - } - else if (args(0).is_cellstr ()) - { - Array<std::string> sa = args(0).cellstr_value (); - retval = sa.map<Complex> (str2double1); + retval = str2double1 (args(0).string_value ()); } else - gripe_wrong_type_arg ("str2double", args(0)); + { + const string_vector sv = args(0).all_strings (); + if (! error_state) + retval = sv.map<Complex> (str2double1); + } + } + else if (args(0).is_cell ()) + { + const Cell cell = args(0).cell_value (); + + if (! error_state) + { + ComplexNDArray output (cell.dims (), octave_NaN); + for (octave_idx_type i = 0; i < cell.numel (); i++) + { + if (cell(i).is_string ()) + output(i) = str2double1 (cell(i).string_value ()); + } + retval = output; + } } else - print_usage (); + retval = NDArray (args(0).dims (), octave_NaN); + return retval; } @@ -296,6 +304,7 @@ %!assert (str2double ("1e-3 + i*.25"), 1e-3 + 0.25i) %!assert (str2double (["2 + j";"1.25e-3";"-05"]), [2+i; 1.25e-3; -5]) %!assert (str2double ({"2 + j","1.25e-3","-05"}), [2+i, 1.25e-3, -5]) +%!assert (str2double (1), NaN) %!assert (str2double ("Hello World"), NaN) %!assert (str2double ("NaN"), NaN) %!assert (str2double ("NA"), NA) @@ -305,5 +314,8 @@ %!assert (str2double ("NaN + Inf*i"), complex (NaN, Inf)) %!assert (str2double ("Inf - Inf*i"), complex (Inf, -Inf)) %!assert (str2double ("-i*NaN - Inf"), complex (-Inf, -NaN)) +%!assert (str2double ({"abc", "4i"}), [NaN + 0i, 4i]) +%!assert (str2double ({2, "4i"}), [NaN + 0i, 4i]) +%!assert (str2double (zeros(3,1,2)), NaN (3,1,2)) */