Skip to content

Most visited

Recently visited

navigation

Half

public final class Half
extends Number implements Comparable<Half>

java.lang.Object
   ↳ java.lang.Number
     ↳ android.util.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 startPrecision
01 ⁄ 16,777,216
1 ⁄ 16,3841 ⁄ 16,777,216
1 ⁄ 8,1921 ⁄ 8,388,608
1 ⁄ 4,0961 ⁄ 4,194,304
1 ⁄ 2,0481 ⁄ 2,097,152
1 ⁄ 1,0241 ⁄ 1,048,576
1 ⁄ 5121 ⁄ 524,288
1 ⁄ 2561 ⁄ 262,144
1 ⁄ 1281 ⁄ 131,072
1 ⁄ 641 ⁄ 65,536
1 ⁄ 321 ⁄ 32,768
1 ⁄ 161 ⁄ 16,384
1 ⁄ 81 ⁄ 8,192
1 ⁄ 41 ⁄ 4,096
1 ⁄ 21 ⁄ 2,048
11 ⁄ 1,024
21 ⁄ 512
41 ⁄ 256
81 ⁄ 128
161 ⁄ 64
321 ⁄ 32
641 ⁄ 16
1281 ⁄ 8
2561 ⁄ 4
5121 ⁄ 2
1,0241
2,0482
4,0964
8,1928
16,38416
32,76832

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

Summary

Constants

short EPSILON

Epsilon is the difference between 1.0 and the next value representable by a half-precision floating-point.

short LOWEST_VALUE

Smallest negative value a half-precision float may have.

int MAX_EXPONENT

Maximum exponent a finite half-precision float may have.

short MAX_VALUE

Maximum positive finite value a half-precision float may have.

int MIN_EXPONENT

Minimum exponent a normalized half-precision float may have.

short MIN_NORMAL

Smallest positive normal value a half-precision float may have.

short MIN_VALUE

Smallest positive non-zero value a half-precision float may have.

short NEGATIVE_INFINITY

Negative infinity of type half-precision float.

short NEGATIVE_ZERO

Negative 0 of type half-precision float.

short NaN

A Not-a-Number representation of a half-precision float.

short POSITIVE_INFINITY

Positive infinity of type half-precision float.

short POSITIVE_ZERO

Positive 0 of type half-precision float.

int SIZE

The number of bits used to represent a half-precision float value.

Public constructors

Half(short value)

Constructs a newly allocated Half object that represents the half-precision float type argument.

Half(float value)

Constructs a newly allocated Half object that represents the argument converted to a half-precision float.

Half(double value)

Constructs a newly allocated Half object that represents the argument converted to a half-precision float.

Half(String value)

Constructs a newly allocated Half object that represents the half-precision float value represented by the string.

Public methods

static short abs(short h)

Returns the absolute value of the specified half-precision float.

byte byteValue()

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 compareTo(Half h)

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 doubleValue()

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 equals(Object o)

Compares this object against the specified object.

float floatValue()

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 halfToRawIntBits(short h)

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 halfValue()

Returns the half-precision value of this Half as a short containing the bit representation described in Half.

static int hashCode(short h)

Returns a hash code for a half-precision float value.

int hashCode()

Returns a hash code for this Half object.

static short intBitsToHalf(int bits)

Returns the half-precision float value corresponding to a given bit representation.

int intValue()

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 isNaN()

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 longValue()

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 parseHalf(String s)

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 shortValue()

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.

static String toString(short h)

Returns a string representation of the specified half-precision float value.

String toString()

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(String s)

Returns a Half instance representing the specified string 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.

Inherited methods

From class java.lang.Number
From class java.lang.Object
From interface java.lang.Comparable

Constants

EPSILON

short EPSILON

Epsilon is the difference between 1.0 and the next value representable by a half-precision floating-point.

Constant Value: 5120 (0x00001400)

LOWEST_VALUE

short LOWEST_VALUE

Smallest negative value a half-precision float may have.

Constant Value: -1025 (0xfffffbff)

MAX_EXPONENT

int MAX_EXPONENT

Maximum exponent a finite half-precision float may have.

Constant Value: 15 (0x0000000f)

MAX_VALUE

short MAX_VALUE

Maximum positive finite value a half-precision float may have.

Constant Value: 31743 (0x00007bff)

