Construct half-precision numeric object
Use the half
constructor to assign a half-precision data type
to a number or variable. A half-precision data type occupies 16 bits of memory, but its
floating-point representation enables it to handle wider dynamic ranges than integer or
fixed-point data types of the same size.
For more information, see Floating-Point Numbers (Fixed-Point Designer).
v
— Input arrayInput array, specified as a scalar, vector, matrix, or multidimensional array.
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
| logical
Complex Number Support: Yes
These functions are supported for use with half-precision inputs.
abs | Absolute value and complex magnitude |
acos | Inverse cosine in radians |
asin | Inverse sine in radians |
atanh | Inverse hyperbolic tangent |
ceil | Round toward positive infinity |
conv | Convolution and polynomial multiplication |
conv2 | 2-D convolution |
cos | Cosine of argument in radians |
cospi | Compute cos(X*pi) accurately |
dot | Dot product |
exp | Exponential |
expm1 | Compute exp(x)-1 accurately for small values of x |
fix | Round toward zero |
floor | Round toward negative infinity |
fma | Multiply and add using fused multiply add approach |
hypot | Square root of sum of squares (hypotenuse) |
ldivide | Left array division |
log | Natural logarithm |
log1p | Compute log(1+x) accurately for small values of x |
mean | Average or mean value of array |
minus | Subtraction |
mod | Remainder after division (modulo operation) |
mtimes | Matrix multiplication |
plus | Addition |
pow10 | Base 10 power and scale half-precision numbers |
pow2 | Base 2 power and scale floating-point numbers |
prod | Product of array elements |
rdivide | Right array division |
rem | Remainder after division |
round | Round to nearest decimal or integer |
rsqrt | Reciprocal square root |
sin | Sine of argument in radians |
sinpi | Compute sin(X*pi) accurately |
sqrt | Square root |
sum | Sum of array elements |
tanh | Hyperbolic tangent |
times | Multiplication |
uminus | Unary minus |
uplus | Unary plus |
cast | Convert variable to different data type |
cell | Cell array |
double | Double-precision arrays |
eps | Floating-point relative accuracy |
Inf | Create array of all Inf values |
int16 | 16-bit signed integer arrays |
int32 | 32-bit signed integer arrays |
int64 | 64-bit signed integer arrays |
int8 | 8-bit signed integer arrays |
isa | Determine if input has specified data type |
isfloat | Determine whether input is floating-point data type |
islogical | Determine if input is logical array |
isnan | Determine which array elements are NaN |
isnumeric | Determine whether input is numeric array |
isreal | Determine whether array is real |
logical | Convert numeric values to logicals |
NaN | Create array of all NaN values |
single | Single-precision arrays |
uint16 | 16-bit unsigned integer arrays |
uint32 | 32-bit unsigned integer arrays |
uint64 | 64-bit unsigned integer arrays |
uint8 | 8-bit unsigned integer arrays |
all | Determine if all array elements are nonzero or true |
and | Find logical AND |
any | Determine if any array elements are nonzero |
eq | Determine equality |
ge | Determine greater than or equal to |
gt | Determine greater than |
le | Determine less than or equal to |
lt | Determine less than |
ne | Determine inequality |
not | Find logical NOT |
or | Find logical OR |
cat | Concatenate arrays |
colon | Vector creation, array subscripting, and for -loop
iteration |
eye | Identity matrix |
full | Convert sparse matrix to full storage |
horzcat | Horizontal concatenation for heterogeneous arrays |
iscolumn | Determine whether input is column vector |
isempty | Determine whether array is empty |
isfinite | Determine which array elements are finite |
isinf | Determine which array elements are infinite |
ismatrix | Determine whether input is matrix |
isrow | Determine whether input is row vector |
isscalar | Determine whether input is scalar |
isvector | Determine whether input is vector |
length | Length of largest array dimension |
max | Maximum elements of an array |
min | Minimum elements of an array |
ndims | Number of array dimensions |
numel | Number of array elements |
ones | Create array of all ones |
repmat | Repeat copies of array |
reshape | Reshape array |
size | Array size |
subsasgn | Redefine subscripted assignment |
subsref | Subscripted reference |
transpose | Transpose vector or matrix |
vertcat | Vertical concatenation for heterogeneous arrays |
zeros | Create array of all zeros |
bar | Bar graph |
barh | Horizontal bar graph |
fplot | Plot expression or function |
line | Create primitive line |
plot | 2-D line plot |
plot3 | 3-D point or line plot |
plotmatrix | Scatter plot matrix |
rgbplot | Plot colormap |
scatter | Scatter plot |
scatter3 | 3-D scatter plot |
xlim | Set or query x-axis limits |
ylim | Set or query y-axis limits |
zlim | Set or query z-axis limits |
To cast a double-precision number to half precision, use the half
function.
a = half(pi)
a = half 3.1406
You can also use the half
function to cast an existing variable to half precision.
v = single(magic(3))
v = 3x3 single matrix
8 1 6
3 5 7
4 9 2
a = half(v)
a = 3x3 half matrix 8 1 6 3 5 7 4 9 2
The following functions which support half-precision inputs, do not support complex half-precision inputs.
rsqrt
fma
All functions that support half-precision inputs,
support code generation, except for the rsqrt
function.
In MATLAB®, the isobject
function returns true with a half-precision
input. However, in generated code, this function returns false.
If your target hardware does not have native support for half-precision, then half is used as a storage type, with arithmetic operations performed in single precision.
Some functions use half only as a storage type and the arithmetic is always performed in single-precision, regardless of the target hardware.
Code generation for 32-bit targets is not supported if your MATLAB code contains half-precision data types.
CUDA® compute capability of 5.3 or higher is required for generating and executing code with half-precision data types.
CUDA toolkit version of 10.0 or higher is required for generating and executing code with half-precision data types.
The memory allocation (malloc
) mode for generating CUDA code must be set to 'Discrete'
.
Half-precision complex data types are not supported for GPU code generation.
For GPU Code generation, half-precision matrix multiplication can only be performed with real inputs.
In MATLAB, the isobject
function returns true with a
half-precision input. However, in generated code, this function returns false.
If your target hardware does not have native support for half-precision, then half is used as a storage type, with arithmetic operations performed in single precision.
Some functions use half only as a storage type and the arithmetic is always performed in single-precision, regardless of the target hardware.
Code generation for 32-bit targets is not supported if your MATLAB code contains half-precision data types.
You have a modified version of this example. Do you want to open this example with your edits?