# log2

Base-2 logarithm of symbolic input

## Syntax

``Y = log2(X)``
``[F,E] = log2(X)``

## Description

example

````Y = log2(X)` returns the logarithm to the base 2 of `X` such that 2Y = X. If `X` is an array, then `log2` acts element-wise on `X`.```

example

````[F,E] = log2(X)` returns arrays of mantissas and exponents, `F` and `E`, such that $X=F\cdot {2}^{E}$. The values returned in `F` are in the range ```0.5 <= abs(F) < 1```. Any zeros in `X` return ```F = 0``` and `E = 0`.```

## Examples

collapse all

Compute the base-2 logarithm of a numeric input.

`y = log2(4^(1/3))`
```y = 0.6667 ```

Compute the base-2 logarithm of a symbolic input. The result is in terms of the natural logarithm `log` function.

```syms x ySym = log2(x^(1/3))```
```ySym =  $\frac{\mathrm{log}\left({x}^{1/3}\right)}{\mathrm{log}\left(2\right)}$```

Substitute the symbolic variable `x` with a number by using `subs`. Simplify the result by using `simplify`.

`yVal = subs(ySym,x,4)`
```yVal =  $\frac{\mathrm{log}\left({4}^{1/3}\right)}{\mathrm{log}\left(2\right)}$```
`simplify(yVal)`
```ans =  $\frac{2}{3}$```

Find the mantissa and exponent of a base-2 logarithm of an input $X$. The mantissa $F$ and the exponent $E$ satisfy the relation $X=F\cdot {2}^{E}$.

Create a symbolic variable `a` and assume that it is real. Create a symbolic vector `X` that contains symbolic numbers and expressions. Find the exponent and mantissa for each element of `X`.

```syms a real; X = [1 0.5*2^a 5/7]```
```X =  $\left(\begin{array}{ccc}1& \frac{{2}^{a}}{2}& \frac{5}{7}\end{array}\right)$```
`[F,E] = log2(X)`
```F =  $\left(\begin{array}{ccc}\frac{1}{2}& \frac{\frac{1}{{2}^{⌊\frac{\mathrm{log}\left(\frac{{2}^{a}}{2}\right)}{\mathrm{log}\left(2\right)}⌋+1}} {2}^{a}}{2}& \frac{5}{7}\end{array}\right)$```
```E =  $\left(\begin{array}{ccc}1& ⌊\frac{\mathrm{log}\left(\frac{{2}^{a}}{2}\right)}{\mathrm{log}\left(2\right)}⌋+1& 0\end{array}\right)$```

The values returned in `F` have magnitudes in the range `0.5 <= abs(F) < 1`.

Simplify the results using `simplify`.

`F = simplify(F)`
```F =  $\left(\begin{array}{ccc}\frac{1}{2}& {2}^{a-⌊a⌋-1}& \frac{5}{7}\end{array}\right)$```
`E = simplify(E)`
`E = $\left(\begin{array}{ccc}1& ⌊a⌋& 0\end{array}\right)$`

## Input Arguments

collapse all

Input array, specified as a symbolic number, array, variable, function, or expression.

• When computing the base-2 logarithms of complex elements in `X`, `log2` ignores their imaginary parts.

• For the syntax `[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`.

## Output Arguments

collapse all

Base-2 logarithm values, returned as a symbolic number, vector, matrix, or array of the same size as `X`.

Mantissa values, returned as a symbolic scalar, vector, matrix, or array of the same size as `X`. The values in `F` and `E` satisfy `X = F.*2.^E`.

Exponent values, returned as a symbolic scalar, vector, matrix, or array of the same size as `X`. The values in `F` and `E` satisfy `X = F.*2.^E`.

## Tips

• For floating-point input, the syntax `[F,E] = log2(X)` corresponds to the ANSI® C function `frexp()` and the IEEE® standard function `logb()`. Any zeros in `X` produce `F = 0` and `E = 0`.

## Version History

Introduced before R2006a