Half
The Half class is a wrapper and a utility class to manipulate half-precision 16-bit IEEE 754 floating point data types (also called fp16 or binary16). A half-precision float can be created from or converted to single-precision floats, and is stored in a short data type.
The IEEE 754 standard specifies an fp16 as having the following format:
Sign bit: 1 bit
Exponent width: 5 bits
Significand: 10 bits
The format is laid out as follows:
1 11111 1111111111
^ --^-- -----^----
sign | |_______ significand
|
-- exponentHalf-precision floating points can be useful to save memory and/or bandwidth at the expense of range and precision when compared to single-precision floating points (fp32).
To help you decide whether fp16 is the right storage type for you need, please refer to the table below that shows the available precision throughout the range of possible values. The precision column indicates the step size between two consecutive numbers in a specific part of the range.
| Range start | Precision |
|---|---|
| 0 | 1 ⁄ 16,777,216 |
| 1 ⁄ 16,384 | 1 ⁄ 16,777,216 |
| 1 ⁄ 8,192 | 1 ⁄ 8,388,608 |
| 1 ⁄ 4,096 | 1 ⁄ 4,194,304 |
| 1 ⁄ 2,048 | 1 ⁄ 2,097,152 |
| 1 ⁄ 1,024 | 1 ⁄ 1,048,576 |
| 1 ⁄ 512 | 1 ⁄ 524,288 |
| 1 ⁄ 256 | 1 ⁄ 262,144 |
| 1 ⁄ 128 | 1 ⁄ 131,072 |
| 1 ⁄ 64 | 1 ⁄ 65,536 |
| 1 ⁄ 32 | 1 ⁄ 32,768 |
| 1 ⁄ 16 | 1 ⁄ 16,384 |
| 1 ⁄ 8 | 1 ⁄ 8,192 |
| 1 ⁄ 4 | 1 ⁄ 4,096 |
| 1 ⁄ 2 | 1 ⁄ 2,048 |
| 1 | 1 ⁄ 1,024 |
| 2 | 1 ⁄ 512 |
| 4 | 1 ⁄ 256 |
| 8 | 1 ⁄ 128 |
| 16 | 1 ⁄ 64 |
| 32 | 1 ⁄ 32 |
| 64 | 1 ⁄ 16 |
| 128 | 1 ⁄ 8 |
| 256 | 1 ⁄ 4 |
| 512 | 1 ⁄ 2 |
| 1,024 | 1 |
| 2,048 | 2 |
| 4,096 | 4 |
| 8,192 | 8 |
| 16,384 | 16 |
| 32,768 | 32 |
This table shows that numbers higher than 1024 lose all fractional precision.
Constructors
Types
Functions
Returns true if this half-precision float value represents infinity, false otherwise.
Returns true if this half-precision float value is normalized (does not have a subnormal representation). If this value is Half.POSITIVE_INFINITY, Half.NEGATIVE_INFINITY, Half.POSITIVE_ZERO, Half.NEGATIVE_ZERO, NaN or any subnormal number, this method returns false.
Returns a hexadecimal string representation of the specified half-precision float value. If the value is a NaN, the result is "NaN", otherwise the result follows this format:
Properties
Returns the unbiased exponent used in the representation of this half-precision float value. if the value is NaN or infinite, this method returns MAX_EXPONENT + 1. If the argument is 0 or a subnormal representation, this method returns MIN_EXPONENT - 1.