Mercurial > octave
annotate scripts/strings/dec2bin.m @ 31239:dd6b37f67db2
Accept negative inputs to -2^63 for dec2bin and dec2hex (bug #63089)
dec2bin.m: Accept negative inputs lower than -flintmax down to -2^63
Return 64-bit string in certain cases for Matlab compatibility
Activate BISTs commented out earlier
dec2hex.m: Remove repeated code and call dec2bin instead
Accept negative inputs lower than -flintmax down to -2^63
Update and activate BISTs commented out earlier
author | Arun Giridhar <arungiridhar@gmail.com> |
---|---|
date | Sun, 25 Sep 2022 06:22:25 -0400 |
parents | 5d3faba0342e |
children | 7018819318d1 |
rev | line source |
---|---|
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
1 ######################################################################## |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
2 ## |
30564
796f54d4ddbf
update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents:
29359
diff
changeset
|
3 ## Copyright (C) 1996-2022 The Octave Project Developers |
27918
b442ec6dda5c
use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
4 ## |
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
5 ## See the file COPYRIGHT.md in the top-level directory of this |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
6 ## distribution or <https://octave.org/copyright/>. |
2325 | 7 ## |
2313 | 8 ## This file is part of Octave. |
9 ## | |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
10 ## Octave is free software: you can redistribute it and/or modify it |
2313 | 11 ## under the terms of the GNU General Public License as published by |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
12 ## the Free Software Foundation, either version 3 of the License, or |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
13 ## (at your option) any later version. |
2313 | 14 ## |
15 ## Octave is distributed in the hope that it will be useful, but | |
16 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
17 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
18 ## GNU General Public License for more details. |
2313 | 19 ## |
20 ## You should have received a copy of the GNU General Public License | |
7016 | 21 ## along with Octave; see the file COPYING. If not, see |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
22 ## <https://www.gnu.org/licenses/>. |
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
23 ## |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
24 ######################################################################## |
2268 | 25 |
3361 | 26 ## -*- texinfo -*- |
30875
5d3faba0342e
doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents:
30564
diff
changeset
|
27 ## @deftypefn {} {@var{bstr} =} dec2bin (@var{d}) |
5d3faba0342e
doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents:
30564
diff
changeset
|
28 ## @deftypefnx {} {@var{bstr} =} dec2bin (@var{d}, @var{len}) |
28237
ac3a078e688f
dec2bin.m: Reduce number of times search for negative numbers is done (bug #58147).
Rik <rik@octave.org>
parents:
28236
diff
changeset
|
29 ## Return a string of ones and zeros representing the conversion of the integer |
ac3a078e688f
dec2bin.m: Reduce number of times search for negative numbers is done (bug #58147).
Rik <rik@octave.org>
parents:
28236
diff
changeset
|
30 ## @var{d} to a binary number. |
20164
df437a52bcaf
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
31 ## |
28237
ac3a078e688f
dec2bin.m: Reduce number of times search for negative numbers is done (bug #58147).
Rik <rik@octave.org>
parents:
28236
diff
changeset
|
32 ## If @var{d} is a matrix or cell array, return a string matrix with one row |
ac3a078e688f
dec2bin.m: Reduce number of times search for negative numbers is done (bug #58147).
Rik <rik@octave.org>
parents:
28236
diff
changeset
|
33 ## for each element in @var{d}, padded with leading zeros to the width of the |
ac3a078e688f
dec2bin.m: Reduce number of times search for negative numbers is done (bug #58147).
Rik <rik@octave.org>
parents:
28236
diff
changeset
|
34 ## largest value. |
ac3a078e688f
dec2bin.m: Reduce number of times search for negative numbers is done (bug #58147).
Rik <rik@octave.org>
parents:
28236
diff
changeset
|
35 ## |
ac3a078e688f
dec2bin.m: Reduce number of times search for negative numbers is done (bug #58147).
Rik <rik@octave.org>
parents:
28236
diff
changeset
|
36 ## The optional second argument, @var{len}, specifies the minimum number of |
ac3a078e688f
dec2bin.m: Reduce number of times search for negative numbers is done (bug #58147).
Rik <rik@octave.org>
parents:
28236
diff
changeset
|
37 ## digits in the result. |
ac3a078e688f
dec2bin.m: Reduce number of times search for negative numbers is done (bug #58147).
Rik <rik@octave.org>
parents:
28236
diff
changeset
|
38 ## |
31239
dd6b37f67db2
Accept negative inputs to -2^63 for dec2bin and dec2hex (bug #63089)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
39 ## For negative elements of @var{d}, return the binary value of the two's |
dd6b37f67db2
Accept negative inputs to -2^63 for dec2bin and dec2hex (bug #63089)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
40 ## complement. The result is padded with leading ones to 8, 16, 32, or 64 |
dd6b37f67db2
Accept negative inputs to -2^63 for dec2bin and dec2hex (bug #63089)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
41 ## bits as appropriate for the magnitude of the input. Positive input |
dd6b37f67db2
Accept negative inputs to -2^63 for dec2bin and dec2hex (bug #63089)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
42 ## elements are padded with leading zeros to the same width. If the second |
dd6b37f67db2
Accept negative inputs to -2^63 for dec2bin and dec2hex (bug #63089)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
43 ## argument @var{len} exceeds that calculated width, the result is further |
dd6b37f67db2
Accept negative inputs to -2^63 for dec2bin and dec2hex (bug #63089)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
44 ## padded with leading zeros, for compatibility with @sc{matlab}. |
dd6b37f67db2
Accept negative inputs to -2^63 for dec2bin and dec2hex (bug #63089)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
45 ## |
28237
ac3a078e688f
dec2bin.m: Reduce number of times search for negative numbers is done (bug #58147).
Rik <rik@octave.org>
parents:
28236
diff
changeset
|
46 ## Examples: |
3426 | 47 ## |
3361 | 48 ## @example |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
49 ## @group |
3361 | 50 ## dec2bin (14) |
51 ## @result{} "1110" | |
28236
5bb1c0cbb27e
dec2bin.m: Allow negative number inputs (bug #58147).
Nicholas R. Jankowski <jankowskin@asme.org>
parents:
27985
diff
changeset
|
52 ## |
5bb1c0cbb27e
dec2bin.m: Allow negative number inputs (bug #58147).
Nicholas R. Jankowski <jankowskin@asme.org>
parents:
27985
diff
changeset
|
53 ## dec2bin (-14) |
5bb1c0cbb27e
dec2bin.m: Allow negative number inputs (bug #58147).
Nicholas R. Jankowski <jankowskin@asme.org>
parents:
27985
diff
changeset
|
54 ## @result{} "11110010" |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
55 ## @end group |
3361 | 56 ## @end example |
3789 | 57 ## |
29197
1a3cb2e1644a
dec2bin.m: Add docstring note about fractional input incompatibility (bug #59719).
Nicholas R. Jankowski <jankowskin@asme.org>
parents:
28886
diff
changeset
|
58 ## Known @sc{matlab} Incompatibility: @sc{matlab}'s @code{dec2bin} allows |
1a3cb2e1644a
dec2bin.m: Add docstring note about fractional input incompatibility (bug #59719).
Nicholas R. Jankowski <jankowskin@asme.org>
parents:
28886
diff
changeset
|
59 ## non-integer values for @var{d}, truncating the value using the equivalent |
1a3cb2e1644a
dec2bin.m: Add docstring note about fractional input incompatibility (bug #59719).
Nicholas R. Jankowski <jankowskin@asme.org>
parents:
28886
diff
changeset
|
60 ## of @code{fix (@var{d})} for positive values, but, as of R2020b and in |
1a3cb2e1644a
dec2bin.m: Add docstring note about fractional input incompatibility (bug #59719).
Nicholas R. Jankowski <jankowskin@asme.org>
parents:
28886
diff
changeset
|
61 ## conflict with published documentation, appears to use |
1a3cb2e1644a
dec2bin.m: Add docstring note about fractional input incompatibility (bug #59719).
Nicholas R. Jankowski <jankowskin@asme.org>
parents:
28886
diff
changeset
|
62 ## @code{round (@var{d})} for negative values. To be consistent with |
1a3cb2e1644a
dec2bin.m: Add docstring note about fractional input incompatibility (bug #59719).
Nicholas R. Jankowski <jankowskin@asme.org>
parents:
28886
diff
changeset
|
63 ## @code{dec2hex} and @code{dec2base}, Octave produces an error for non-integer |
1a3cb2e1644a
dec2bin.m: Add docstring note about fractional input incompatibility (bug #59719).
Nicholas R. Jankowski <jankowskin@asme.org>
parents:
28886
diff
changeset
|
64 ## valued inputs for @var{d}. Users wanting compatible code for non-integer |
1a3cb2e1644a
dec2bin.m: Add docstring note about fractional input incompatibility (bug #59719).
Nicholas R. Jankowski <jankowskin@asme.org>
parents:
28886
diff
changeset
|
65 ## valued inputs should make use of @code{fix} or @code{round} as appropriate. |
11172
7e8ce65f73cf
Overhaul functions used to convert between number bases.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
66 ## @seealso{bin2dec, dec2base, dec2hex} |
3361 | 67 ## @end deftypefn |
2268 | 68 |
30875
5d3faba0342e
doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents:
30564
diff
changeset
|
69 function bstr = dec2bin (d, len) |
2268 | 70 |
28789
28de41192f3c
Eliminate unneeded verification of nargin, nargout in m-files.
Rik <rik@octave.org>
parents:
28693
diff
changeset
|
71 if (nargin == 0) |
28237
ac3a078e688f
dec2bin.m: Reduce number of times search for negative numbers is done (bug #58147).
Rik <rik@octave.org>
parents:
28236
diff
changeset
|
72 print_usage (); |
ac3a078e688f
dec2bin.m: Reduce number of times search for negative numbers is done (bug #58147).
Rik <rik@octave.org>
parents:
28236
diff
changeset
|
73 endif |
28236
5bb1c0cbb27e
dec2bin.m: Allow negative number inputs (bug #58147).
Nicholas R. Jankowski <jankowskin@asme.org>
parents:
27985
diff
changeset
|
74 |
28237
ac3a078e688f
dec2bin.m: Reduce number of times search for negative numbers is done (bug #58147).
Rik <rik@octave.org>
parents:
28236
diff
changeset
|
75 if (iscell (d)) |
ac3a078e688f
dec2bin.m: Reduce number of times search for negative numbers is done (bug #58147).
Rik <rik@octave.org>
parents:
28236
diff
changeset
|
76 d = cell2mat (d); |
28236
5bb1c0cbb27e
dec2bin.m: Allow negative number inputs (bug #58147).
Nicholas R. Jankowski <jankowskin@asme.org>
parents:
27985
diff
changeset
|
77 endif |
28237
ac3a078e688f
dec2bin.m: Reduce number of times search for negative numbers is done (bug #58147).
Rik <rik@octave.org>
parents:
28236
diff
changeset
|
78 ## Create column vector for algorithm (output is always col. vector anyways) |
ac3a078e688f
dec2bin.m: Reduce number of times search for negative numbers is done (bug #58147).
Rik <rik@octave.org>
parents:
28236
diff
changeset
|
79 d = d(:); |
28693
2bb050267d74
maint: Remove trailing spaces from code base.
Rik <rik@octave.org>
parents:
28241
diff
changeset
|
80 |
31239
dd6b37f67db2
Accept negative inputs to -2^63 for dec2bin and dec2hex (bug #63089)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
81 neg = (d < 0); # keep track of which elements are negative |
dd6b37f67db2
Accept negative inputs to -2^63 for dec2bin and dec2hex (bug #63089)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
82 if (any (neg)) # must be a signed type |
dd6b37f67db2
Accept negative inputs to -2^63 for dec2bin and dec2hex (bug #63089)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
83 ## Cast to a suitable signed integer type, then to unsigned. |
dd6b37f67db2
Accept negative inputs to -2^63 for dec2bin and dec2hex (bug #63089)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
84 ## Ensure that the left-most bit of the unsigned number is 1, |
dd6b37f67db2
Accept negative inputs to -2^63 for dec2bin and dec2hex (bug #63089)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
85 ## to signify negative input. |
dd6b37f67db2
Accept negative inputs to -2^63 for dec2bin and dec2hex (bug #63089)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
86 tmp = int64 (d); |
dd6b37f67db2
Accept negative inputs to -2^63 for dec2bin and dec2hex (bug #63089)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
87 if (all (tmp >= -128 & tmp <= 127)) |
dd6b37f67db2
Accept negative inputs to -2^63 for dec2bin and dec2hex (bug #63089)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
88 d = int8 (d); |
dd6b37f67db2
Accept negative inputs to -2^63 for dec2bin and dec2hex (bug #63089)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
89 d(neg) = (d(neg) + intmax (d)) + 1; |
dd6b37f67db2
Accept negative inputs to -2^63 for dec2bin and dec2hex (bug #63089)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
90 d = uint8 (d); |
dd6b37f67db2
Accept negative inputs to -2^63 for dec2bin and dec2hex (bug #63089)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
91 d(neg) += uint8 (128); |
dd6b37f67db2
Accept negative inputs to -2^63 for dec2bin and dec2hex (bug #63089)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
92 elseif (all (tmp >= -32768 & tmp <= 32767)) |
dd6b37f67db2
Accept negative inputs to -2^63 for dec2bin and dec2hex (bug #63089)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
93 d = int16 (d); |
dd6b37f67db2
Accept negative inputs to -2^63 for dec2bin and dec2hex (bug #63089)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
94 d(neg) = (d(neg) + intmax (d)) + 1; |
dd6b37f67db2
Accept negative inputs to -2^63 for dec2bin and dec2hex (bug #63089)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
95 d = uint16 (d); |
dd6b37f67db2
Accept negative inputs to -2^63 for dec2bin and dec2hex (bug #63089)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
96 d(neg) += uint16 (32768); |
dd6b37f67db2
Accept negative inputs to -2^63 for dec2bin and dec2hex (bug #63089)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
97 elseif (all (tmp >= -2147483648 & tmp <= 2147483647)) |
dd6b37f67db2
Accept negative inputs to -2^63 for dec2bin and dec2hex (bug #63089)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
98 d = int32 (d); |
dd6b37f67db2
Accept negative inputs to -2^63 for dec2bin and dec2hex (bug #63089)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
99 d(neg) = (d(neg) + intmax (d)) + 1; |
dd6b37f67db2
Accept negative inputs to -2^63 for dec2bin and dec2hex (bug #63089)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
100 d = uint32 (d); |
dd6b37f67db2
Accept negative inputs to -2^63 for dec2bin and dec2hex (bug #63089)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
101 d(neg) += uint32 (2147483648); |
28237
ac3a078e688f
dec2bin.m: Reduce number of times search for negative numbers is done (bug #58147).
Rik <rik@octave.org>
parents:
28236
diff
changeset
|
102 else |
31239
dd6b37f67db2
Accept negative inputs to -2^63 for dec2bin and dec2hex (bug #63089)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
103 d = int64 (d); |
dd6b37f67db2
Accept negative inputs to -2^63 for dec2bin and dec2hex (bug #63089)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
104 d(neg) = (d(neg) + intmax (d)) + 1; |
dd6b37f67db2
Accept negative inputs to -2^63 for dec2bin and dec2hex (bug #63089)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
105 d = uint64 (d); |
dd6b37f67db2
Accept negative inputs to -2^63 for dec2bin and dec2hex (bug #63089)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
106 d(neg) += uint64 (9223372036854775808); |
28237
ac3a078e688f
dec2bin.m: Reduce number of times search for negative numbers is done (bug #58147).
Rik <rik@octave.org>
parents:
28236
diff
changeset
|
107 endif |
ac3a078e688f
dec2bin.m: Reduce number of times search for negative numbers is done (bug #58147).
Rik <rik@octave.org>
parents:
28236
diff
changeset
|
108 endif |
28236
5bb1c0cbb27e
dec2bin.m: Allow negative number inputs (bug #58147).
Nicholas R. Jankowski <jankowskin@asme.org>
parents:
27985
diff
changeset
|
109 |
4492 | 110 if (nargin == 1) |
30875
5d3faba0342e
doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents:
30564
diff
changeset
|
111 bstr = dec2base (d, 2); |
28237
ac3a078e688f
dec2bin.m: Reduce number of times search for negative numbers is done (bug #58147).
Rik <rik@octave.org>
parents:
28236
diff
changeset
|
112 else |
30875
5d3faba0342e
doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents:
30564
diff
changeset
|
113 bstr = dec2base (d, 2, len); |
2268 | 114 endif |
115 | |
116 endfunction | |
7411 | 117 |
13167
f7cb824dc8c0
Allow cellstr inputs for dec2* conversion functions.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
118 |
28236
5bb1c0cbb27e
dec2bin.m: Allow negative number inputs (bug #58147).
Nicholas R. Jankowski <jankowskin@asme.org>
parents:
27985
diff
changeset
|
119 %!assert (dec2bin (3), "11") |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
120 %!assert (dec2bin (14), "1110") |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
121 %!assert (dec2bin (14, 6), "001110") |
28236
5bb1c0cbb27e
dec2bin.m: Allow negative number inputs (bug #58147).
Nicholas R. Jankowski <jankowskin@asme.org>
parents:
27985
diff
changeset
|
122 %!assert (dec2bin ([1, 2; 3, 4]), ["001"; "011"; "010"; "100"]) |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
123 %!assert (dec2bin ({1, 2; 3, 4}), ["001"; "011"; "010"; "100"]) |
28236
5bb1c0cbb27e
dec2bin.m: Allow negative number inputs (bug #58147).
Nicholas R. Jankowski <jankowskin@asme.org>
parents:
27985
diff
changeset
|
124 %!assert (dec2bin ({1, 2; 3, 4}, 4), ["0001"; "0011"; "0010"; "0100"]) |
5bb1c0cbb27e
dec2bin.m: Allow negative number inputs (bug #58147).
Nicholas R. Jankowski <jankowskin@asme.org>
parents:
27985
diff
changeset
|
125 |
5bb1c0cbb27e
dec2bin.m: Allow negative number inputs (bug #58147).
Nicholas R. Jankowski <jankowskin@asme.org>
parents:
27985
diff
changeset
|
126 ## Test negative inputs |
5bb1c0cbb27e
dec2bin.m: Allow negative number inputs (bug #58147).
Nicholas R. Jankowski <jankowskin@asme.org>
parents:
27985
diff
changeset
|
127 %!assert (dec2bin (-3), "11111101") |
5bb1c0cbb27e
dec2bin.m: Allow negative number inputs (bug #58147).
Nicholas R. Jankowski <jankowskin@asme.org>
parents:
27985
diff
changeset
|
128 %!assert (dec2bin (-3, 3), "11111101") |
5bb1c0cbb27e
dec2bin.m: Allow negative number inputs (bug #58147).
Nicholas R. Jankowski <jankowskin@asme.org>
parents:
27985
diff
changeset
|
129 %!assert (dec2bin (-3, 9), "011111101") |
31239
dd6b37f67db2
Accept negative inputs to -2^63 for dec2bin and dec2hex (bug #63089)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
130 %!assert (dec2bin (-2^7 - 1), "1111111101111111") |
dd6b37f67db2
Accept negative inputs to -2^63 for dec2bin and dec2hex (bug #63089)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
131 %!assert (dec2bin (-2^15 - 1), "11111111111111110111111111111111") |
dd6b37f67db2
Accept negative inputs to -2^63 for dec2bin and dec2hex (bug #63089)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
132 %!assert (dec2bin (-2^31 - 1), |
dd6b37f67db2
Accept negative inputs to -2^63 for dec2bin and dec2hex (bug #63089)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
133 %! "1111111111111111111111111111111101111111111111111111111111111111") |
28241
ef7bc64a604b
dec2bin.m: Fix algorithm for numbers less than intmin ('int32') (bug #58147).
Rik <rik@octave.org>
parents:
28237
diff
changeset
|
134 %!assert (dec2bin (-2^52), |
31239
dd6b37f67db2
Accept negative inputs to -2^63 for dec2bin and dec2hex (bug #63089)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
135 %! "1111111111110000000000000000000000000000000000000000000000000000") |
dd6b37f67db2
Accept negative inputs to -2^63 for dec2bin and dec2hex (bug #63089)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
136 %!assert (dec2bin (-2^63), |
28237
ac3a078e688f
dec2bin.m: Reduce number of times search for negative numbers is done (bug #58147).
Rik <rik@octave.org>
parents:
28236
diff
changeset
|
137 %! "1000000000000000000000000000000000000000000000000000000000000000") |
31239
dd6b37f67db2
Accept negative inputs to -2^63 for dec2bin and dec2hex (bug #63089)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
138 %!assert (dec2bin (int64 (-2) ^ 63), |
dd6b37f67db2
Accept negative inputs to -2^63 for dec2bin and dec2hex (bug #63089)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
139 %! "1000000000000000000000000000000000000000000000000000000000000000") |
dd6b37f67db2
Accept negative inputs to -2^63 for dec2bin and dec2hex (bug #63089)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
140 %!assert (dec2bin (int64 (-2) ^ 63 - 1), |
dd6b37f67db2
Accept negative inputs to -2^63 for dec2bin and dec2hex (bug #63089)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
141 %! "1000000000000000000000000000000000000000000000000000000000000000") |
dd6b37f67db2
Accept negative inputs to -2^63 for dec2bin and dec2hex (bug #63089)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
142 %!assert (dec2bin (int64 (-2) ^ 63 + 1), |
dd6b37f67db2
Accept negative inputs to -2^63 for dec2bin and dec2hex (bug #63089)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
143 %! "1000000000000000000000000000000000000000000000000000000000000001") |
28237
ac3a078e688f
dec2bin.m: Reduce number of times search for negative numbers is done (bug #58147).
Rik <rik@octave.org>
parents:
28236
diff
changeset
|
144 %!assert (dec2bin ([-1, -2; -3, -4]), |
ac3a078e688f
dec2bin.m: Reduce number of times search for negative numbers is done (bug #58147).
Rik <rik@octave.org>
parents:
28236
diff
changeset
|
145 %! ["11111111"; "11111101"; "11111110"; "11111100"]) |
ac3a078e688f
dec2bin.m: Reduce number of times search for negative numbers is done (bug #58147).
Rik <rik@octave.org>
parents:
28236
diff
changeset
|
146 %!assert (dec2bin ([1, 2; 3, -4]), |
ac3a078e688f
dec2bin.m: Reduce number of times search for negative numbers is done (bug #58147).
Rik <rik@octave.org>
parents:
28236
diff
changeset
|
147 %! ["00000001"; "00000011"; "00000010"; "11111100"]) |
ac3a078e688f
dec2bin.m: Reduce number of times search for negative numbers is done (bug #58147).
Rik <rik@octave.org>
parents:
28236
diff
changeset
|
148 %!assert (dec2bin ({1, 2; 3, -4}), |
ac3a078e688f
dec2bin.m: Reduce number of times search for negative numbers is done (bug #58147).
Rik <rik@octave.org>
parents:
28236
diff
changeset
|
149 %! ["00000001"; "00000011"; "00000010"; "11111100"]) |
7411 | 150 |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
151 ## Test input validation |
28886
d8318c12d903
test: remove unnecessary BIST tests in m-files checking for excessive number of inputs.
Rik <rik@octave.org>
parents:
28789
diff
changeset
|
152 %!error <Invalid call> dec2bin () |
31239
dd6b37f67db2
Accept negative inputs to -2^63 for dec2bin and dec2hex (bug #63089)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
153 |