# HG changeset patch # User Rik # Date 1307399210 25200 # Node ID 76e2c55906de187c94c36229292bf62efad8dfa1 # Parent b67c2d580a25dcd315a70b9101928a06d6f66ec9 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. diff -r b67c2d580a25 -r 76e2c55906de src/DLD-FUNCTIONS/str2double.cc --- a/src/DLD-FUNCTIONS/str2double.cc Fri Jun 10 14:35:42 2011 -0400 +++ b/src/DLD-FUNCTIONS/str2double.cc Mon Jun 06 15:26:50 2011 -0700 @@ -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 (str2double1); - } - } - else if (args(0).is_cellstr ()) - { - Array sa = args(0).cellstr_value (); - retval = sa.map (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 (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)) */