log2
Base 2 logarithm and floating-point number dissection
Description
Examples
Dissect several numbers into the exponent and mantissa. These operations all follow standard IEEE® arithmetic.
Create a vector X that contains several test values. Calculate the exponent and mantissa for each number.
X = [1 pi -3 eps realmax realmin];
format rat
[F,E] = log2(X)
F = 1/2 355/452 -3/4 1/2 1 1/2
E = 1 2 2 -51 1024 -1021
Collect the results in a table. Convert the numbers into character vectors for display purposes.
x = {'1','pi','-3','eps','realmax','realmin'}'; f = strtrim(cellstr(rats(F'))); T = table(x,f,E','VariableNames',{'Value','Mantissa','Exponent'})
T=6×3 table
Value Mantissa Exponent
___________ ___________ ________
{'1' } {'1/2' } 1
{'pi' } {'355/452'} 2
{'-3' } {'-3/4' } 2
{'eps' } {'1/2' } -51
{'realmax'} {'1' } 1024
{'realmin'} {'1/2' } -1021
The results indicate that, for the first row, . Similarly, for the fourth row, .
Input Arguments
Input matrix, specified as a scalar, vector, matrix, multidimensional array, table, or timetable.
For floating-point number dissection [F,E] = log2(X)
,
any zeros in X
produce F = 0
and
E = 0
. Input values of Inf
,
-Inf
, or NaN
are returned
unchanged in F
with a corresponding exponent of
E = 0
.
Data Types: single
| double
| table
| timetable
Complex Number Support: Yes
Output Arguments
Base 2 logarithm values, returned as a scalar, vector, matrix,
multidimensional array, table, or timetable of the same size as
X
.
Mantissa values, returned as a scalar, vector, matrix, multidimensional
array, table, or timetable of the same size as X
. The
values in F
and E
satisfy X =
F.*2.^E
.
Exponent values, returned as a scalar, vector, matrix, multidimensional
array, table, or timetable of the same size as X
. The
values in F
and E
satisfy X =
F.*2.^E
.
Tips
This function corresponds to the ANSI® C function
frexp()
and the IEEE® floating-point standard functionlogb()
. Any zeros inX
produceF = 0
andE = 0
.
Extended Capabilities
The
log2
function fully supports tall arrays. For more information,
see Tall Arrays.
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
GPU Code Generation
Generate CUDA® code for NVIDIA® GPUs using GPU Coder™.
This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.
The log2
function
supports GPU array input with these usage notes and limitations:
The syntax
[F,E] = log2(X)
is not supported.If the output of the function running on the GPU can be complex, then you must explicitly specify its input arguments as complex. For more information, see Work with Complex Numbers on a GPU (Parallel Computing Toolbox).
For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).
This function fully supports distributed arrays. For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).
Version History
Introduced before R2006aThe log2
function issues a warning if you specify a complex
input when using the two-output syntax. Support for complex input in this syntax
will be removed in a future release. In previous releases, the two-output syntax of
log2
ignored the imaginary part of a complex input and
processed only the real part.
To preserve the behavior of previous releases, use the real
function to extract the real part of a complex input, as shown in this table.
Not Recommended (Warns) | Recommended |
---|---|
X = 2 - 1i; [F,E] = log2(X); |
X = 2 - 1i; [F,E] = log2(real(X)); |
The log2
function can calculate on all variables within a table or
timetable without indexing to access those variables. All variables must have data types
that support the calculation. For more information, see Direct Calculations on Tables and Timetables.
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: United States.
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)