Class Half
- All Implemented Interfaces:
Serializable,Comparable<Half>
Half class implements half precision (FP16) float-point number according to IEEE 754 standard.
In addition, this class provides several methods for converting a Half to a String and a
String to a Half, as well as other constants and methods useful when dealing with a Half.
Half is implemented to provide, as much as possible, the same interface as Float and Double.
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intThe number of bytes used to represent aHalfvalue.static final intMaximum exponent a finiteHalfvariable may have.static final HalfA constant holding the largest positive finite value of typeHalf, (2-2-10)·215.static final intMinimum exponent a normalizedHalfvariable may have.static final HalfA constant holding the smallest positive normal value of typeHalf, 2-14.static final HalfA constant holding the smallest positive nonzero value of typeHalf, 2-24.static final HalfA constant holding a Not-a-Number (NaN) value of typeHalf.static final HalfA constant holding the negative infinity of typeHalf.static final HalfA constant holding the largest positive finite value of typeHalf, (2-2-10)·215.static final HalfA constant holding the negative zero of typeHalf.static final HalfA constant holding the positive infinity of typeHalf.static final HalfA constant holding the positive zero of typeHalf.static final intThe number of bits used to represent aHalfvalue. -
Method Summary
Modifier and TypeMethodDescriptionbyteReturns the value of the specified number as abyte.static intCompares the two specifiedHalfobjects.intdoublebooleanstatic shortfloatToHalfShortBits(float floatValue) static shortfloatToShortBitsCollapseNaN(float floatRepresentation) Returns a representation of the specified floating-point value according to the IEEE 754 floating-point "single format" bit layout.floatstatic floathalfShortToFloat(short shortBits) static shorthalfToRawShortBits(Half half) Returns a representation of the specified floating-point value according to the IEEE 754 floating-point "single format" bit layout, preserving Not-a-Number (NaN) values.static shorthalfToShortBits(Half half) Returns a representation of the specified floating-point value according to the IEEE 754 floating-point "single format" bit layout.inthashCode()Returns a hash code for thisHalfobject.static intReturns a hash code for aHalf; compatible withHalf.hashCode().intintValue()booleanisFinite()Returnstrueif the argument is a finite floating-point value; returnsfalseotherwise (for NaN and infinity arguments).static booleanReturnstrueif the argument is a finite floating-point value; returnsfalseotherwise (for NaN and infinity arguments).booleanReturnstrueif thisHalfvalue is infinitely large in magnitude,falseotherwise.static booleanisInfinite(Half half) Returnstrueif the specifiedHalfis infinitely large in magnitude,falseotherwise.booleanisNaN()Returnstrueif thisHalfvalue is a Not-a-Number (NaN),falseotherwise.static booleanReturnstrueif the specified number is a Not-a-Number (NaN) value,falseotherwise.longstatic HalfReturns the greater of twoHalfobjects.
Determined usingaFloatValue = a.floatValue()andbFloatValue = b.floatValue()then callingFloat.max(aFloatValue, bFloatValue).static HalfReturns the smaller of twoHalfobjects.
Determined usingaFloatValue = a.floatValue()andbFloatValue = b.floatValue()then callingFloat.min(aFloatValue, bFloatValue).static floatquantizeFloat(float floatValue) static HalfshortBitsToHalf(short shortBits) Returns theHalfobject corresponding to a given bit representation.shortReturns the value of the specified number as ashort.static HalfAdds twoHalfvalues together as per the + operator.static StringtoHexString(Half half) Returns a hexadecimal string representation of thehalfargument.toString()static StringReturns a string representation of thehalfargument.static HalfvalueOf(double doubleValue) Returns aHalfinstance representing the specifieddoublevalue.static HalfvalueOf(float floatValue) Returns aHalfinstance representing the specifiedfloatvalue.static HalfReturns a newHalfinstance identical to the specifiedhalf.static HalfReturns aHalfinstance representing the specifiedDoublevalue.static HalfReturns aHalfinstance representing the specifiedFloatvalue.static HalfReturns aHalfobject represented by the argument strings.
-
Field Details
-
POSITIVE_INFINITY
A constant holding the positive infinity of typeHalf.It is equal to the value returned by
shortBitsToHalf((short)0x7c00). -
NEGATIVE_INFINITY
A constant holding the negative infinity of typeHalf.It is equal to the value returned by
shortBitsToHalf((short)0xfc00). -
NaN
A constant holding a Not-a-Number (NaN) value of typeHalf.It is equivalent to the value returned by
shortBitsToHalf((short)0x7e00). -
MAX_VALUE
A constant holding the largest positive finite value of typeHalf, (2-2-10)·215.It is equal to
shortBitsToHalf((short)0x7bff). -
NEGATIVE_MAX_VALUE
A constant holding the largest positive finite value of typeHalf, (2-2-10)·215.It is equal to
shortBitsToHalf((short)0xfbff). -
MIN_NORMAL
A constant holding the smallest positive normal value of typeHalf, 2-14.It is equal to
shortBitsToHalf((short)0x0400). -
MIN_VALUE
A constant holding the smallest positive nonzero value of typeHalf, 2-24.It is equal to
shortBitsToHalf((short)0x1). -
MAX_EXPONENT
public static final int MAX_EXPONENTMaximum exponent a finiteHalfvariable may have.It is equal to the value returned by
HalfMath.getExponent(Half.MAX_VALUE).- See Also:
-
MIN_EXPONENT
public static final int MIN_EXPONENTMinimum exponent a normalizedHalfvariable may have.It is equal to the value returned by
HalfMath.getExponent(Half.MIN_NORMAL).- See Also:
-
SIZE
public static final int SIZEThe number of bits used to represent aHalfvalue.- See Also:
-
BYTES
public static final int BYTESThe number of bytes used to represent aHalfvalue.- See Also:
-
POSITIVE_ZERO
A constant holding the positive zero of typeHalf.It is equal to the value returned by
shortBitsToHalf((short)0x0). -
NEGATIVE_ZERO
A constant holding the negative zero of typeHalf.It is equal to the value returned by
shortBitsToHalf((short)0x8000).
-
-
Method Details
-
shortBitsToHalf
Returns theHalfobject corresponding to a given bit representation. The argument is considered to be a representation of a floating-point value according to the IEEE 754 floating-point "half format" bit layout.If the argument is
0x7c00, the result is positive infinity.If the argument is
0xfc00, the result is negative infinity.If the argument is any value in the range {0x7c01} through
0x7fffor in the range0xfc01through0xffff, the result is a NaN. No IEEE 754 floating-point operation provided by Java can distinguish between two NaN values of the same type with different bit patterns. Distinct values of NaN are only distinguishable by use of theHalf.halfToRawShortBitsmethod.In all other cases, let s, e, and m be three values that can be computed from the argument:
Then the float-point result equals the value of the mathematical expression s·m·2e-25.{ @code int s = ((bits >> 16) == 0) ? 1 : -1; int e = ((bits >> 10) & 0x1f); int m = (e == 0) ? (bits & 0x3ff) << 1 : (bits & 0x3ff) | 0x200; }Note that this method may not be able to return a
HalfNaN with exactly same bit pattern as theshortargument. IEEE 754 distinguishes between two kinds of NaNs, quiet NaNs and signaling NaNs. The differences between the two kinds of NaN are generally not visible in Java. Arithmetic operations on signaling NaNs turn them into quiet NaNs with a different, but often similar, bit pattern. However, on some processors merely copying a signaling NaN also performs that conversion. In particular, copying a signaling NaN to return it to the calling method may perform this conversion. SoshortBitsToHalfmay not be able to return aHalfwith a signaling NaN bit pattern. Consequently, for someshortvalues,halfToRawShortBits(shortBitsToHalf(start))may not equalstart. Moreover, which particular bit patterns represent signaling NaNs is platform dependent; although all NaN bit patterns, quiet or signaling, must be in the NaN range identified above.- Parameters:
shortBits- a short.- Returns:
- the
Halffloat-point object with the same bit pattern.
-
halfShortToFloat
public static float halfShortToFloat(short shortBits) -
halfToShortBits
Returns a representation of the specified floating-point value according to the IEEE 754 floating-point "single format" bit layout.Bit 15 (the bit that is selected by the mask
0x8000) represents the sign of the floating-point number. Bits 14-10 (the bits that are selected by the mask0x7c00) represent the exponent. Bits 9-0 (the bits that are selected by the mask0x03ff) represent the significand (sometimes called the mantissa) of the floating-point number.If the argument is positive infinity, the result is
0x7c00.If the argument is negative infinity, the result is
0xfc00.If the argument is NaN, the result is
0x7e00.In all cases, the result is a short that, when given to the
shortBitsToHalf(short)method, will produce a floating-point value the same as the argument tohalfToShortBits(except all NaN values are collapsed to a single "canonical" NaN value).- Parameters:
half- a Half object.- Returns:
- the bits that represent the floating-point number.
-
floatToShortBitsCollapseNaN
public static short floatToShortBitsCollapseNaN(float floatRepresentation) Returns a representation of the specified floating-point value according to the IEEE 754 floating-point "single format" bit layout.Bit 15 (the bit that is selected by the mask
0x8000) represents the sign of the floating-point number. Bits 14-10 (the bits that are selected by the mask0x7c00) represent the exponent. Bits 9-0 (the bits that are selected by the mask0x03ff) represent the significand (sometimes called the mantissa) of the floating-point number.If the argument is positive infinity, the result is
0x7c00.If the argument is negative infinity, the result is
0xfc00.If the argument is NaN, the result is
0x7e00.In all cases, the result is a short that, when given to the
shortBitsToHalf(short)method, will produce a floating-point value the same as the argument tohalfToShortBits(except all NaN values are collapsed to a single "canonical" NaN value).- Parameters:
floatRepresentation- a float representation as used within aHalfobject.- Returns:
- the bits that represent the floating-point number.
-
halfToRawShortBits
Returns a representation of the specified floating-point value according to the IEEE 754 floating-point "single format" bit layout, preserving Not-a-Number (NaN) values.Bit 15 (the bit that is selected by the mask
0x8000) represents the sign of the floating-point number. Bits 14-10 (the bits that are selected by the mask0x7c00) represent the exponent. Bits 9-0 (the bits that are selected by the mask0x03ff) represent the significand (sometimes called the mantissa) of the floating-point number.If the argument is positive infinity, the result is
0x7c00.If the argument is negative infinity, the result is
0xfc00.If the argument is NaN, the result is the integer representing the actual NaN value. Unlike the
halfToShortBitsmethod,halfToRawShortBitsdoes not collapse all the bit patterns encoding a NaN to a single "canonical" NaN value.In all cases, the result is a short that, when given to the
shortBitsToHalf(short)method, will produce a floating-point value the same as the argument tohalfToRawShortBits.- Parameters:
half- a Half object.- Returns:
- the bits that represent the half-point number.
-
floatToHalfShortBits
public static short floatToHalfShortBits(float floatValue) -
shortValue
public short shortValue()Returns the value of the specified number as ashort.- Overrides:
shortValuein classNumber- Returns:
- the numeric value represented by this object after conversion to type
short.
-
intValue
public int intValue() -
longValue
public long longValue() -
floatValue
public float floatValue()- Specified by:
floatValuein classNumber
-
doubleValue
public double doubleValue()- Specified by:
doubleValuein classNumber
-
byteValue
public byte byteValue()Returns the value of the specified number as abyte. -
valueOf
Returns aHalfobject represented by the argument strings.If
sisnull, then aNullPointerExceptionis thrown.Leading and trailing whitespace characters in
sare ignored. Whitespace is removed as if by theString.trim()method; that is, both ASCII space and control characters are removed. The rest ofsshould constitute a FloatValue as described by the lexical syntax rules:
where Sign, FloatingPointLiteral, HexNumeral, HexDigits, SignedInteger and FloatTypeSuffix are as defined in the lexical structure sections of The Java Language Specification, except that underscores are not accepted between digits. If- FloatValue:
- Signopt
NaN- Signopt
Infinity- Signopt FloatingPointLiteral
- Signopt HexFloatingPointLiteral
- SignedInteger
- Signopt
- HexFloatingPointLiteral:
- HexSignificand BinaryExponent FloatTypeSuffixopt
- HexSignificand:
- HexNumeral
- HexNumeral
.0xHexDigitsopt.HexDigits0XHexDigitsopt.HexDigits - HexNumeral
- BinaryExponent:
- BinaryExponentIndicator SignedInteger
- BinaryExponentIndicator:
pP
sdoes not have the form of a FloatValue, then aNumberFormatExceptionis thrown. Otherwise,sis regarded as representing an exact decimal value in the usual "computerized scientific notation" or as an exact hexadecimal value; this exact numerical value is then conceptually converted to an "infinitely precise" binary value that is then rounded to typehalfby the usual round-to-nearest rule of IEEE 754 floating-point arithmetic, which includes preserving the sign of a zero value. Note that the round-to-nearest rule also implies overflow and underflow behaviour; if the exact value ofsis large enough in magnitude (greater than or equal to (MAX_VALUE+HalfMath.ulp(MAX_VALUE)/2), rounding tofloatwill result in an infinity and if the exact value ofsis small enough in magnitude (less than or equal toMIN_VALUE/2), rounding to float will result in a zero. Finally, after rounding aHalfobject is returned.To interpret localized string representations of a floating-point value, use subclasses of
NumberFormat.To avoid calling this method on an invalid string and having a
NumberFormatExceptionbe thrown, the documentation forDouble.valueOflists a regular expression which can be used to screen the input.- Parameters:
s- the string to be parsed.- Returns:
- a
Halfobject holding the value represented by theStringargument. - Throws:
NumberFormatException- if the string does not contain a parsable number.
-
valueOf
Returns aHalfinstance representing the specifieddoublevalue.- Parameters:
doubleValue- a double value.- Returns:
- a
Halfinstance representingdoubleValue.
-
valueOf
Returns aHalfinstance representing the specifiedDoublevalue.- Parameters:
doubleValue- a double value.- Returns:
- a
Halfinstance representingdoubleValue.
-
valueOf
Returns aHalfinstance representing the specifiedfloatvalue.- Parameters:
floatValue- a float value.- Returns:
- a
Halfinstance representingfloatValue.
-
quantizeFloat
public static float quantizeFloat(float floatValue) -
valueOf
Returns aHalfinstance representing the specifiedFloatvalue.- Parameters:
floatValue- a float value.- Returns:
- a
Halfinstance representingfloatValue.
-
valueOf
Returns a newHalfinstance identical to the specifiedhalf.- Parameters:
half- a half instance.- Returns:
- a
Halfinstance representingdoubleValue.
-
isNaN
Returnstrueif the specified number is a Not-a-Number (NaN) value,falseotherwise.- Parameters:
half- theHalfto be tested.- Returns:
trueif the argument is NaN;falseotherwise.
-
isNaN
public boolean isNaN()Returnstrueif thisHalfvalue is a Not-a-Number (NaN),falseotherwise.- Returns:
trueif the value represented by this object is NaN;falseotherwise.
-
isInfinite
Returnstrueif the specifiedHalfis infinitely large in magnitude,falseotherwise.- Parameters:
half- theHalfto be tested.- Returns:
trueif the argument is positive infinity or negative infinity;falseotherwise.
-
isInfinite
public boolean isInfinite()Returnstrueif thisHalfvalue is infinitely large in magnitude,falseotherwise.- Returns:
trueif the value represented by this object is positive infinity or negative infinity;falseotherwise.
-
isFinite
Returnstrueif the argument is a finite floating-point value; returnsfalseotherwise (for NaN and infinity arguments).- Parameters:
half- theHalfto be tested- Returns:
trueif the argument is a finite floating-point value,falseotherwise.
-
isFinite
public boolean isFinite()Returnstrueif the argument is a finite floating-point value; returnsfalseotherwise (for NaN and infinity arguments).- Returns:
trueif the argument is a finite floating-point value,falseotherwise.
-
toString
Returns a string representation of thehalfargument. All characters mentioned below are ASCII characters.- If the argument is NaN, the result is the string "
NaN". - Otherwise, the result is a string that represents the sign and magnitude (absolute value) of the argument. If
the sign is negative, the first character of the result is '
-' ('\u002D'); if the sign is positive, no sign character appears in the result. As for the magnitude m:- If m is infinity, it is represented by the characters
"Infinity"; thus, positive infinity produces the result"Infinity"and negative infinity produces the result"-Infinity". - If m is zero, it is represented by the characters
"0.0"; thus, negative zero produces the result"-0.0"and positive zero produces the result"0.0". - If m is greater than or equal to 10-3 but less than 107, then it is represented
as the integer part of m, in decimal form with no leading zeroes, followed by '
.' ('\u002E'), followed by one or more decimal digits representing the fractional part of m. - If m is less than 10-3 or greater than or equal to 107, then it is represented
in so-called "computerized scientific notation." Let n be the unique integer such that 10n
≤ m < 10n+1; then let a be the mathematically exact quotient
of m and 10n so that 1 ≤ a < 10. The magnitude is then represented
as the integer part of a, as a single decimal digit, followed by '
.' ('\u002E'), followed by decimal digits representing the fractional part of a, followed by the letter 'E' ('\u0045'), followed by a representation of n as a decimal integer, as produced by the methodInteger.toString(int).
- If m is infinity, it is represented by the characters
Float.toString(floatValue)using results offloatValue()call usinghalfinstance.To create localized string representations of a floating-point value, use subclasses of
NumberFormat.- Parameters:
half- the Half to be converted.- Returns:
- a string representation of the argument.
- If the argument is NaN, the result is the string "
-
toString
-
toHexString
Returns a hexadecimal string representation of thehalfargument. All characters mentioned below are ASCII characters.- If the argument is NaN, the result is the string "
NaN". - Otherwise, the result is a string that represents the sign and magnitude (absolute value) of the argument. If
the sign is negative, the first character of the result is '
-' ('\u002D'); if the sign is positive, no sign character appears in the result. As for the magnitude m:- If m is infinity, it is represented by the string
"Infinity"; thus, positive infinity produces the result"Infinity"and negative infinity produces the result"-Infinity". - If m is zero, it is represented by the string
"0x0.0p0"; thus, negative zero produces the result"-0x0.0p0"and positive zero produces the result"0x0.0p0". - If m is a
halfwith a normalized representation, substrings are used to represent the significand and exponent fields. The significand is represented by the characters"0x1."followed by a lowercase hexadecimal representation of the rest of the significand as a fraction. Trailing zeros in the hexadecimal representation are removed unless all the digits are zero, in which case a single zero is used. Next, the exponent is represented by"p"followed by a decimal string of the unbiased exponent as if produced by a call toInteger.toStringon the exponent value. - If m is a
halfwith a subnormal representation, the significand is represented by the characters"0x0."followed by a hexadecimal representation of the rest of the significand as a fraction. Trailing zeros in the hexadecimal representation are removed. Next, the exponent is represented by"p-14". Note that there must be at least one nonzero digit in a subnormal significand.
- If m is infinity, it is represented by the string
Examples Floating-point Value Hexadecimal String 1.00x1.0p0-1.0-0x1.0p02.00x1.0p13.00x1.8p10.50x1.0p-10.250x1.0p-2Float.MAX_VALUE0x1.ffcp15Minimum Normal Value0x1.0p-14Maximum Subnormal Value0x0.ffcp-14Float.MIN_VALUE0x0.004p-14- Parameters:
half- theHalfto be converted.- Returns:
- a hex string representation of the argument.
- If the argument is NaN, the result is the string "
-
equals
-
hashCode
Returns a hash code for aHalf; compatible withHalf.hashCode().- Parameters:
half- theHalfto hash- Returns:
- a hash code value for a
Halfvalue.
-
hashCode
public int hashCode()Returns a hash code for thisHalfobject. The result is the short bit representation, exactly as produced by the methodhalfToShortBits(Half)represented by thisHalfobject. -
compare
Compares the two specifiedHalfobjects. The sign of the integer value returned is the same as that of the integer that would be returned by the call:half1.compareTo(half2)
- Parameters:
half1- the firstHalfto compare.half2- the secondHalfto compare.- Returns:
- the value
0ifhalf1is numerically equal tohalf2; a value less than0ifhalf1is numerically less thanhalf2; and a value greater than0ifhalf1is numerically greater thanhalf2.
-
compareTo
- Specified by:
compareToin interfaceComparable<Half>
-
sum
Adds twoHalfvalues together as per the + operator.- Parameters:
a- the first operandb- the second operand- Returns:
- the sum of
aandb - See Also:
-
max
Returns the greater of twoHalfobjects.
Determined usingaFloatValue = a.floatValue()andbFloatValue = b.floatValue()then callingFloat.max(aFloatValue, bFloatValue).- Parameters:
a- the first operandb- the second operand- Returns:
- the greater of
aandb - See Also:
-
min
Returns the smaller of twoHalfobjects.
Determined usingaFloatValue = a.floatValue()andbFloatValue = b.floatValue()then callingFloat.min(aFloatValue, bFloatValue).- Parameters:
a- the first operandb- the second operand- Returns:
- the smaller of
aandb - See Also:
-