# HG changeset patch # User Markus Mützel # Date 1661354134 -7200 # Node ID 863730dd0f839b1778723fa4fb969b4fe6776df0 # Parent 4c38cf0ce06cf4a37a5eb37ec6f3235d5a62defd nextpow2: Fix for input between 0.5 and 1 (bug #62947). * scripts/general/nextpow2.m: Switch to a naïve implementation using log2 with a single output argument and ceil. diff -r 4c38cf0ce06c -r 863730dd0f83 scripts/general/nextpow2.m --- a/scripts/general/nextpow2.m Tue Aug 23 19:44:16 2022 +0200 +++ b/scripts/general/nextpow2.m Wed Aug 24 17:15:34 2022 +0200 @@ -49,10 +49,8 @@ error ("nextpow2: X must be numeric"); endif - [f, n] = log2 (abs (x)); - idx = (n == 0); # Find any failures of log2 function (n == 0) - n(idx) = f(idx); # and copy over value. - n(f == 0.5)--; + n = ceil (log2 (abs (x))); + n(x == 0) = 0; # special case endfunction @@ -64,6 +62,8 @@ %!assert (nextpow2 (-17), 5) %!assert (nextpow2 (-31), 5) %!assert (nextpow2 (1:17), [0 1 2 2 3 3 3 3 4 4 4 4 4 4 4 4 5]) +%!assert (nextpow2 (0.5), -1) +%!assert (nextpow2 (0.6), 0) ## Special cases %!assert (nextpow2 (0), 0) %!assert (nextpow2 (1), 0)