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 |
5307
|
17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
18 ## 02110-1301, 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 |
5642
|
33 ## @seealso{log, log10, logspace, exp} |
3321
|
34 ## @end deftypefn |
2537
|
35 |
|
36 ## Author: AW <Andreas.Weingessel@ci.tuwien.ac.at> |
|
37 ## Created: 17 October 1994 |
|
38 ## Adapted-By: jwe |
|
39 |
|
40 function [f, e] = log2 (x) |
|
41 |
|
42 if (nargin != 1) |
|
43 usage ("y = log2 (x) or [f, e] = log2 (x)"); |
|
44 endif |
|
45 |
|
46 if (nargout < 2) |
|
47 f = log (x) / log (2); |
|
48 elseif (nargout == 2) |
|
49 ## Only deal with the real parts ... |
|
50 x = real (x); |
3426
|
51 ## Since log (0) gives problems, 0 entries are replaced by 1. |
2537
|
52 ## This is corrected later by multiplication with the sign. |
|
53 f = abs (x) + (x == 0); |
|
54 e = (floor (log (f) / log (2)) + 1) .* (x != 0); |
|
55 f = sign (x) .* f ./ (2 .^ e); |
|
56 else |
|
57 error ("log2 takes at most 2 output arguments"); |
|
58 endif |
|
59 |
|
60 endfunction |
|
61 |