Mercurial > octave
changeset 31342:7880af7a3b3f stable
dec2bin.m: Fix input validation (bug #63089)
dec2bin.m: Input validation to allow only real integer input.
Cells are OK, but complex and struct types are not.
Throw error for non-integer inputs for self-consistency.
author | Arun Giridhar <arungiridhar@gmail.com> |
---|---|
date | Tue, 25 Oct 2022 13:03:06 -0400 |
parents | 0fe1cb1e2f12 |
children | 121b24b3a224 989ebec75ff0 |
files | scripts/strings/dec2bin.m |
diffstat | 1 files changed, 15 insertions(+), 7 deletions(-) [+] |
line wrap: on
line diff
--- a/scripts/strings/dec2bin.m Sat Oct 22 20:24:03 2022 +0200 +++ b/scripts/strings/dec2bin.m Tue Oct 25 13:03:06 2022 -0400 @@ -56,13 +56,11 @@ ## @end example ## ## Known @sc{matlab} Incompatibility: @sc{matlab}'s @code{dec2bin} allows -## non-integer values for @var{d}, truncating the value using the equivalent -## of @code{fix (@var{d})} for positive values, but, as of R2020b and in -## conflict with published documentation, appears to use -## @code{round (@var{d})} for negative values. To be consistent with -## @code{dec2hex} and @code{dec2base}, Octave produces an error for non-integer -## valued inputs for @var{d}. Users wanting compatible code for non-integer -## valued inputs should make use of @code{fix} or @code{round} as appropriate. +## non-integer values for @var{d} as of Release 2022b, but is inconsistent +## with truncation versus rounding and is also inconsistent with its own +## @code{dec2hex} function. For self-consistency, Octave gives an error for +## non-integer inputs. Users requiring compatible code for non-integer inputs +## should make use of @code{fix} or @code{round} as appropriate. ## @seealso{bin2dec, dec2base, dec2hex} ## @end deftypefn @@ -75,6 +73,11 @@ if (iscell (d)) d = cell2mat (d); endif + + if (! isnumeric (d) || iscomplex (d) || any (d(:) != round (d(:)))) + error ("dec2bin: input must be integers"); + endif + ## Create column vector for algorithm (output is always col. vector anyways) d = d(:); @@ -150,4 +153,9 @@ ## Test input validation %!error <Invalid call> dec2bin () +%!error <input must be integer> dec2bin (+2.1) +%!error <input must be integer> dec2bin (-2.1) +%!error <input must be integer> dec2bin (+2.9) +%!error <input must be integer> dec2bin (-2.9) +%!error <input must be integer> dec2bin (1+i)