2537
|
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 |
2537
|
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 |
2537
|
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 ## |
2537
|
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 |
|
17 ## Software Foundation, 59 Temple Place - Suite 330, Boston, MA |
|
18 ## 02111-1307, USA. |
2537
|
19 |
3321
|
20 ## -*- texinfo -*- |
3500
|
21 ## @deftypefn {Mapping Function} {} log2 (@var{x}) |
5016
|
22 ## @deftypefnx {Mapping Function} {[@var{f}, @var{e}] =} log2 (@var{x}) |
3321
|
23 ## Compute the base-2 logarithm of @var{x}. With two outputs, returns |
|
24 ## @var{f} and @var{e} such that |
|
25 ## @iftex |
|
26 ## @tex |
|
27 ## $1/2 <= |f| < 1$ and $x = f \cdot 2^e$. |
|
28 ## @end tex |
|
29 ## @end iftex |
|
30 ## @ifinfo |
|
31 ## 1/2 <= abs(f) < 1 and x = f * 2^e. |
|
32 ## @end ifinfo |
|
33 ## @end deftypefn |
5053
|
34 ## |
3408
|
35 ## @seealso{log, log10, logspace, and exp} |
2537
|
36 |
|
37 ## Author: AW <Andreas.Weingessel@ci.tuwien.ac.at> |
|
38 ## Created: 17 October 1994 |
|
39 ## Adapted-By: jwe |
|
40 |
|
41 function [f, e] = log2 (x) |
|
42 |
|
43 if (nargin != 1) |
|
44 usage ("y = log2 (x) or [f, e] = log2 (x)"); |
|
45 endif |
|
46 |
|
47 if (nargout < 2) |
|
48 f = log (x) / log (2); |
|
49 elseif (nargout == 2) |
|
50 ## Only deal with the real parts ... |
|
51 x = real (x); |
3426
|
52 ## Since log (0) gives problems, 0 entries are replaced by 1. |
2537
|
53 ## This is corrected later by multiplication with the sign. |
|
54 f = abs (x) + (x == 0); |
|
55 e = (floor (log (f) / log (2)) + 1) .* (x != 0); |
|
56 f = sign (x) .* f ./ (2 .^ e); |
|
57 else |
|
58 error ("log2 takes at most 2 output arguments"); |
|
59 endif |
|
60 |
|
61 endfunction |
|
62 |