Half

value class Half(v: UShort) : Comparable<Half> (source)

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
|
-- exponent

Half-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 startPrecision
01 ⁄ 16,777,216
1 ⁄ 16,3841 ⁄ 16,777,216
1 ⁄ 8,1921 ⁄ 8,388,608
1 ⁄ 4,0961 ⁄ 4,194,304
1 ⁄ 2,0481 ⁄ 2,097,152
1 ⁄ 1,0241 ⁄ 1,048,576
1 ⁄ 5121 ⁄ 524,288
1 ⁄ 2561 ⁄ 262,144
1 ⁄ 1281 ⁄ 131,072
1 ⁄ 641 ⁄ 65,536
1 ⁄ 321 ⁄ 32,768
1 ⁄ 161 ⁄ 16,384
1 ⁄ 81 ⁄ 8,192
1 ⁄ 41 ⁄ 4,096
1 ⁄ 21 ⁄ 2,048
11 ⁄ 1,024
21 ⁄ 512
41 ⁄ 256
81 ⁄ 128
161 ⁄ 64
321 ⁄ 32
641 ⁄ 16
1281 ⁄ 8
2561 ⁄ 4
5121 ⁄ 2
1,0241
2,0482
4,0964
8,1928
16,38416
32,76832

This table shows that numbers higher than 1024 lose all fractional precision.

Constructors

Link copied to clipboard
fun Half(v: UShort)

Types

Link copied to clipboard
object Companion

Functions

Link copied to clipboard
open operator override fun compareTo(other: Half): Int
Link copied to clipboard
operator fun dec(): Half
Link copied to clipboard
operator fun div(other: Half): Half
Link copied to clipboard
operator fun inc(): Half
Link copied to clipboard

Returns true if this half-precision float value does not represent infinity nor NaN, false otherwise.

Link copied to clipboard

Returns true if this half-precision float value represents infinity, false otherwise.

Link copied to clipboard
fun isNaN(): Boolean

Returns true if this half-precision float value represents a Not-a-Number, false otherwise.

Link copied to clipboard

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.

Link copied to clipboard

Returns true if this half-precision float value represents zero, false otherwise.

Link copied to clipboard
operator fun minus(other: Half): Half
Link copied to clipboard
fun nextDown(): Half

Returns the Half value nearest to this value in direction of negative infinity.

Link copied to clipboard

Returns the Half value nearest to this value in direction from this value towards the value to.

Link copied to clipboard
fun nextUp(): Half

Returns the Half value nearest to this value in direction of positive infinity.

Link copied to clipboard
operator fun plus(other: Half): Half
Link copied to clipboard

Rounds this Half value to the nearest integer and converts the result to Int. Ties are rounded towards positive infinity.

Link copied to clipboard

Rounds this Half value to the nearest integer and converts the result to Long. Ties are rounded towards positive infinity.

Link copied to clipboard
operator fun times(other: Half): Half
Link copied to clipboard
fun toBits(): Int

Returns a bit representation of this half-precision floating point value as Int according to the IEEE 754 floating-point half-precision bit layout.

Link copied to clipboard
fun toByte(): Byte

Returns the value of this Half as a byte after a narrowing primitive conversion.

Link copied to clipboard

Returns the value of this Half as a double after a widening primitive conversion.

Link copied to clipboard
fun toFloat(): Float

Returns the value of this Half as a float after a widening primitive conversion.

Link copied to clipboard

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:

Link copied to clipboard
fun toInt(): Int

Returns the value of this Half as a int after a narrowing primitive conversion.

Link copied to clipboard
fun toLong(): Long

Returns the value of this Half as a long after a narrowing primitive conversion.

Link copied to clipboard
fun toShort(): Short

Returns the value of this Half as a short after a narrowing primitive conversion.

Link copied to clipboard
open override fun toString(): String
Link copied to clipboard
operator fun unaryMinus(): Half
Link copied to clipboard
operator fun unaryPlus(): Half
Link copied to clipboard
fun withSign(sign: Half): Half

Returns this value with the sign bit same as of the sign value. If sign is NaN the sign of the result is undefined.

Properties

Link copied to clipboard

Returns the absolute value of this half-precision float. Special values are handled in the following ways:

Link copied to clipboard

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.

Link copied to clipboard
val sign: Half

Returns the sign of this half-precision float value:

Link copied to clipboard

Returns the significand, or mantissa, used in the representation of this half-precision float value.

Link copied to clipboard
val ulp: Half

Returns the ulp of this value. An ulp is a positive distance between this value and the next nearest Half value larger in magnitude.

Extensions

Link copied to clipboard
inline operator fun Half.div(v: Half3): Half3
inline operator fun Half.div(v: Half4): Half4
Link copied to clipboard
inline operator fun Half.minus(v: Half3): Half3
inline operator fun Half.minus(v: Half4): Half4
Link copied to clipboard
inline operator fun Half.plus(v: Half3): Half3
inline operator fun Half.plus(v: Half4): Half4
Link copied to clipboard
inline operator fun Half.times(v: Half3): Half3
inline operator fun Half.times(v: Half4): Half4