Class Half

java.lang.Object
java.lang.Number
net.solarnetwork.util.Half
All Implemented Interfaces:
Serializable, Comparable<Half>

public class Half extends Number implements Comparable<Half>
An IEEE 754 half-precision (16-bit) floating point number.
Since:
1.51
Version:
1.0
Author:
matt
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final short
    Epsilon is the difference between 1.0 and the next value representable by a half-precision floating-point.
    static final short
    Smallest negative value a half-precision float may have.
    static final int
    Maximum exponent a finite half-precision float may have.
    static final short
    Maximum positive finite value a half-precision float may have.
    static final int
    Minimum exponent a normalized half-precision float may have.
    static final short
    Smallest positive normal value a half-precision float may have.
    static final short
    Smallest positive non-zero value a half-precision float may have.
    static final short
    A Not-a-Number representation of a half-precision float.
    static final short
    Negative infinity of type half-precision float.
    static final short
    Negative 0 of type half-precision float.
    static final short
    Positive infinity of type half-precision float.
    static final short
    Positive 0 of type half-precision float.
    static final int
    /** The number of bits used to represent a half-precision float value.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Half(double value)
    Constructs a newly allocated Half object that represents the argument converted to a half-precision float.
    Half(float value)
    Constructs a newly allocated Half object that represents the argument converted to a half-precision float.
    Half(short value)
    Constructs a newly allocated Half object that represents the half-precision float type argument.
    Half(String value)
    Constructs a newly allocated Half object that represents the half-precision float value represented by the string.
  • Method Summary

    Modifier and Type
    Method
    Description
    static short
    abs(short h)
    Returns the absolute value of the specified half-precision float.
    byte
    Returns the value of this Half as a byte after a narrowing primitive conversion.
    static short
    ceil(short h)
    Returns the smallest half-precision float value toward negative infinity greater than or equal to the specified half-precision float value.
    static int
    compare(short x, short y)
    Compares the two specified half-precision float values.
    int
    Compares the two specified half-precision float values.
    static short
    copySign(short magnitude, short sign)
    Returns the first parameter with the sign of the second parameter.
    double
    Returns the value of this Half as a double after a widening primitive conversion.
    static boolean
    equals(short x, short y)
    Returns true if the two half-precision float values are equal.
    boolean
    Compares this object against the specified object.
    float
    Returns the value of this Half as a float after a widening primitive conversion.
    static short
    floor(short h)
    Returns the largest half-precision float value toward positive infinity less than or equal to the specified half-precision float value.
    static int
    getExponent(short h)
    Returns the unbiased exponent used in the representation of the specified half-precision float value.
    static int
    getSign(short h)
    Returns the sign of the specified half-precision float.
    static int
    getSignificand(short h)
    Returns the significand, or mantissa, used in the representation of the specified half-precision float value.
    static boolean
    greater(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 boolean
    greaterEquals(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 int
    halfToIntBits(short h)
    Returns a representation of the specified half-precision float value according to the bit layout described in Half.
    static int
    Returns a representation of the specified half-precision float value according to the bit layout described in Half.
    static short
    halfToShortBits(short h)
    Returns a representation of the specified half-precision float value according to the bit layout described in Half.
    short
    Returns the half-precision value of this Half as a short containing the bit representation described in Half.
    int
    Returns a hash code for this Half object.
    static int
    hashCode(short h)
    Returns a hash code for a half-precision float value.
    static short
    intBitsToHalf(int bits)
    Returns the half-precision float value corresponding to a given bit representation.
    int
    Returns the value of this Half as a int after a narrowing primitive conversion.
    static boolean
    isInfinite(short h)
    Returns true if the specified half-precision float value represents infinity, false otherwise.
    boolean
    Returns true if this Half value represents a Not-a-Number, false otherwise.
    static boolean
    isNaN(short h)
    Returns true if the specified half-precision float value represents a Not-a-Number, false otherwise.
    static boolean
    isNormalized(short h)
    Returns true if the specified half-precision float value is normalized (does not have a subnormal representation).
    static boolean
    less(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 boolean
    lessEquals(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.
    long
    Returns the value of this Half as a long after a narrowing primitive conversion.
    static short
    max(short x, short y)
    Returns the larger of two half-precision float values (the value closest to positive infinity).
    static short
    min(short x, short y)
    Returns the smaller of two half-precision float values (the value closest to negative infinity).
    static short
    Returns the half-precision float value represented by the specified string.
    static short
    round(short h)
    Returns the closest integral half-precision float value to the specified half-precision float value.
    short
    Returns the value of this Half as a short after a narrowing primitive conversion.
    static float
    toFloat(short h)
    Converts the specified half-precision float value into a single-precision float value.
    static short
    toHalf(float f)
    Converts the specified single-precision float value into a half-precision float value.
    static String
    toHexString(short h)
    Returns a hexadecimal string representation of the specified half-precision float value.
    Returns a string representation of the specified half-precision float value.
    static String
    toString(short h)
    Returns a string representation of the specified half-precision float value.
    static short
    trunc(short h)
    Returns the truncated half-precision float value of the specified half-precision float value.
    static Half
    valueOf(float f)
    Returns a Half instance representing the specified float value.
    static Half
    valueOf(short h)
    Returns a Half instance representing the specified half-precision float value.
    static Half
    Returns a Half instance representing the specified string value.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • SIZE

      public static final int SIZE
      /** The number of bits used to represent a half-precision float value.
      See Also:
    • EPSILON

      public static final short 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_EXPONENT
      Maximum exponent a finite half-precision float may have.
      See Also:
    • MIN_EXPONENT

      public static final int MIN_EXPONENT
      Minimum exponent a normalized half-precision float may have.
      See Also:
    • LOWEST_VALUE

      public static final short LOWEST_VALUE
      Smallest negative value a half-precision float may have.
      See Also:
    • MAX_VALUE

      public static final short MAX_VALUE
      Maximum positive finite value a half-precision float may have.
      See Also:
    • MIN_NORMAL

      public static final short MIN_NORMAL
      Smallest positive normal value a half-precision float may have.
      See Also:
    • MIN_VALUE

      public static final short MIN_VALUE
      Smallest positive non-zero value a half-precision float may have.
      See Also:
    • NaN

      public static final short NaN
      A Not-a-Number representation of a half-precision float.
      See Also:
    • NEGATIVE_INFINITY

      public static final short NEGATIVE_INFINITY
      Negative infinity of type half-precision float.
      See Also:
    • NEGATIVE_ZERO

      public static final short NEGATIVE_ZERO
      Negative 0 of type half-precision float.
      See Also:
    • POSITIVE_INFINITY

      public static final short POSITIVE_INFINITY
      Positive infinity of type half-precision float.
      See Also:
    • POSITIVE_ZERO

      public static final short POSITIVE_ZERO
      Positive 0 of type half-precision float.
      See Also:
  • Constructor Details

    • Half

      public Half(short value)
      Constructs a newly allocated Half object that represents the half-precision float type argument.
      Parameters:
      value - The value to be represented by the Half
    • Half

      public Half(float value)
      Constructs a newly allocated Half object that represents the argument converted to a half-precision float.
      Parameters:
      value - The value to be represented by the Half
      See Also:
    • Half

      public Half(double value)
      Constructs a newly allocated Half object that represents the argument converted to a half-precision float.
      Parameters:
      value - The value to be represented by the Half
      See Also:
    • Half

      public Half(String value) throws NumberFormatException

      Constructs a newly allocated Half object that represents the half-precision float value represented by the string. The string is converted to a half-precision float value as if by the valueOf(String) method.

      Calling this constructor is equivalent to calling:

       new Half(Float.parseFloat(value))
       
      Parameters:
      value - A string to be converted to a Half
      Throws:
      NumberFormatException - if the string does not contain a parsable number
      See Also:
  • Method Details

    • halfValue

      public short halfValue()
      Returns the half-precision value of this Half as a short containing the bit representation described in Half.
      Returns:
      The half-precision float value represented by this object
    • byteValue

      public byte byteValue()
      Returns the value of this Half as a byte after a narrowing primitive conversion.
      Overrides:
      byteValue in class Number
      Returns:
      The half-precision float value represented by this object converted to type byte
    • shortValue

      public short shortValue()
      Returns the value of this Half as a short after a narrowing primitive conversion.
      Overrides:
      shortValue in class Number
      Returns:
      The half-precision float value represented by this object converted to type short
    • intValue

      public int intValue()
      Returns the value of this Half as a int after a narrowing primitive conversion.
      Specified by:
      intValue in class Number
      Returns:
      The half-precision float value represented by this object converted to type int
    • longValue

      public long longValue()
      Returns the value of this Half as a long after a narrowing primitive conversion.
      Specified by:
      longValue in class Number
      Returns:
      The half-precision float value represented by this object converted to type long
    • floatValue

      public float floatValue()
      Returns the value of this Half as a float after a widening primitive conversion.
      Specified by:
      floatValue in class Number
      Returns:
      The half-precision float value represented by this object converted to type float
    • doubleValue

      public double doubleValue()
      Returns the value of this Half as a double after a widening primitive conversion.
      Specified by:
      doubleValue in class Number
      Returns:
      The half-precision float value represented by this object converted to type double
    • isNaN

      public boolean isNaN()
      Returns true if this Half value represents a Not-a-Number, false otherwise.
      Returns:
      True if the value is a NaN, false otherwise
    • equals

      public boolean equals(Object o)
      Compares this object against the specified object. The result is true if and only if the argument is not null and is a Half object 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 method halfToIntBits(short) returns an identical int value for both.
      Overrides:
      equals in class Object
      Parameters:
      o - The object to compare
      Returns:
      True if the objects are the same, false otherwise
      See Also:
    • hashCode

      public int hashCode()
      Returns a hash code for this Half object. The result is the integer bit representation, exactly as produced by the method halfToIntBits(short), of the primitive half-precision float value represented by this Half object.
      Overrides:
      hashCode in class Object
      Returns:
      A hash code value for this object
    • toString

      public String toString()
      Returns a string representation of the specified half-precision float value. See toString(short) for more information.
      Overrides:
      toString in class Object
      Returns:
      A string representation of this Half object
    • compareTo

      public int compareTo(Half h)

      Compares the two specified half-precision float values. The following conditions apply during the comparison:

      • NaN is considered by this method to be equal to itself and greater than all other half-precision float values (including #POSITIVE_INFINITY)
      • POSITIVE_ZERO is considered by this method to be greater than NEGATIVE_ZERO.
      Specified by:
      compareTo in interface Comparable<Half>
      Parameters:
      h - The half-precision float value to compare to the half-precision value represented by this Half object
      Returns:
      The value 0 if x is numerically equal to y; a value less than 0 if x is numerically less than y; and a value greater than 0 if x is numerically greater than y
    • hashCode

      public static int hashCode(short h)
      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

      public static int compare(short x, short y)

      Compares the two specified half-precision float values. The following conditions apply during the comparison:

      • NaN is considered by this method to be equal to itself and greater than all other half-precision float values (including #POSITIVE_INFINITY)
      • POSITIVE_ZERO is considered by this method to be greater than NEGATIVE_ZERO.
      Parameters:
      x - The first half-precision float value to compare.
      y - The second half-precision float value to compare
      Returns:
      The value 0 if x is numerically equal to y, a value less than 0 if x is numerically less than y, and a value greater than 0 if x is numerically greater than y
    • halfToShortBits

      public static short halfToShortBits(short h)

      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 by NaN.

      Parameters:
      h - A half-precision float value
      Returns:
      The bits that represent the half-precision float value
      See Also:
    • halfToIntBits

      public static int halfToIntBits(short h)

      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 by NaN.

      Parameters:
      h - A half-precision float value
      Returns:
      The bits that represent the half-precision float value
      See Also:
    • halfToRawIntBits

      public static int halfToRawIntBits(short h)

      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

      public static short intBitsToHalf(int bits)

      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

      public static short copySign(short magnitude, short sign)
      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 result
      sign - 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

      public static short abs(short h)
      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

      public static short round(short h)
      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

      public static short ceil(short h)
      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

      public static short floor(short h)
      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

      public static short trunc(short h)
      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

      public static short min(short x, short y)
      Returns the smaller of two half-precision float values (the value closest to negative infinity). Special values are handled in the following ways:
      Parameters:
      x - The first half-precision value
      y - The second half-precision value
      Returns:
      The smaller of the two specified half-precision values
    • max

      public static short max(short x, short y)
      Returns the larger of two half-precision float values (the value closest to positive infinity). Special values are handled in the following ways:
      Parameters:
      x - The first half-precision value
      y - The second half-precision value
      Returns:
      The larger of the two specified half-precision values
    • less

      public static boolean less(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. If either of the values is NaN, the result is false.
      Parameters:
      x - The first half-precision value
      y - The second half-precision value
      Returns:
      True if x is less than y, false otherwise
    • lessEquals

      public static boolean lessEquals(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. If either of the values is NaN, the result is false.
      Parameters:
      x - The first half-precision value
      y - The second half-precision value
      Returns:
      True if x is less than or equal to y, false otherwise
    • greater

      public static boolean greater(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. If either of the values is NaN, the result is false.
      Parameters:
      x - The first half-precision value
      y - The second half-precision value
      Returns:
      True if x is greater than y, false otherwise
    • greaterEquals

      public static boolean greaterEquals(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. If either of the values is NaN, the result is false.
      Parameters:
      x - The first half-precision value
      y - The second half-precision value
      Returns:
      True if x is greater than y, false otherwise
    • equals

      public static boolean equals(short x, short y)
      Returns true if the two half-precision float values are equal. If either of the values is NaN, the result is false. POSITIVE_ZERO and NEGATIVE_ZERO are considered equal.
      Parameters:
      x - The first half-precision value
      y - The second half-precision value
      Returns:
      True if x is equal to y, false otherwise
    • getSign

      public static int getSign(short h)
      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

      public static int getExponent(short h)
      Returns the unbiased exponent used in the representation of the specified 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.
      Parameters:
      h - A half-precision float value
      Returns:
      The unbiased exponent of the specified value
    • getSignificand

      public static int getSignificand(short h)
      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

      public static boolean isInfinite(short h)
      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

      public static boolean isNaN(short h)
      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

      public static boolean isNormalized(short h)
      Returns true if the specified half-precision float value is normalized (does not have a subnormal representation). If the specified value is POSITIVE_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

      public static float toFloat(short h)

      Converts the specified half-precision float value into a single-precision float value. The following special cases are handled:

      Parameters:
      h - The half-precision float value to convert to single-precision
      Returns:
      A normalized single-precision float value
    • toHalf

      public static short toHalf(float f)

      Converts the specified single-precision float value into a half-precision float value. The following special cases are handled:

      Parameters:
      f - The single-precision float value to convert to half-precision
      Returns:
      A half-precision float value
    • valueOf

      public static Half valueOf(short h)
      Returns a Half instance representing the specified half-precision float value.
      Parameters:
      h - A half-precision float value
      Returns:
      a Half instance representing h
    • valueOf

      public static Half valueOf(float f)
      Returns a Half instance representing the specified float value.
      Parameters:
      f - A float value
      Returns:
      a Half instance representing f
    • valueOf

      public static Half valueOf(String s)
      Returns a Half instance representing the specified string value. Calling this method is equivalent to calling toHalf(Float.parseString(h)). See Float.valueOf(String) for more information on the format of the string representation.
      Parameters:
      s - The string to be parsed
      Returns:
      a Half instance representing h
      Throws:
      NumberFormatException - if the string does not contain a parsable half-precision float value
    • parseHalf

      public static short parseHalf(String s) throws NumberFormatException
      Returns the half-precision float value represented by the specified string. Calling this method is equivalent to calling toHalf(Float.parseString(h)). See Float.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

      public static String toString(short h)
      Returns a string representation of the specified half-precision float value. Calling this method is equivalent to calling Float.toString(toFloat(h)). See Float.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

      public static String toHexString(short h)

      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