# HG changeset patch
# User Rik
# Date 1307399210 25200
# Branch stable
# Node ID 2949f88e3b226aae790b3bc2585d54e3375e078e
# Parent 7c8b50cd4fb40e26bc7e741cb3998361b035cf7b
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 7c8b50cd4fb4 -r 2949f88e3b22 src/DLD-FUNCTIONS/str2double.cc
--- a/src/DLD-FUNCTIONS/str2double.cc Mon Jun 06 12:38:15 2011 -0500
+++ 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))
*/