changeset 31198:863730dd0f83 stable

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.
author Markus Mützel <markus.muetzel@gmx.de>
date Wed, 24 Aug 2022 17:15:34 +0200
parents 4c38cf0ce06c
children 8bd9b64aeb01 075443476dfb
files scripts/general/nextpow2.m
diffstat 1 files changed, 4 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- 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)