-
- 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 | -- 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.
This table shows that numbers higher than 1024 lose all fractional precision.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description public classHalf.Companion
-
Field Summary
Fields Modifier and Type Field Description private final Halfsignprivate final Integerexponentprivate final Integersignificandprivate final HalfabsoluteValueprivate final Halfulp
-
Method Summary
Modifier and Type Method Description final HalfgetSign()final IntegergetExponent()final IntegergetSignificand()final HalfgetAbsoluteValue()final HalfgetUlp()final IntegertoBits()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 BytetoByte()Returns the value of this Half as a byteafter a narrowing primitive conversion.final ShorttoShort()Returns the value of this Half as a shortafter a narrowing primitive conversion.final IntegertoInt()Returns the value of this Half as a intafter a narrowing primitive conversion.final LongtoLong()Returns the value of this Half as a longafter a narrowing primitive conversion.final FloattoFloat()Returns the value of this Half as a floatafter a widening primitive conversion.final DoubletoDouble()Returns the value of this Half as a doubleafter a widening primitive conversion.final BooleanisNaN()Returns true if this half-precision float value represents a Not-a-Number, false otherwise. final BooleanisInfinite()Returns true if this half-precision float value represents infinity, false otherwise. final BooleanisFinite()Returns true if this half-precision float value does not represent infinity nor NaN, false otherwise. final BooleanisZero()Returns true if this half-precision float value represents zero, false otherwise. final BooleanisNormalized()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 HalfnextUp()Returns the Half value nearest to this value in direction of positive infinity. final HalfnextDown()Returns the Half value nearest to this value in direction of negative infinity. final HalfnextTowards(Half to)Returns the Half value nearest to this value in direction from this value towards the value to. final IntegerroundToInt()Rounds this Half value to the nearest integer and converts the result to Int. final LongroundToLong()Rounds this Half value to the nearest integer and converts the result to Long. final <ERROR CLASS>unaryMinus()final HalfunaryPlus()final Halfplus(Half other)final Halfminus(Half other)final Halftimes(Half other)final Halfdiv(Half other)final Halfinc()final Halfdec()IntegercompareTo(Half other)StringtoString()final StringtoHexString()Returns a hexadecimal string representation of the specified half-precision float value. -
-
Constructor Detail
-
Half
Half(UShort v)
-
-
Method Detail
-
getExponent
final Integer getExponent()
-
getSignificand
final Integer getSignificand()
-
getAbsoluteValue
final Half getAbsoluteValue()
-
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
byteafter a narrowing primitive conversion.
-
toShort
final Short toShort()
Returns the value of this Half as a
shortafter a narrowing primitive conversion.
-
toInt
final Integer toInt()
Returns the value of this Half as a
intafter a narrowing primitive conversion.
-
toLong
final Long toLong()
Returns the value of this Half as a
longafter a narrowing primitive conversion.
-
toFloat
final Float toFloat()
Returns the value of this Half as a
floatafter a widening primitive conversion.The following special cases are handled:
If the input is Half.POSITIVE_INFINITY or Half.NEGATIVE_INFINITY, the returned value is respectively Float.POSITIVE_INFINITY or Float.NEGATIVE_INFINITY
If the input is 0 (positive or negative), the returned value is +/-0.0f
Otherwise, the returned value is a normalized single-precision float value
-
toDouble
final Double toDouble()
Returns the value of this Half as a
doubleafter a widening primitive conversion.The following special cases are handled:
If the input is Half.NaN, the returned value is Double.NaN
If the input is Half.POSITIVE_INFINITY or Half.NEGATIVE_INFINITY, the returned value is respectively Double.POSITIVE_INFINITY or Double.NEGATIVE_INFINITY
If the input is 0 (positive or negative), the returned value is +/-0.0f
Otherwise, the returned value is a normalized double-precision float value
-
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.
-
isNormalized
final Boolean isNormalized()
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.
-
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)
-
roundToInt
final Integer roundToInt()
-
roundToLong
final Long roundToLong()
-
unaryMinus
final <ERROR CLASS> unaryMinus()
-
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 exponentIf 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"
-
-
-
-