Mercurial > octave
comparison scripts/general/nextpow2.m @ 25236:69b21b8a0e9f
nextpow2.m: Compute value correctly for Inf or NaN inputs (bug #53463).
* nextpow2.m: Change docstring to define the output variable N.
Check for failure of 2-input form of log2 function by looking for
an exponent of 0. If found, copy over the value that log2 was
not able to process (e.g., Inf or NaN). Add BIST tests for
special cases. Add fail pattern to %!error test.
author | Rik <rik@octave.org> |
---|---|
date | Thu, 12 Apr 2018 14:34:10 -0700 |
parents | 6652d3823428 |
children | 996d78102a71 |
comparison
equal
deleted
inserted
replaced
25235:27e6b38571d3 | 25236:69b21b8a0e9f |
---|---|
15 ## You should have received a copy of the GNU General Public License | 15 ## You should have received a copy of the GNU General Public License |
16 ## along with Octave; see the file COPYING. If not, see | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <https://www.gnu.org/licenses/>. | 17 ## <https://www.gnu.org/licenses/>. |
18 | 18 |
19 ## -*- texinfo -*- | 19 ## -*- texinfo -*- |
20 ## @deftypefn {} {} nextpow2 (@var{x}) | 20 ## @deftypefn {} {@var{n} =} nextpow2 (@var{x}) |
21 ## Compute the exponent for the smallest power of two larger than the input. | 21 ## Compute the exponent for the smallest power of two larger than the input. |
22 ## | 22 ## |
23 ## For each element in the input array @var{x}, return the first integer | 23 ## For each element in the input array @var{x}, return the first integer |
24 ## @var{n} such that | 24 ## @var{n} such that |
25 ## @tex | 25 ## @tex |
45 if (! isnumeric (x)) | 45 if (! isnumeric (x)) |
46 error ("nextpow2: X must be numeric"); | 46 error ("nextpow2: X must be numeric"); |
47 endif | 47 endif |
48 | 48 |
49 [f, n] = log2 (abs (x)); | 49 [f, n] = log2 (abs (x)); |
50 n(f == 0.5)--; | 50 idx = (n == 0); # Find any failures of log2 function (n == 0) |
51 n(idx) = f(idx); # and copy over value. | |
52 n(f == 0.5)--; | |
51 | 53 |
52 endfunction | 54 endfunction |
53 | 55 |
54 | 56 |
55 %!assert (nextpow2 (16), 4) | 57 %!assert (nextpow2 (16), 4) |
57 %!assert (nextpow2 (31), 5) | 59 %!assert (nextpow2 (31), 5) |
58 %!assert (nextpow2 (-16), 4) | 60 %!assert (nextpow2 (-16), 4) |
59 %!assert (nextpow2 (-17), 5) | 61 %!assert (nextpow2 (-17), 5) |
60 %!assert (nextpow2 (-31), 5) | 62 %!assert (nextpow2 (-31), 5) |
61 %!assert (nextpow2 (1:17), [0 1 2 2 3 3 3 3 4 4 4 4 4 4 4 4 5]) | 63 %!assert (nextpow2 (1:17), [0 1 2 2 3 3 3 3 4 4 4 4 4 4 4 4 5]) |
64 ## Special cases | |
65 %!assert (nextpow2 (0), 0) | |
66 %!assert (nextpow2 (1), 0) | |
67 %!assert (nextpow2 (Inf), Inf) | |
68 %!assert (nextpow2 (-Inf), Inf) | |
69 %!assert (nextpow2 (NaN), NaN) | |
70 %!assert (nextpow2 ([1, Inf, 3, -Inf, 9, NaN]), [0, Inf, 2, Inf, 4, NaN]) | |
62 | 71 |
72 ## Test input validation | |
63 %!error nexpow2 () | 73 %!error nexpow2 () |
64 %!error nexpow2 (1, 2) | 74 %!error nexpow2 (1, 2) |
75 %!error <X must be numeric> nextpow2 ("t") |