MIN_EXPONENT

int MIN_EXPONENT

Minimum exponent a normalized half-precision float may have.

Constant Value: -14 (0xfffffff2)

MIN_NORMAL

short MIN_NORMAL

Smallest positive normal value a half-precision float may have.

Constant Value: 1024 (0x00000400)

MIN_VALUE

short MIN_VALUE

Smallest positive non-zero value a half-precision float may have.

Constant Value: 1 (0x00000001)

NEGATIVE_INFINITY

short NEGATIVE_INFINITY

Negative infinity of type half-precision float.

Constant Value: -1024 (0xfffffc00)

NEGATIVE_ZERO

short NEGATIVE_ZERO

Negative 0 of type half-precision float.

Constant Value: -32768 (0xffff8000)

NaN

short NaN

A Not-a-Number representation of a half-precision float.

Constant Value: 32256 (0x00007e00)

POSITIVE_INFINITY

short POSITIVE_INFINITY

Positive infinity of type half-precision float.

Constant Value: 31744 (0x00007c00)

POSITIVE_ZERO

short POSITIVE_ZERO

Positive 0 of type half-precision float.

Constant Value: 0 (0x00000000)

SIZE

int SIZE

The number of bits used to represent a half-precision float value.

Constant Value: 16 (0x00000010)

Public constructors

Half

Half (short value)

Constructs a newly allocated Half object that represents the half-precision float type argument.

Parameters
value short: The value to be represented by the Half

Half

Half (float value)

Constructs a newly allocated Half object that represents the argument converted to a half-precision float.

Parameters
value float: The value to be represented by the Half

See also:

Half

Half (double value)

Constructs a newly allocated Half object that represents the argument converted to a half-precision float.

Parameters
value double: The value to be represented by the Half

See also:

Half

Half (String value)

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 String: A string to be converted to a Half

This value must never be null.

Throws
NumberFormatException if the string does not contain a parsable number

See also:

Public methods

abs

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 short: A half-precision float value

Returns
short The absolute value of the specified half-precision float

byteValue

byte byteValue ()

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

Returns
byte The half-precision float value represented by this object converted to type byte

ceil

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 short: A half-precision float value

Returns
short The smallest half-precision float value toward negative infinity greater than or equal to the specified half-precision float value

compare

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 short: The first half-precision float value to compare.

y short: The second half-precision float value to compare

Returns
int 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

compareTo

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.

Parameters
h Half: The half-precision float value to compare to the half-precision value represented by this Half object

This value must never be null.

Returns
int 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

copySign

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 short: A half-precision float value providing the magnitude of the result

sign short: A half-precision float value providing the sign of the result

Returns
short A value with the magnitude of the first parameter and the sign of the second parameter

doubleValue

double doubleValue ()

Returns the value of this Half as a double after a widening primitive conversion.

Returns
double The half-precision float value represented by this object converted to type double

equals

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 short: The first half-precision value

y short: The second half-precision value

Returns
boolean True if x is equal to y, false otherwise

equals

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.

Parameters
o Object: The object to compare

This value may be null.

Returns
boolean True if the objects are the same, false otherwise

See also:

floatValue

float floatValue ()

Returns the value of this Half as a float after a widening primitive conversion.

Returns
float The half-precision float value represented by this object converted to type float

floor

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 short: A half-precision float value

Returns
short The largest half-precision float value toward positive infinity less than or equal to the specified half-precision float value

getExponent

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 short: A half-precision float value

Returns
int The unbiased exponent of the specified value

getSign

int getSign (short h)

Returns the sign of the specified half-precision float.

Parameters
h short: A half-precision float value

Returns
int 1 if the value is positive, -1 if the value is negative

getSignificand

int getSignificand (short h)

Returns the significand, or mantissa, used in the representation of the specified half-precision float value.

Parameters
h short: A half-precision float value

Returns
int The significand, or significand, of the specified vlaue

greater

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 short: The first half-precision value

y short: The second half-precision value

Returns
boolean True if x is greater than y, false otherwise

greaterEquals

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 short: The first half-precision value

y short: The second half-precision value

Returns
boolean True if x is greater than y, false otherwise

halfToIntBits

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 short: A half-precision float value

Returns
int The bits that represent the half-precision float value

See also:

