# HG changeset patch # User Arun Giridhar # Date 1714059664 14400 # Node ID 4401310ad7377a8b178e79dc2fba462a315240de # Parent 1ad552012ffb1110c70e57f7f870739e4b64d8bf 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). diff -r 1ad552012ffb -r 4401310ad737 scripts/general/rat.m --- 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 rat (int8 (3)) %!error rat (1, "a") %!error rat (1, [1 2]) -%!error rat (1, -1) +%!error rat (1, -1)