changeset 33459:4401310ad737

rat.m: Fix FIXME about tolerance The default tolerance calculated by rat() could become 0, for example when the input is all-zeros, and that is a valid tolerance, but any tolerance value explicitly passed as input was being forbidden from being 0, which broke complex inputs. This check has been changed from "> 0" to ">= 0". * rat.m: Change comparison with 0, update BISTs, add new BIST, remove FIXME. Also minor formatting fixes (end --> endif, spacing).
author Arun Giridhar <arungiridhar@gmail.com>
date Thu, 25 Apr 2024 11:41:04 -0400
parents 1ad552012ffb
children d9edf5bc95c7 2f5a875246ae
files scripts/general/rat.m
diffstat 1 files changed, 11 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/general/rat.m	Thu Apr 25 16:24:25 2024 +0200
+++ b/scripts/general/rat.m	Thu Apr 25 11:41:04 2024 -0400
@@ -98,11 +98,9 @@
   if (nargin == 1)
     ## default norm
     tol = 1e-6 * norm (y, 1);
-    ## FIXME: tol becomes 0 if all inputs have Inf in them,
-    ## which breaks rat (complex (0, inf)); see bug #55198.
   else
-    if (! (isscalar (tol) && isnumeric (tol) && tol > 0))
-      error ("rat: TOL must be a numeric scalar > 0");
+    if (! (isscalar (tol) && isnumeric (tol) && tol >= 0))
+      error ("rat: TOL must be a numeric scalar >= 0");
     endif
   endif
 
@@ -115,8 +113,8 @@
     elseif (nargout <= 1)  # string output
       realstr = rat (real (x), tol);
       imagstr = rat (imag (x), tol);
-      n = [repmat("(", rows(realstr), 1), realstr, repmat(") + (", rows(realstr), 1), imagstr, repmat(") * i", rows(imagstr), 1)];
-    end
+      n = [repmat("(", rows (realstr), 1), realstr, repmat(") + (", rows (realstr), 1), imagstr, repmat(") * i", rows (imagstr), 1)];
+    endif
     return
   endif
 
@@ -268,10 +266,15 @@
 %! assert (str(4, :), "(0 + 1/(3 + 1/(4 + 1/(4 + 1/(4 + 1/4))))) + (-1 + 1/(20 + 1/(2 + 1/(3 + 1/6)))     ) * i");
 
 ## Test complex exceptional inputs
-%!test <55198>
+%!test <*55198>
 %! assert (rat (complex (inf, 0)), "(Inf) + (0) * i");
 %! assert (rat (complex (0, inf)), "(0) + (Inf) * i");
 
+## Test eval with complex inputs
+%!test <*55198>
+%! x = complex (0.5, pi);
+%! assert (eval (rat (x)), x, 1e-6 * norm (x, 1))
+
 %!assert <*43374> (eval (rat (0.75)), [0.75])
 
 ## Test input validation
@@ -279,4 +282,4 @@
 %!error <X must be a single or double array> rat (int8 (3))
 %!error <TOL must be a numeric scalar> rat (1, "a")
 %!error <TOL must be a numeric scalar> rat (1, [1 2])
-%!error <TOL must be a numeric scalar . 0> rat (1, -1)
+%!error <TOL must be a numeric scalar .* 0> rat (1, -1)