Mercurial > hg > octave-jordi
changeset 19789:7eff42dd717d
maint: Merge away accidental head.
author | Rik <rik@octave.org> |
---|---|
date | Fri, 20 Feb 2015 08:13:24 -0800 |
parents | 52e625f0e361 (current diff) 511b3ae4872b (diff) |
children | b254a2e0859c |
files | |
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)