The FLOAT16_ENCODE function converts a scalar or array of 32-bit floating-point numbers into 16-bit "half-precision" floating-point values. Since IDL does not have a native 16-bit float data type, the result is encoded as 16-bit unsigned integers using the IEEE 754 binary16 (or float16) format, where the bits are:

seeeeeffffffffff

where "s" is the sign bit, "eeeee" is the 5-bit exponent (with an offset of 15), and the f's are 10 bits for the fractional (significand) part. The significand actually has 11 bits of precision because it has an implicit lead bit of 1 (unless the exponent bits are all zero).

Since IDL does not have a native 16-bit float data type, the input data must be of type Byte, Int, or Uint. For Byte input, it is assumed that each pair of bytes represents a single 16-bit binary16 number. For Int or Uint, it is assumed that each value represents the bits of the 16-bit binary16 number. In all cases, the bits will be decoded using the binary16 format, and then converted to 32-bit single-precision floats.

Note: Binary16 numbers have a limited range and precision, and are only suitable for certain applications. The smallest representable number is ±5.96e-08, while the largest is ±65504. Numbers smaller than ±5.96e-08 will be encoded as zero, while numbers larger than ±65504 will be encoded as ±Infinity (0x0x7c00 and 0xfc00). Not-a-number (NaN) values will be encoded as the quiet NaN 0x7fff.

Example


a = [0.00000, 5.96046e-8, -5.96046e-86.09756e-51, -165504, !values.f_infinity, -!values.f_infinity, !values.f_nan]
b = float16_encode(a)
help, b
print, b, format='(10z)'

IDL prints:

B    UINT  =  Array[10]
0   1   8001   3ff   3c00   bc00   7bff   7c00   fc00   7fff

Syntax


Result = FLOAT16_ENCODE( Value )

Return Value


Returns a 16-bit unsigned integer scalar value or array of the same dimensions as the Value.

Arguments


Value

Set this argument to a scalar or array of floating-point numbers. If the argument is not of type Float, it will be converted first to type Float.

Keywords


None

Version History


9.1

Introduced

See Also


FLOAT16_DECODE, READ_BINARY