Main Content

pow2

Base 2 exponentiation and scaling of floating-point numbers

Description

Y = pow2(E) computes 2 to the power of E such that Y=2E.

example

Y = pow2(X,E) computes X times 2 to the power of E such that Y=X2E.

example

Examples

collapse all

Raise 2 to the power of E.

E = [1 -2 4 -4 3 9];
Y = pow2(E)
Y = 1×6

    2.0000    0.2500   16.0000    0.0625    8.0000  512.0000

In this example, compare the standard IEEE® arithmetic results of scaling significands by 2 raised to the power of exponents and the pow2 results.

Create a cell array of character vectors to represent the exact values of several significands. Specify the exponents.

Xcell = {'1/2','pi/4','-3/4','1/2','1-eps/2','1/2'}';
E = [1 2 2 -51 1024 -1021]';

Specify Ycell as the standard IEEE arithmetic results of scaling Xcell by 2 raised to the power of E. Show these results in a table.

Ycell = {'1','pi','-3','eps','realmax','realmin'}';
table(Xcell,E,Ycell,'VariableNames',["Significand" "Exponent" "Value"])
ans=6×3 table
    Significand    Exponent       Value   
    ___________    ________    ___________

    {'1/2'    }         1      {'1'      }
    {'pi/4'   }         2      {'pi'     }
    {'-3/4'   }         2      {'-3'     }
    {'1/2'    }       -51      {'eps'    }
    {'1-eps/2'}      1024      {'realmax'}
    {'1/2'    }     -1021      {'realmin'}

Next, compare the results in the table to pow2.

Convert Xcell to floating-point numbers X. Scale X by 2 raised to the power of E by using pow2(X,E).

X = str2num(char(Xcell));
Y = pow2(X,E)
Y = 6×1
10308 ×

    0.0000
    0.0000
   -0.0000
    0.0000
    1.7977
    0.0000

Convert Ycell to floating-point numbers Ynum. Show that pow2 follows the standard IEEE arithmetic operations by comparing Y and Ynum using isequal.

Ynum = str2num(char(Ycell))
Ynum = 6×1
10308 ×

    0.0000
    0.0000
   -0.0000
    0.0000
    1.7977
    0.0000

isequal(Y,Ynum)
ans = logical
   1

Input Arguments

collapse all

Exponent values, specified as a scalar, vector, matrix, multidimensional array, table, or timetable.

Data Types: single | double | table | timetable

Significand values, specified as a scalar, vector, matrix, or multidimensional array of the same size as E.

Data Types: single | double | table | timetable

Tips

The syntax Y = pow2(X,E) corresponds to the ANSI® C function ldexp() and the IEEE® floating-point standard function scalbn(). The result Y is computed quickly by simply adding E to the floating-point exponent of X.

Extended Capabilities

Version History

Introduced before R2006a

expand all

See Also

| | | | | |