# half

Construct half-precision numeric object

## Description

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) and
What Is Half Precision? (Fixed-Point Designer).

For a list of functions that support code generation with half-precision inputs, see Half Precision Code Generation Support.

## Creation

### Syntax

### Description

converts
the values in `a`

= half(`v`

)`v`

to half-precision.

### Input Arguments

`v`

— Input array

scalar | vector | matrix | multidimensional array

Input 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

## Object Functions

These functions are supported for simulation with half-precision inputs in MATLAB^{®}. MATLAB
System object™ supports half-precision data type and MATLAB System (Simulink) block
supports half-precision data type with real values. For a list of functions that support code
generation with half-precision inputs, see Half Precision Code Generation Support (Fixed-Point Designer).

### Math and Arithmetic

`abs` | Absolute value and complex magnitude |

`acos` | Inverse cosine in radians |

`acosh` | Inverse hyperbolic cosine |

`asin` | Inverse sine in radians |

`asinh` | Inverse hyperbolic sine |

`atan` | Inverse tangent in radians |

`atan2` | Four-quadrant inverse tangent |

`atanh` | Inverse hyperbolic tangent |

`ceil` | Round toward positive infinity |

`conj` | Complex conjugate |

`conv` | Convolution and polynomial multiplication |

`conv2` | 2-D convolution |

`cos` | Cosine of argument in radians |

`cosh` | Hyperbolic cosine |

`cospi` | Compute cos(X*pi) accurately |

`cumsum` | Cumulative sum |

`dot` | Dot product |

`exp` | Exponential |

`expm1` | Compute `exp(X)-1` accurately for small
`X` |

`fft` | Fast Fourier transform |

`fft2` | 2-D fast Fourier transform |

`fftn` | N-D fast Fourier transform |

`fftshift` | Shift zero-frequency component to center of spectrum |

`fix` | Round toward zero |

`fixed.interp1` (Fixed-Point Designer) | 1-D data interpolation (table lookup) |

`fixed.interp2` (Fixed-Point Designer) | Interpolation for 2-D gridded data in `meshgrid`
format |

`fixed.interp3` (Fixed-Point Designer) | Interpolation for 3-D gridded data in `meshgrid`
format |

`fixed.interpn` (Fixed-Point Designer) | Interpolation for 1-D, 2-D, 3-D, and N-D gridded data in
`ndgrid` format |

`floor` | Round toward negative infinity |

`fma` (Fixed-Point Designer) | Multiply and add using fused multiply add approach |

`hypot` | Square root of sum of squares (hypotenuse) |

`ifft` | Inverse fast Fourier transform |

`ifft2` | 2-D inverse fast Fourier transform |

`ifftn` | Multidimensional inverse fast Fourier transform |

`ifftshift` | Inverse zero-frequency shift |

`imag` | Imaginary part of complex number |

`ldivide` | Left array division |

`log` | Natural logarithm |

`log10` | Common logarithm (base 10) |

`log1p` | Compute natural logarithm of `1+X` accurately for small
`X` |

`log2` | Base 2 logarithm and floating-point number dissection |

`mean` | Average or mean value of array |

`minus` | Subtraction |

`mldivide` | Solve systems of linear equations Ax = B for
x |

`mod` | Remainder after division (modulo operation) |

`mrdivide` | Solve systems of linear equations xA = B for
x |

`mtimes` | Matrix multiplication |

`plus` | Add numbers, append strings |

`pow10` (Fixed-Point Designer) | Base 10 power and scale half-precision numbers |

`pow2` | Base 2 exponentiation and scaling of floating-point numbers |

`power` | Element-wise power |

`prod` | Product of array elements |

`rdivide` | Right array division |

`real` | Real part of complex number |

`rem` | Remainder after division |

`round` | Round to nearest decimal or integer |

`rsqrt` (Fixed-Point Designer) | Reciprocal square root |

`sign` | Sign function (signum function) |

`sin` | Sine of argument in radians |

`sinh` | Hyperbolic sine |

`sinpi` | Compute sin(X*pi) accurately |

`sqrt` | Square root |

`sum` | Sum of array elements |

`tan` | Tangent of argument in radians |

`tanh` | Hyperbolic tangent |

`times` | Multiplication |

`uminus` | Unary minus |

`uplus` | Unary plus |

### Data Types

`allfinite` | Determine if all array elements are finite |

`anynan` | Determine if any array element is `NaN` |

`cast` | Convert variable to different data type |

`cell` | Cell array |

`double` | Double-precision arrays |

`eps` | Floating-point relative accuracy |

`flintmax` | Largest consecutive integer in floating-point format |