halfToRawIntBits

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 short: A half-precision float value

Returns
int The bits that represent the half-precision float value

See also:

halfToShortBits

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 short: A half-precision float value

Returns
short The bits that represent the half-precision float value

See also:

halfValue

short halfValue ()

Returns the half-precision value of this Half as a short containing the bit representation described in Half.

Returns
short The half-precision float value represented by this object

hashCode

int hashCode (short h)

Returns a hash code for a half-precision float value.

Parameters
h short: The value to hash

Returns
int A hash code value for a half-precision float value

hashCode

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.

Returns
int A hash code value for this object

intBitsToHalf

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 int: An integer

Returns
short The half-precision float value with the same bit pattern

intValue

int intValue ()

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

Returns
int The half-precision float value represented by this object converted to type int

isInfinite

boolean isInfinite (short h)

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

Parameters
h short: A half-precision float value

Returns
boolean True if the value is positive infinity or negative infinity, false otherwise

isNaN

boolean isNaN ()

Returns true if this Half value represents a Not-a-Number, false otherwise.

Returns
boolean True if the value is a NaN, false otherwise

isNaN

boolean isNaN (short h)

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

Parameters
h short: A half-precision float value

Returns
boolean True if the value is a NaN, false otherwise

isNormalized

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 short: A half-precision float value

Returns
boolean True if the value is normalized, false otherwise

less

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 short: The first half-precision value

y short: The second half-precision value

Returns
boolean True if x is less than y, false otherwise

lessEquals

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 short: The first half-precision value

y short: The second half-precision value

Returns
boolean True if x is less than or equal to y, false otherwise

longValue

long longValue ()

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

Returns
long The half-precision float value represented by this object converted to type long

max

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 short: The first half-precision value

y short: The second half-precision value

Returns
short The larger of the two specified half-precision values

min

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 short: The first half-precision value

y short: The second half-precision value

Returns
short The smaller of the two specified half-precision values

parseHalf

short parseHalf (String s)

Returns the half-precision float value represented by the specified string. Calling this method is equivalent to calling toHalf(Float.parseString(h)). See valueOf(String) for more information on the format of the string representation.

Parameters
s String: The string to be parsed

This value must never be null.

Returns
short A half-precision float value represented by the string

Throws
NumberFormatException if the string does not contain a parsable half-precision float value

round

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)

Parameters
h short: A half-precision float value

Returns
short The value of the specified half-precision float rounded to the nearest half-precision float value

shortValue

short shortValue ()

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

Returns
short The half-precision float value represented by this object converted to type short

toFloat

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 short: The half-precision float value to convert to single-precision

Returns
float A normalized single-precision float value

toHalf

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 float: The single-precision float value to convert to half-precision

Returns
short A half-precision float value

toHexString

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 short: A half-precision float value

Returns
String A hexadecimal string representation of the specified value

This value will never be null.

toString

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 toString(float) for more information on the format of the string representation.

Parameters
h short: A half-precision float value

Returns
String A string representation of the specified value

This value will never be null.

toString

String toString ()

Returns a string representation of the specified half-precision float value. See toString(short) for more information.

Returns
String A string representation of this Half object

This value will never be null.

trunc

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 short: A half-precision float value

Returns
short The truncated half-precision float value of the specified half-precision float value

valueOf

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 valueOf(String) for more information on the format of the string representation.

Parameters
s String: The string to be parsed

This value must never be null.

Returns
Half a Half instance representing h

This value will never be null.

Throws
NumberFormatException if the string does not contain a parsable half-precision float value

valueOf

Half valueOf (float f)

Returns a Half instance representing the specified float value.

Parameters
f float: A float value

Returns
Half a Half instance representing f

This value will never be null.

valueOf

Half valueOf (short h)

Returns a Half instance representing the specified half-precision float value.

Parameters
h short: A half-precision float value

Returns
Half a Half instance representing h

This value will never be null.

This site uses cookies to store your preferences for site-specific language and display options.

Get the latest Android developer news and tips that will help you find success on Google Play.

* Required Fields

Hooray!

Browse this site in ?

You requested a page in , but your language preference for this site is .

Would you like to change your language preference and browse this site in ? If you want to change your language preference later, use the language menu at the bottom of each page.

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a one-minute survey?
Help us improve Android tools and documentation.