changeset 27742:d39c56824d01

idivid.m: error out if both inputs are floating point (bug #57278). * idivide.m: Redo input validation and issue an error if both inputs are floating point numbers. Make error() messages more informative. Add BIST tests for input validation.
author Rik <rik@octave.org>
date Sat, 23 Nov 2019 10:12:49 -0800
parents b5e88a4bce43
children d51b3c6f3892
files scripts/general/idivide.m
diffstat 1 files changed, 34 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/general/idivide.m	Sat Nov 23 07:47:54 2019 -0800
+++ b/scripts/general/idivide.m	Sat Nov 23 10:12:49 2019 -0800
@@ -69,35 +69,43 @@
 
   if (nargin < 2 || nargin > 3)
     print_usage ();
-  elseif (nargin == 2)
+  endif
+
+  if (nargin == 2)
     op = "fix";
   else
     op = tolower (op);
   endif
 
-  if (strcmp (op, "round"))
-    z = x ./ y;
-  else
-    if (isfloat (x))
+  if (isfloat (x))
+    if (isinteger (y))
       typ = class (y);
-    elseif (isfloat (y))
+    else
+      error ("idivide: at least one input (X or Y) must be an integer type");
+    endif
+  elseif (isfloat (y))
+    if (isinteger (x))
       typ = class (x);
     else
-      typ = class (x);
-      if (! strcmp (class (x), class (y)))
-        error ("idivide: incompatible types");
-      endif
+      error ("idivide: at least one input (X or Y) must be an integer type");
     endif
+  else
+    typ = class (x);
+    if (! strcmp (typ, class (y)))
+      error ("idivide: integer type of X (%s) must match integer type of Y (%s)", typ, class (y));
+    endif
+  endif
 
-    if (strcmp (op, "fix"))
-      z = cast (fix (double (x) ./ double (y)), typ);
-    elseif (strcmp (op, "floor"))
-      z = cast (floor (double (x) ./ double (y)), typ);
-    elseif (strcmp (op, "ceil"))
-      z = cast (ceil (double (x) ./ double (y)), typ);
-    else
-      error ("idivide: unrecognized rounding type");
-    endif
+  if (strcmp (op, "fix"))
+    z = cast (fix (double (x) ./ double (y)), typ);
+  elseif (strcmp (op, "round"))
+    z = x ./ y;
+  elseif (strcmp (op, "floor"))
+    z = cast (floor (double (x) ./ double (y)), typ);
+  elseif (strcmp (op, "ceil"))
+    z = cast (ceil (double (x) ./ double (y)), typ);
+  else
+    error ('idivide: unrecognized rounding type "%s"', op);
   endif
 
 endfunction
@@ -124,4 +132,10 @@
 %!assert (idivide (a, bf, "ceil"), int8 ([0, 1]))
 %!assert (idivide (a, bf, "round"), int8 ([-1, 1]))
 
-%!error (idivide (uint8 (1), int8 (1)))
+## Test input validation
+%!error idivide (uint8 (1))
+%!error idivide (uint8 (1), 2, 3)
+%!error <at least one input> idivide (1, 2)
+%!error <at least one input> idivide ({1}, 2)
+%!error <X \(int8\) must match.* Y \(uint8\)> idivide (int8 (1), uint8 (2))
+%!error <unrecognized rounding type "foo"> idivide (int8 (1), 2, "foo")