log2
Base 2 logarithm and floating-point number dissection
Description
Examples
Base 2 Logarithm Values
Floating-Point Number Dissection
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
X
— Input matrix
scalar | vector | matrix | multidimensional array | table | timetable
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
Y
— Base 2 logarithm values
scalar | vector | matrix | multidimensional array | table | timetable
Base 2 logarithm values, returned as a scalar, vector, matrix,
multidimensional array, table, or timetable of the same size as
X
.
F
— Mantissa values
scalar | vector | matrix | multidimensional array | table | timetable
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
.
E
— Exponent values
scalar | vector | matrix | multidimensional array | table | timetable
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
Tall Arrays
Calculate with arrays that have more rows than fit in memory.
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™.
Thread-Based Environment
Run code in the background using MATLAB® backgroundPool
or accelerate code with Parallel Computing Toolbox™ ThreadPool
.
This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.
GPU Arrays
Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.
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).
Distributed Arrays
Partition large arrays across the combined memory of your cluster using 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 R2006aR2023a: Perform calculations directly on tables and timetables
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: .
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)