`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 is instance of specified class |

`isfloat` (Fixed-Point Designer) | Determine whether input is floating-point data type |

`isinteger` | Determine whether input is integer array |

`islogical` | Determine if input is logical array |

`isnan` | Determine which array elements are NaN |

`isnumeric` | Determine whether input is numeric array |

`isobject` | Determine if input is MATLAB object |

`isreal` | Determine whether array uses complex storage |

`logical` | Convert numeric values to logicals |

`NaN` | Create array of all `NaN` values |

`realmax` | Largest positive floating-point number |

`realmin` | Smallest normalized floating-point number |

`single` | Single-precision arrays |

`storedInteger` (Fixed-Point Designer) | Stored integer value of `fi` object |

`typecast` | Convert data type without changing underlying data |

`uint16` | 16-bit unsigned integer arrays |

`uint32` | 32-bit unsigned integer arrays |

`uint64` | 64-bit unsigned integer arrays |

`uint8` | 8-bit unsigned integer arrays |

### Relational and Logical Operators

`all` | Determine if all array elements are nonzero or `true` |

`and` | Find logical AND |

```
Short-Circuit
AND
``` | Logical AND with short-circuiting |

`any` | Determine if any array elements are nonzero |

`eq` | Determine equality |

`ge` | Determine greater than or equal to |

`gt` | Determine greater than |

`isequal` | Determine array equality |

`isequaln` | Determine array equality, treating NaN values as equal |

`le` | Determine less than or equal to |

`lt` | Determine less than |

`ne` | Determine inequality |

`not` | Find logical NOT |

`or` | Find logical OR |

```
Short-Circuit
OR
``` | Logical OR with short-circuiting |

### Array and Matrix Operations

`cat` | Concatenate arrays |

`chol` | Cholesky factorization |

`circshift` | Shift array circularly |

`colon` | Vector creation, array subscripting, and `for` -loop
iteration |

`complex` | Create complex array |

`ctranspose` | Complex conjugate transpose |

`empty` | Create empty array of specified class |

`eye` | Identity matrix |

`flip` | Flip order of elements |

`fliplr` | Flip array left to right |

`flipud` | Flip array up to down |

`horzcat` | Horizontal concatenation for heterogeneous arrays |

`iscolumn` | Determine if 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 if input is row vector |

`isscalar` | Determine whether input is scalar |

`issorted` | Determine if array is sorted |

`isvector` | Determine whether input is vector |

`length` | Length of largest array dimension |

`lu` | LU matrix factorization |

`max` | Maximum elements of array |

`min` | Minimum elements of array |

`ndims` | Number of array dimensions |

`numel` | Number of array elements |

`ones` | Create array of all ones |

`permute` | Permute array dimensions |

`repelem` | Repeat copies of array elements |

`repmat` | Repeat copies of array |

`reshape` | Reshape array by rearranging existing elements |

`size` | Array size |

`sort` | Sort array elements |

`squeeze` | Remove dimensions of length 1 |

`transpose` | Transpose vector or matrix |

`vertcat` | Vertically concatenate for heterogeneous arrays |

`zeros` | Create array of all zeros |

### Graphics

`area` | Area of 2-D alpha shape |

`bar` | Bar graph |

`barh` | Horizontal bar graph |

`fplot` | Plot expression or function |

`line` | Create primitive line |

`plot` | 2-D line plot |

`plot3` | 3-D 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 |

### Deep Learning

`activations` (Deep Learning Toolbox) | (Not recommended) Compute deep learning network layer activations |

`classify` (Deep Learning Toolbox) | (Not recommended) Classify data using trained deep learning neural network |

`predict` (Deep Learning Toolbox) | Reconstruct the inputs using trained autoencoder |

`predictAndUpdateState` (Deep Learning Toolbox) | (Not recommended) Predict responses using a trained recurrent neural network and update the network state |

To display a list of supported functions, at the MATLAB Command Window, enter:

methods(half(1))

## Examples

### Convert Value to Half Precision

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

## Limitations

Arithmetic operations which combine half-precision and logical types are not supported.

For additional usage notes and limitations, see Half Precision Code Generation Support.

## Extended Capabilities

### C/C++ Code Generation

Generate C and C++ code using MATLAB® Coder™.

For a list of functions that support code generation with half-precision inputs and associated limitations, see Half Precision Code Generation Support.

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 performed in single-precision, regardless of the target hardware.For deep learning code generation, half inputs are cast to single precision and computations are performed in single precision.

In MATLAB, the

`isobject`

function returns true with a half-precision input. In generated code, this function returns false.

### GPU Code Generation

Generate CUDA® code for NVIDIA® GPUs using GPU Coder™.

For a list of functions that support code generation with half-precision inputs and associated limitations, see Half Precision Code Generation Support.

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 later is required for generating and executing code with half-precision data types.

You must set the memory allocation (

`malloc`

) mode to`'Discrete'`

for generating CUDA code.For more information, see

`coder.gpuConfig`

.Half-precision complex data types are not supported for GPU code generation.

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 performed in single-precision, regardless of the target hardware.For deep learning code generation, half inputs are cast to single precision and computations are performed in single precision. To perform computations in half, set the library target to

`'tensorrt'`

and set the data type to`'FP16'`

in`coder.DeepLearningConfig`

.In MATLAB, the

`isobject`

function returns true with a half-precision input. In generated code, this function returns false.

## Version History

**Introduced in R2018b**

## See Also

### Topics

- Half Precision Code Generation Support
- Floating-Point Numbers (Fixed-Point Designer)
- What Is Half Precision? (Fixed-Point Designer)
- Edge Detection with Sobel Method in Half-Precision

## 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)