2539
|
1 ## Copyright (C) 1995, 1996 Kurt Hornik |
3426
|
2 ## |
3922
|
3 ## This file is part of Octave. |
|
4 ## |
|
5 ## Octave is free software; you can redistribute it and/or modify it |
|
6 ## under the terms of the GNU General Public License as published by |
2539
|
7 ## the Free Software Foundation; either version 2, or (at your option) |
|
8 ## any later version. |
3426
|
9 ## |
3922
|
10 ## Octave is distributed in the hope that it will be useful, but |
2539
|
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
3426
|
13 ## General Public License for more details. |
|
14 ## |
2539
|
15 ## You should have received a copy of the GNU General Public License |
3922
|
16 ## along with Octave; see the file COPYING. If not, write to the Free |
5307
|
17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
18 ## 02110-1301, USA. |
2539
|
19 |
3321
|
20 ## -*- texinfo -*- |
|
21 ## @deftypefn {Function File} {} nextpow2 (@var{x}) |
|
22 ## If @var{x} is a scalar, returns the first integer @var{n} such that |
|
23 ## @iftex |
|
24 ## @tex |
|
25 ## $2^n \ge |x|$. |
|
26 ## @end tex |
|
27 ## @end iftex |
|
28 ## @ifinfo |
|
29 ## 2^n >= abs (x). |
|
30 ## @end ifinfo |
3426
|
31 ## |
3321
|
32 ## If @var{x} is a vector, return @code{nextpow2 (length (@var{x}))}. |
|
33 ## @end deftypefn |
5053
|
34 ## |
3407
|
35 ## @seealso{pow2} |
2539
|
36 |
5428
|
37 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
2539
|
38 ## Created: 7 October 1994 |
|
39 ## Adapted-By: jwe |
|
40 |
|
41 function n = nextpow2 (x) |
3426
|
42 |
2539
|
43 if (nargin != 1) |
|
44 usage ("nextpow2 (x)"); |
|
45 endif |
|
46 |
4030
|
47 if (! (isscalar (x) || isvector (x))) |
2539
|
48 error ("nextpow2: x must be a scalar or a vector"); |
|
49 endif |
2816
|
50 |
|
51 t = length (x); |
|
52 if (t > 1) |
|
53 x = t; |
|
54 endif |
3426
|
55 |
2539
|
56 [f, n] = log2 (abs (x)); |
|
57 if (f == 0.5) |
|
58 n = n - 1; |
|
59 endif |
3426
|
60 |
2539
|
61 endfunction |