Mercurial > hg > octave-nkf
changeset 19993:511b3ae4872b
nextpow2: compute for each element of input instead of its length (bug #44291)
* general/nextpow2.m: when input is not a scalar, nextpow2() was using
length(x). Not only is this Matlab incompatible, it is also not very
useful in the light of nd signals where input will be the length of each
dimension. It would also break very bad when the user wants to pass a
vector that has length 1 in which case it would use the first element of
the vector. This commit changes it so that it computes nextpow2 for each
element of the input. When the previous behaviour is intended,
`nextpow2 (length (x))' should be used.
* NEWS: make note of this backwards incompatible change.
author | Carnë Draug <carandraug@octave.org> |
---|---|
date | Wed, 18 Feb 2015 17:27:06 +0000 |
parents | 4ae2987c5f66 |
children | 7eff42dd717d |
files | NEWS scripts/general/nextpow2.m |
diffstat | 2 files changed, 13 insertions(+), 13 deletions(-) [+] |
line wrap: on
line diff
--- a/NEWS +++ b/NEWS @@ -44,6 +44,11 @@ the dimension of "x". The old behaviour of "ismatrix" is obtained by "isnumeric (x) || islogical (x) || ischar (x)". + ** The nextpow2 function behaviour has been changed for vector inputs. + Instead of computing `nextpow2 (length (x))', it will now compute + nextpow2() for each element of the input. Not only is this change + Matlab compatible, it also prevents bugs for "vectors" of length 1. + ** The preference do_braindead_shortcircuit_evaluation
--- a/scripts/general/nextpow2.m +++ b/scripts/general/nextpow2.m @@ -18,7 +18,10 @@ ## -*- texinfo -*- ## @deftypefn {Function File} {} nextpow2 (@var{x}) -## If @var{x} is a scalar, return the first integer @var{n} such that +## Compute exponent for smallest power of two larger than input. +## +## For each element in the input array @var{x}, returns the first integer +## @var{n} such that ## @tex ## $2^n \ge |x|$. ## @end tex @@ -26,7 +29,6 @@ ## 2^n @geq{} abs (x). ## @end ifnottex ## -## If @var{x} is a vector, return @code{nextpow2 (length (@var{x}))}. ## @seealso{pow2, log2} ## @end deftypefn @@ -40,19 +42,12 @@ print_usage (); endif - if (! (isscalar (x) || isvector (x))) - error ("nextpow2: X must be a scalar or a vector"); - endif - - t = length (x); - if (t > 1) - x = t; + if (! isnumeric (x)) + error ("nextpow2: X must be numeric"); endif [f, n] = log2 (abs (x)); - if (f == 0.5) - n = n - 1; - endif + n(f == 0.5)--; endfunction @@ -63,7 +58,7 @@ %!assert (nextpow2 (-16), 4) %!assert (nextpow2 (-17), 5) %!assert (nextpow2 (-31), 5) -%!assert (nextpow2 (1:17), 5) +%!assert (nextpow2 (1:17), [0 1 2 2 3 3 3 3 4 4 4 4 4 4 4 4 5]) %!error nexpow2 () %!error nexpow2 (1, 2)