Mercurial > octave
comparison scripts/strings/dec2bin.m @ 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 | 96ad887ae4f8 |
children | 121b24b3a224 |
comparison
equal
deleted
inserted
replaced
31339:0fe1cb1e2f12 | 31342:7880af7a3b3f |
---|---|
54 ## @result{} "11110010" | 54 ## @result{} "11110010" |
55 ## @end group | 55 ## @end group |
56 ## @end example | 56 ## @end example |
57 ## | 57 ## |
58 ## Known @sc{matlab} Incompatibility: @sc{matlab}'s @code{dec2bin} allows | 58 ## Known @sc{matlab} Incompatibility: @sc{matlab}'s @code{dec2bin} allows |
59 ## non-integer values for @var{d}, truncating the value using the equivalent | 59 ## non-integer values for @var{d} as of Release 2022b, but is inconsistent |
60 ## of @code{fix (@var{d})} for positive values, but, as of R2020b and in | 60 ## with truncation versus rounding and is also inconsistent with its own |
61 ## conflict with published documentation, appears to use | 61 ## @code{dec2hex} function. For self-consistency, Octave gives an error for |
62 ## @code{round (@var{d})} for negative values. To be consistent with | 62 ## non-integer inputs. Users requiring compatible code for non-integer inputs |
63 ## @code{dec2hex} and @code{dec2base}, Octave produces an error for non-integer | 63 ## should make use of @code{fix} or @code{round} as appropriate. |
64 ## valued inputs for @var{d}. Users wanting compatible code for non-integer | |
65 ## valued inputs should make use of @code{fix} or @code{round} as appropriate. | |
66 ## @seealso{bin2dec, dec2base, dec2hex} | 64 ## @seealso{bin2dec, dec2base, dec2hex} |
67 ## @end deftypefn | 65 ## @end deftypefn |
68 | 66 |
69 function bstr = dec2bin (d, len) | 67 function bstr = dec2bin (d, len) |
70 | 68 |
73 endif | 71 endif |
74 | 72 |
75 if (iscell (d)) | 73 if (iscell (d)) |
76 d = cell2mat (d); | 74 d = cell2mat (d); |
77 endif | 75 endif |
76 | |
77 if (! isnumeric (d) || iscomplex (d) || any (d(:) != round (d(:)))) | |
78 error ("dec2bin: input must be integers"); | |
79 endif | |
80 | |
78 ## Create column vector for algorithm (output is always col. vector anyways) | 81 ## Create column vector for algorithm (output is always col. vector anyways) |
79 d = d(:); | 82 d = d(:); |
80 | 83 |
81 neg = (d < 0); # keep track of which elements are negative | 84 neg = (d < 0); # keep track of which elements are negative |
82 if (any (neg)) # must be a signed type | 85 if (any (neg)) # must be a signed type |
148 %!assert (dec2bin ({1, 2; 3, -4}), | 151 %!assert (dec2bin ({1, 2; 3, -4}), |
149 %! ["00000001"; "00000011"; "00000010"; "11111100"]) | 152 %! ["00000001"; "00000011"; "00000010"; "11111100"]) |
150 | 153 |
151 ## Test input validation | 154 ## Test input validation |
152 %!error <Invalid call> dec2bin () | 155 %!error <Invalid call> dec2bin () |
156 %!error <input must be integer> dec2bin (+2.1) | |
157 %!error <input must be integer> dec2bin (-2.1) | |
158 %!error <input must be integer> dec2bin (+2.9) | |
159 %!error <input must be integer> dec2bin (-2.9) | |
160 %!error <input must be integer> dec2bin (1+i) | |
153 | 161 |