Main Content

log2

Base-2 logarithm of symbolic input

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=F2E. 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 = 

log(x1/3)log(2)

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

yVal = subs(ySym,x,4)
yVal = 

log(41/3)log(2)

simplify(yVal)
ans = 

23

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=F2E.

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 = 

(12a257)

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

(1212log(2a2)log(2)+12a257)

E = 

(1log(2a2)log(2)+10)

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

Simplify the results using simplify.

F = simplify(F)
F = 

(122a-a-157)

E = simplify(E)
E = (1a0)

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

See Also

| |