Package 

Class Half

  • All Implemented Interfaces:
    kotlin.Comparable

    @JvmInline() 
    public final class Half
     implements Comparable<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
          |
          -- 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.

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

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      public class Half.Companion
    • Constructor Summary

      Constructors 
      Constructor Description
      Half(UShort v)
    • Method Summary

      Modifier and Type Method Description
      final Half getSign()
      final Integer getExponent()
      final Integer getSignificand()
      final Half getAbsoluteValue()
      final Half getUlp()
      final Integer toBits() Returns a bit representation of this half-precision floating point value as Int according to the IEEE 754 floating-point half-precision bit layout.
      final Byte toByte() Returns the value of this Half as a byte after a narrowing primitive conversion.
      final Short toShort() Returns the value of this Half as a short after a narrowing primitive conversion.
      final Integer toInt() Returns the value of this Half as a int after a narrowing primitive conversion.
      final Long toLong() Returns the value of this Half as a long after a narrowing primitive conversion.
      final Float toFloat() Returns the value of this Half as a float after a widening primitive conversion.
      final Double toDouble() Returns the value of this Half as a double after a widening primitive conversion.
      final Boolean isNaN() Returns true if this half-precision float value represents a Not-a-Number, false otherwise.
      final Boolean isInfinite() Returns true if this half-precision float value represents infinity, false otherwise.
      final Boolean isFinite() Returns true if this half-precision float value does not represent infinity nor NaN, false otherwise.
      final Boolean isZero() Returns true if this half-precision float value represents zero, false otherwise.
      final Boolean isNormalized() Returns true if this half-precision float value is normalized (does not have a subnormal representation).
      final <ERROR CLASS> withSign(Half sign) Returns this value with the sign bit same as of the sign value.
      final Half nextUp() Returns the Half value nearest to this value in direction of positive infinity.
      final Half nextDown() Returns the Half value nearest to this value in direction of negative infinity.
      final Half nextTowards(Half to) Returns the Half value nearest to this value in direction from this value towards the value to.
      final Integer roundToInt() Rounds this Half value to the nearest integer and converts the result to Int.
      final Long roundToLong() Rounds this Half value to the nearest integer and converts the result to Long.
      final <ERROR CLASS> unaryMinus()
      final Half unaryPlus()
      final Half plus(Half other)
      final Half minus(Half other)
      final Half times(Half other)
      final Half div(Half other)
      final Half inc()
      final Half dec()
      Integer compareTo(Half other)
      String toString()
      final String toHexString() Returns a hexadecimal string representation of the specified half-precision float value.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

    • Method Detail

      • toBits

         final Integer toBits()

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

      • toByte

         final Byte toByte()

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

      • toShort

         final Short toShort()

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

      • toInt

         final Integer toInt()

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

      • toLong

         final Long toLong()

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

      • isNaN

         final Boolean isNaN()

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

      • isInfinite

         final Boolean isInfinite()

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

      • isFinite

         final Boolean isFinite()

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

      • isZero

         final Boolean isZero()

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

      • withSign

         final <ERROR CLASS> withSign(Half sign)

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

      • nextUp

         final Half nextUp()

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

      • nextDown

         final Half nextDown()

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

      • nextTowards

         final Half nextTowards(Half to)

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

        Special cases:

        • x.nextTowards(y) is NaN if either x or y are NaN

        • x.nextTowards(x) == x

      • roundToInt

         final Integer roundToInt()

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

      • roundToLong

         final Long roundToLong()

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

      • toHexString

         final String toHexString()

        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:

        • If the sign is positive, no sign character appears in the result

        • If the sign is negative, the first character is '-'

        • If the value is inifinity, the string is "Infinity"

        • If the value is 0, the string is "0x0.0p0"

        • If the value has a normalized representation, the exponent and significand are represented in the string in two fields. The significand starts with "0x1." followed by its lowercase hexadecimal representation. Trailing zeroes are removed unless all digits are 0, then a single zero is used. The significand representation is followed by the exponent, represented by "p", itself followed by a decimal string of the unbiased exponent

        • If the value has a subnormal representation, the significand starts with "0x0." followed by its lowercase hexadecimal representation. Trailing zeroes are removed unless all digits are 0, then a single zero is used. The significand representation is followed by the exponent, represented by "p-14"