Class Half
- All Implemented Interfaces:
Serializable,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.
To distinguish short values holding half-precision floats from regular short values,
it is recommended to use the @HalfFloat annotation.
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 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.
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intThe number of bytes used to represent a half-precision float value.static final shortEpsilon is the difference between 1.0 and the next value representable by a half-precision floating-point.static final shortSmallest negative value a half-precision float may have.static final intMaximum exponent a finite half-precision float may have.static final shortMaximum positive finite value a half-precision float may have.static final intMinimum exponent a normalized half-precision float may have.static final shortSmallest positive normal value a half-precision float may have.static final shortSmallest positive non-zero value a half-precision float may have.static final shortA Not-a-Number representation of a half-precision float.static final shortNegative infinity of type half-precision float.static final shortNegative 0 of type half-precision float.static final shortPositive infinity of type half-precision float.static final shortPositive 0 of type half-precision float.static final intThe number of bits used to represent a half-precision float value. -
Constructor Summary
ConstructorsConstructorDescriptionHalf(double value) Constructs a newly allocatedHalfobject that represents the argument converted to a half-precision float.Half(float value) Constructs a newly allocatedHalfobject that represents the argument converted to a half-precision float.Half(short value) Constructs a newly allocatedHalfobject that represents the half-precision float type argument.Constructs a newly allocatedHalfobject that represents the half-precision float value represented by the string. -
Method Summary
Modifier and TypeMethodDescriptionstatic shortabs(short h) Returns the absolute value of the specified half-precision float.byteReturns the value of thisHalfas abyteafter a narrowing primitive conversion.static shortceil(short h) Returns the smallest half-precision float value toward negative infinity greater than or equal to the specified half-precision float value.static intcompare(short x, short y) Compares the two specified half-precision float values.intCompares the two specified half-precision float values.static shortcopySign(short magnitude, short sign) Returns the first parameter with the sign of the second parameter.doubleReturns the value of thisHalfas adoubleafter a widening primitive conversion.static booleanequals(short x, short y) Returns true if the two half-precision float values are equal.booleanCompares this object against the specified object.floatReturns the value of thisHalfas afloatafter a widening primitive conversion.static shortfloor(short h) Returns the largest half-precision float value toward positive infinity less than or equal to the specified half-precision float value.static intgetExponent(short h) Returns the unbiased exponent used in the representation of the specified half-precision float value.static intgetSign(short h) Returns the sign of the specified half-precision float.static intgetSignificand(short h) Returns the significand, or mantissa, used in the representation of the specified half-precision float value.static booleangreater(short x, short y) Returns true if the first half-precision float value is greater (larger toward positive infinity) than the second half-precision float value.static booleangreaterEquals(short x, short y) Returns true if the first half-precision float value is greater (larger toward positive infinity) than or equal to the second half-precision float value.static inthalfToIntBits(short h) Returns a representation of the specified half-precision float value according to the bit layout described inHalf.static inthalfToRawIntBits(short h) Returns a representation of the specified half-precision float value according to the bit layout described inHalf.static shorthalfToShortBits(short h) Returns a representation of the specified half-precision float value according to the bit layout described inHalf.shortReturns the half-precision value of thisHalfas ashortcontaining the bit representation described inHalf.inthashCode()Returns a hash code for thisHalfobject.static inthashCode(short h) Returns a hash code for a half-precision float value.static shortintBitsToHalf(int bits) Returns the half-precision float value corresponding to a given bit representation.intintValue()Returns the value of thisHalfas aintafter a narrowing primitive conversion.static booleanisInfinite(short h) Returns true if the specified half-precision float value represents infinity, false otherwise.booleanisNaN()Returns true if thisHalfvalue represents a Not-a-Number, false otherwise.static booleanisNaN(short h) Returns true if the specified half-precision float value represents a Not-a-Number, false otherwise.static booleanisNormalized(short h) Returns true if the specified half-precision float value is normalized (does not have a subnormal representation).static booleanless(short x, short y) Returns true if the first half-precision float value is less (smaller toward negative infinity) than the second half-precision float value.static booleanlessEquals(short x, short y) Returns true if the first half-precision float value is less (smaller toward negative infinity) than or equal to the second half-precision float value.longReturns the value of thisHalfas alongafter a narrowing primitive conversion.static shortmax(short x, short y) Returns the larger of two half-precision float values (the value closest to positive infinity).static shortmin(short x, short y) Returns the smaller of two half-precision float values (the value closest to negative infinity).static shortReturns the half-precision float value represented by the specified string.static shortround(short h) Returns the closest integral half-precision float value to the specified half-precision float value.shortReturns the value of thisHalfas ashortafter a narrowing primitive conversion.static floattoFloat(short h) Converts the specified half-precision float value into a single-precision float value.static shorttoHalf(float f) Converts the specified single-precision float value into a half-precision float value.static StringtoHexString(short h) Returns a hexadecimal string representation of the specified half-precision float value.toString()Returns a string representation of the specified half-precision float value.static StringtoString(short h) Returns a string representation of the specified half-precision float value.static shorttrunc(short h) Returns the truncated half-precision float value of the specified half-precision float value.static HalfvalueOf(float f) Returns aHalfinstance representing the specified float value.static HalfvalueOf(short h) Returns aHalfinstance representing the specified half-precision float value.static HalfReturns aHalfinstance representing the specified string value.
-
Field Details
-
SIZE
public static final int SIZEThe number of bits used to represent a half-precision float value.- See Also:
-
BYTES
public static final int BYTESThe number of bytes used to represent a half-precision float value.- See Also:
-
EPSILON
Epsilon is the difference between 1.0 and the next value representable by a half-precision floating-point.- See Also:
-
MAX_EXPONENT
public static final int MAX_EXPONENTMaximum exponent a finite half-precision float may have.- See Also:
-
MIN_EXPONENT
public static final int MIN_EXPONENTMinimum exponent a normalized half-precision float may have.- See Also:
-
LOWEST_VALUE
Smallest negative value a half-precision float may have.- See Also:
-
MAX_VALUE
Maximum positive finite value a half-precision float may have.- See Also:
-
MIN_NORMAL
Smallest positive normal value a half-precision float may have.- See Also:
-
MIN_VALUE
Smallest positive non-zero value a half-precision float may have.- See Also:
-
NaN
A Not-a-Number representation of a half-precision float.- See Also:
-
NEGATIVE_INFINITY
Negative infinity of type half-precision float.- See Also:
-
NEGATIVE_ZERO
Negative 0 of type half-precision float.- See Also:
-
POSITIVE_INFINITY
Positive infinity of type half-precision float.- See Also:
-
POSITIVE_ZERO
Positive 0 of type half-precision float.- See Also:
-
-
Constructor Details
-
Half
Constructs a newly allocatedHalfobject that represents the half-precision float type argument.- Parameters:
value- The value to be represented by theHalf
-
Half
public Half(float value) Constructs a newly allocatedHalfobject that represents the argument converted to a half-precision float.- Parameters:
value- The value to be represented by theHalf- See Also:
-
Half
public Half(double value) Constructs a newly allocatedHalfobject that represents the argument converted to a half-precision float.- Parameters:
value- The value to be represented by theHalf- See Also:
-
Half
Constructs a newly allocated
Halfobject that represents the half-precision float value represented by the string. The string is converted to a half-precision float value as if by thevalueOf(String)method.Calling this constructor is equivalent to calling:
new Half(Float.parseFloat(value))- Parameters:
value- A string to be converted to aHalf- Throws:
NumberFormatException- if the string does not contain a parsable number- See Also:
-
-
Method Details
-
halfValue
Returns the half-precision value of thisHalfas ashortcontaining the bit representation described inHalf.- Returns:
- The half-precision float value represented by this object
-
byteValue
public byte byteValue()Returns the value of thisHalfas abyteafter a narrowing primitive conversion. -
shortValue
public short shortValue()Returns the value of thisHalfas ashortafter a narrowing primitive conversion.- Overrides:
shortValuein classNumber- Returns:
- The half-precision float value represented by this object
converted to type
short
-
intValue
public int intValue()Returns the value of thisHalfas aintafter a narrowing primitive conversion. -
longValue
public long longValue()Returns the value of thisHalfas alongafter a narrowing primitive conversion. -
floatValue
public float floatValue()Returns the value of thisHalfas afloatafter a widening primitive conversion.- Specified by:
floatValuein classNumber- Returns:
- The half-precision float value represented by this object
converted to type
float
-
doubleValue
public double doubleValue()Returns the value of thisHalfas adoubleafter a widening primitive conversion.- Specified by:
doubleValuein classNumber- Returns:
- The half-precision float value represented by this object
converted to type
double
-
isNaN
public boolean isNaN()Returns true if thisHalfvalue represents a Not-a-Number, false otherwise.- Returns:
- True if the value is a NaN, false otherwise
-
equals
Compares this object against the specified object. The result istrueif and only if the argument is notnulland is aHalfobject that represents the same half-precision value as the this object. Two half-precision values are considered to be the same if and only if the methodhalfToIntBits(short)returns an identicalintvalue for both. -
hashCode
public int hashCode()Returns a hash code for thisHalfobject. The result is the integer bit representation, exactly as produced by the methodhalfToIntBits(short), of the primitive half-precision float value represented by thisHalfobject. -
toString
Returns a string representation of the specified half-precision float value. SeetoString(short)for more information. -
compareTo
Compares the two specified half-precision float values. The following conditions apply during the comparison:
NaNis considered by this method to be equal to itself and greater than all other half-precision float values (including#POSITIVE_INFINITY)POSITIVE_ZEROis considered by this method to be greater thanNEGATIVE_ZERO.
- Specified by:
compareToin interfaceComparable<Half>- Parameters:
h- The half-precision float value to compare to the half-precision value represented by thisHalfobject- Returns:
- The value
0ifxis numerically equal toy; a value less than0ifxis numerically less thany; and a value greater than0ifxis numerically greater thany
-
hashCode
Returns a hash code for a half-precision float value.- Parameters:
h- The value to hash- Returns:
- A hash code value for a half-precision float value
-
compare
Compares the two specified half-precision float values. The following conditions apply during the comparison:
NaNis considered by this method to be equal to itself and greater than all other half-precision float values (including#POSITIVE_INFINITY)POSITIVE_ZEROis considered by this method to be greater thanNEGATIVE_ZERO.
- Parameters:
x- The first half-precision float value to compare.y- The second half-precision float value to compare- Returns:
- The value
0ifxis numerically equal toy, a value less than0ifxis numerically less thany, and a value greater than0ifxis numerically greater thany
-
halfToShortBits
Returns a representation of the specified half-precision float value according to the bit layout described in
Half.Similar to
halfToIntBits(short), this method collapses all possible Not-a-Number values to a single canonical Not-a-Number value defined byNaN.- Parameters:
h- A half-precision float value- Returns:
- The bits that represent the half-precision float value
- See Also:
-
halfToIntBits
Returns a representation of the specified half-precision float value according to the bit layout described in
Half.Unlike
halfToRawIntBits(short), this method collapses all possible Not-a-Number values to a single canonical Not-a-Number value defined byNaN.- Parameters:
h- A half-precision float value- Returns:
- The bits that represent the half-precision float value
- See Also:
-
halfToRawIntBits
Returns a representation of the specified half-precision float value according to the bit layout described in
Half.The argument is considered to be a representation of a half-precision float value according to the bit layout described in
Half. The 16 most significant bits of the returned value are set to 0.- Parameters:
h- A half-precision float value- Returns:
- The bits that represent the half-precision float value
- See Also:
-
intBitsToHalf
Returns the half-precision float value corresponding to a given bit representation.
The argument is considered to be a representation of a half-precision float value according to the bit layout described in
Half. The 16 most significant bits of the argument are ignored.- Parameters:
bits- An integer- Returns:
- The half-precision float value with the same bit pattern
-
copySign
Returns the first parameter with the sign of the second parameter. This method treats NaNs as having a sign.- Parameters:
magnitude- A half-precision float value providing the magnitude of the resultsign- A half-precision float value providing the sign of the result- Returns:
- A value with the magnitude of the first parameter and the sign of the second parameter
-
abs
Returns the absolute value of the specified half-precision float. Special values are handled in the following ways:- If the specified half-precision float is NaN, the result is NaN
- If the specified half-precision float is zero (negative or positive),
the result is positive zero (see
POSITIVE_ZERO) - If the specified half-precision float is infinity (negative or positive),
the result is positive infinity (see
POSITIVE_INFINITY)
- Parameters:
h- A half-precision float value- Returns:
- The absolute value of the specified half-precision float
-
round
Returns the closest integral half-precision float value to the specified half-precision float value. Special values are handled in the following ways:- If the specified half-precision float is NaN, the result is NaN
- If the specified half-precision float is infinity (negative or positive), the result is infinity (with the same sign)
- If the specified half-precision float is zero (negative or positive), the result is zero (with the same sign)
Note: Unlike the identically named
int java.lang.Math.round(float)method, this returns a Half value stored in a short, not an actual short integer result.- Parameters:
h- A half-precision float value- Returns:
- The value of the specified half-precision float rounded to the nearest half-precision float value
-
ceil
Returns the smallest half-precision float value toward negative infinity greater than or equal to the specified half-precision float value. Special values are handled in the following ways:- If the specified half-precision float is NaN, the result is NaN
- If the specified half-precision float is infinity (negative or positive), the result is infinity (with the same sign)
- If the specified half-precision float is zero (negative or positive), the result is zero (with the same sign)
- Parameters:
h- A half-precision float value- Returns:
- The smallest half-precision float value toward negative infinity greater than or equal to the specified half-precision float value
-
floor
Returns the largest half-precision float value toward positive infinity less than or equal to the specified half-precision float value. Special values are handled in the following ways:- If the specified half-precision float is NaN, the result is NaN
- If the specified half-precision float is infinity (negative or positive), the result is infinity (with the same sign)
- If the specified half-precision float is zero (negative or positive), the result is zero (with the same sign)
- Parameters:
h- A half-precision float value- Returns:
- The largest half-precision float value toward positive infinity less than or equal to the specified half-precision float value
-
trunc
Returns the truncated half-precision float value of the specified half-precision float value. Special values are handled in the following ways:- If the specified half-precision float is NaN, the result is NaN
- If the specified half-precision float is infinity (negative or positive), the result is infinity (with the same sign)
- If the specified half-precision float is zero (negative or positive), the result is zero (with the same sign)
- Parameters:
h- A half-precision float value- Returns:
- The truncated half-precision float value of the specified half-precision float value
-
min
Returns the smaller of two half-precision float values (the value closest to negative infinity). Special values are handled in the following ways:- If either value is NaN, the result is NaN
NEGATIVE_ZEROis smaller thanPOSITIVE_ZERO
- Parameters:
x- The first half-precision valuey- The second half-precision value- Returns:
- The smaller of the two specified half-precision values
-
max
Returns the larger of two half-precision float values (the value closest to positive infinity). Special values are handled in the following ways:- If either value is NaN, the result is NaN
POSITIVE_ZEROis greater thanNEGATIVE_ZERO
- Parameters:
x- The first half-precision valuey- The second half-precision value- Returns:
- The larger of the two specified half-precision values
-
less
Returns true if the first half-precision float value is less (smaller toward negative infinity) than the second half-precision float value. If either of the values is NaN, the result is false.- Parameters:
x- The first half-precision valuey- The second half-precision value- Returns:
- True if x is less than y, false otherwise
-
lessEquals
Returns true if the first half-precision float value is less (smaller toward negative infinity) than or equal to the second half-precision float value. If either of the values is NaN, the result is false.- Parameters:
x- The first half-precision valuey- The second half-precision value- Returns:
- True if x is less than or equal to y, false otherwise
-
greater
Returns true if the first half-precision float value is greater (larger toward positive infinity) than the second half-precision float value. If either of the values is NaN, the result is false.- Parameters:
x- The first half-precision valuey- The second half-precision value- Returns:
- True if x is greater than y, false otherwise
-
greaterEquals
Returns true if the first half-precision float value is greater (larger toward positive infinity) than or equal to the second half-precision float value. If either of the values is NaN, the result is false.- Parameters:
x- The first half-precision valuey- The second half-precision value- Returns:
- True if x is greater than y, false otherwise
-
equals
Returns true if the two half-precision float values are equal. If either of the values is NaN, the result is false.POSITIVE_ZEROandNEGATIVE_ZEROare considered equal.- Parameters:
x- The first half-precision valuey- The second half-precision value- Returns:
- True if x is equal to y, false otherwise
-
getSign
Returns the sign of the specified half-precision float.- Parameters:
h- A half-precision float value- Returns:
- 1 if the value is positive, -1 if the value is negative
-
getExponent
Returns the unbiased exponent used in the representation of the specified half-precision float value. if the value is NaN or infinite, this* method returnsMAX_EXPONENT+ 1. If the argument is 0 or a subnormal representation, this method returnsMIN_EXPONENT- 1.- Parameters:
h- A half-precision float value- Returns:
- The unbiased exponent of the specified value
-
getSignificand
Returns the significand, or mantissa, used in the representation of the specified half-precision float value.- Parameters:
h- A half-precision float value- Returns:
- The significand, or significand, of the specified vlaue
-
isInfinite
Returns true if the specified half-precision float value represents infinity, false otherwise.- Parameters:
h- A half-precision float value- Returns:
- True if the value is positive infinity or negative infinity, false otherwise
-
isNaN
Returns true if the specified half-precision float value represents a Not-a-Number, false otherwise.- Parameters:
h- A half-precision float value- Returns:
- True if the value is a NaN, false otherwise
-
isNormalized
Returns true if the specified half-precision float value is normalized (does not have a subnormal representation). If the specified value isPOSITIVE_INFINITY,NEGATIVE_INFINITY,POSITIVE_ZERO,NEGATIVE_ZERO, NaN or any subnormal number, this method returns false.- Parameters:
h- A half-precision float value- Returns:
- True if the value is normalized, false otherwise
-
toFloat
Converts the specified half-precision float value into a single-precision float value. The following special cases are handled:
- If the input is
NaN, the returned value isFloat.NaN - If the input is
POSITIVE_INFINITYorNEGATIVE_INFINITY, the returned value is respectivelyFloat.POSITIVE_INFINITYorFloat.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
- Parameters:
h- The half-precision float value to convert to single-precision- Returns:
- A normalized single-precision float value
- If the input is
-
toHalf
Converts the specified single-precision float value into a half-precision float value. The following special cases are handled:
- If the input is NaN (see
Float.isNaN(float)), the returned value isNaN - If the input is
Float.POSITIVE_INFINITYorFloat.NEGATIVE_INFINITY, the returned value is respectivelyPOSITIVE_INFINITYorNEGATIVE_INFINITY - If the input is 0 (positive or negative), the returned value is
POSITIVE_ZEROorNEGATIVE_ZERO - If the input is a less than
MIN_VALUE, the returned value is flushed toPOSITIVE_ZEROorNEGATIVE_ZERO - If the input is a less than
MIN_NORMAL, the returned value is a denorm half-precision float - Otherwise, the returned value is rounded to the nearest representable half-precision float value
- Parameters:
f- The single-precision float value to convert to half-precision- Returns:
- A half-precision float value
- If the input is NaN (see
-
valueOf
Returns aHalfinstance representing the specified half-precision float value.- Parameters:
h- A half-precision float value- Returns:
- a
Halfinstance representingh
-
valueOf
Returns aHalfinstance representing the specified float value.- Parameters:
f- A float value- Returns:
- a
Halfinstance representingf
-
valueOf
Returns aHalfinstance representing the specified string value. Calling this method is equivalent to callingtoHalf(Float.parseString(h)). SeeFloat.valueOf(String)for more information on the format of the string representation.- Parameters:
s- The string to be parsed- Returns:
- a
Halfinstance representingh - Throws:
NumberFormatException- if the string does not contain a parsable half-precision float value
-
parseHalf
Returns the half-precision float value represented by the specified string. Calling this method is equivalent to callingtoHalf(Float.parseString(h)). SeeFloat.valueOf(String)for more information on the format of the string representation.- Parameters:
s- The string to be parsed- Returns:
- A half-precision float value represented by the string
- Throws:
NumberFormatException- if the string does not contain a parsable half-precision float value
-
toString
Returns a string representation of the specified half-precision float value. Calling this method is equivalent to callingFloat.toString(toFloat(h)). SeeFloat.toString(float)for more information on the format of the string representation.- Parameters:
h- A half-precision float value- Returns:
- A string representation of the specified value
-
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"
- Parameters:
h- A half-precision float value- Returns:
- A hexadecimal string representation of the specified value
-