Half Class

Definition

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).

[Android.Runtime.Register("android/util/Half", ApiSince=26, DoNotGenerateAcw=true)]
public sealed class Half : Java.Lang.Number, IDisposable, Java.Interop.IJavaPeerable, Java.Lang.IComparable
[<Android.Runtime.Register("android/util/Half", ApiSince=26, DoNotGenerateAcw=true)>]
type Half = class
    inherit Number
    interface IComparable
    interface IJavaObject
    interface IDisposable
    interface IJavaPeerable
Inheritance
Attributes
Implements

Remarks

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:

<ul> <li>Sign bit: 1 bit</li> <li>Exponent width: 5 bits</li> <li>Significand: 10 bits</li> </ul>

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 <em>precision</em> column indicates the step size between two consecutive numbers in a specific part of the range.

<table summary="Precision of fp16 across the range"> <tr><th>Range start</th><th>Precision</th></tr> <tr><td>0</td><td>1 &frasl; 16,777,216</td></tr> <tr><td>1 &frasl; 16,384</td><td>1 &frasl; 16,777,216</td></tr> <tr><td>1 &frasl; 8,192</td><td>1 &frasl; 8,388,608</td></tr> <tr><td>1 &frasl; 4,096</td><td>1 &frasl; 4,194,304</td></tr> <tr><td>1 &frasl; 2,048</td><td>1 &frasl; 2,097,152</td></tr> <tr><td>1 &frasl; 1,024</td><td>1 &frasl; 1,048,576</td></tr> <tr><td>1 &frasl; 512</td><td>1 &frasl; 524,288</td></tr> <tr><td>1 &frasl; 256</td><td>1 &frasl; 262,144</td></tr> <tr><td>1 &frasl; 128</td><td>1 &frasl; 131,072</td></tr> <tr><td>1 &frasl; 64</td><td>1 &frasl; 65,536</td></tr> <tr><td>1 &frasl; 32</td><td>1 &frasl; 32,768</td></tr> <tr><td>1 &frasl; 16</td><td>1 &frasl; 16,384</td></tr> <tr><td>1 &frasl; 8</td><td>1 &frasl; 8,192</td></tr> <tr><td>1 &frasl; 4</td><td>1 &frasl; 4,096</td></tr> <tr><td>1 &frasl; 2</td><td>1 &frasl; 2,048</td></tr> <tr><td>1</td><td>1 &frasl; 1,024</td></tr> <tr><td>2</td><td>1 &frasl; 512</td></tr> <tr><td>4</td><td>1 &frasl; 256</td></tr> <tr><td>8</td><td>1 &frasl; 128</td></tr> <tr><td>16</td><td>1 &frasl; 64</td></tr> <tr><td>32</td><td>1 &frasl; 32</td></tr> <tr><td>64</td><td>1 &frasl; 16</td></tr> <tr><td>128</td><td>1 &frasl; 8</td></tr> <tr><td>256</td><td>1 &frasl; 4</td></tr> <tr><td>512</td><td>1 &frasl; 2</td></tr> <tr><td>1,024</td><td>1</td></tr> <tr><td>2,048</td><td>2</td></tr> <tr><td>4,096</td><td>4</td></tr> <tr><td>8,192</td><td>8</td></tr> <tr><td>16,384</td><td>16</td></tr> <tr><td>32,768</td><td>32</td></tr> </table>

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

Java documentation for android.util.Half.

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Constructors

Half(Double)

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

Half(Int16)

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

Half(Single)

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

Half(String)

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

Fields

Epsilon

Epsilon is the difference between 1.

LowestValue

Smallest negative value a half-precision float may have.

MaxExponent

Maximum exponent a finite half-precision float may have.

MaxValue

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

MinExponent

Minimum exponent a normalized half-precision float may have.

MinNormal

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

MinValue

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

NaN

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

NegativeInfinity

Negative infinity of type half-precision float.

NegativeZero

Negative 0 of type half-precision float.

PositiveInfinity

Positive infinity of type half-precision float.

PositiveZero

Positive 0 of type half-precision float.

Size

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

Properties

Class

Returns the runtime class of this Object.

(Inherited from Object)
Handle

The handle to the underlying Android instance.

(Inherited from Object)
IsNaN

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

JniIdentityHashCode (Inherited from Object)
JniPeerMembers
PeerReference (Inherited from Object)
ThresholdClass

This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.

(Inherited from Number)
ThresholdType

This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.

(Inherited from Number)

Methods

Abs(Int16)

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

ByteValue()

Returns the value of the specified number as a byte.

(Inherited from Number)
Ceil(Int16)

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

Clone()

Creates and returns a copy of this object.

(Inherited from Object)
Compare(Int16, Int16)

Compares the two specified half-precision float values.

CompareTo(Half)

Compares the two specified half-precision float values.

CopySign(Int16, Int16)

Returns the first parameter with the sign of the second parameter.

Dispose() (Inherited from Object)
Dispose(Boolean) (Inherited from Object)
DoubleValue()

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

Equals(Int16, Int16)

Returns true if the two half-precision float values are equal.

Equals(Object)

Indicates whether some other object is "equal to" this one.

(Inherited from Object)
FloatValue()

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

Floor(Int16)

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

GetExponent(Int16)

Returns the unbiased exponent used in the representation of the specified half-precision float value.

GetHashCode()

Returns a hash code value for the object.

(Inherited from Object)
GetSign(Int16)

Returns the sign of the specified half-precision float.

GetSignificand(Int16)

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

Greater(Int16, Int16)

Returns true if the first half-precision float value is greater (larger toward positive infinity) than the second half-precision float value.

GreaterEquals(Int16, Int16)

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.

HalfToIntBits(Int16)

Returns a representation of the specified half-precision float value according to the bit layout described in Half.

HalfToRawIntBits(Int16)

Returns a representation of the specified half-precision float value according to the bit layout described in Half.

HalfToShortBits(Int16)

Returns a representation of the specified half-precision float value according to the bit layout described in Half.

HalfValue()

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

HashCode(Int16)

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

IntBitsToHalf(Int32)

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

IntValue()

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

InvokeIsNaN(Int16)

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

IsInfinite(Int16)

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

IsNormalized(Int16)

Returns true if the specified half-precision float value is normalized (does not have a subnormal representation).

JavaFinalize()

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.

(Inherited from Object)
Less(Int16, Int16)

Returns true if the first half-precision float value is less (smaller toward negative infinity) than the second half-precision float value.

LessEquals(Int16, Int16)

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.

LongValue()

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

Max(Int16, Int16)

Returns the larger of two half-precision float values (the value closest to positive infinity).

Min(Int16, Int16)

Returns the smaller of two half-precision float values (the value closest to negative infinity).

Notify()

Wakes up a single thread that is waiting on this object's monitor.

(Inherited from Object)
NotifyAll()

Wakes up all threads that are waiting on this object's monitor.

(Inherited from Object)
ParseHalf(String)

Returns the half-precision float value represented by the specified string.

Round(Int16)

Returns the closest integral half-precision float value to the specified half-precision float value.

SetHandle(IntPtr, JniHandleOwnership)

Sets the Handle property.

(Inherited from Object)
ShortValue()

Returns the value of the specified number as a short.

(Inherited from Number)
ToArray<T>() (Inherited from Object)
ToFloat(Int16)

Converts the specified half-precision float value into a single-precision float value.

ToHalf(Single)

Converts the specified single-precision float value into a half-precision float value.

ToHexString(Int16)

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

ToString()

Returns a string representation of the object.

(Inherited from Object)
ToString(Int16)

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

Trunc(Int16)

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

UnregisterFromRuntime() (Inherited from Object)
ValueOf(Int16)

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

ValueOf(Single)

Returns a Half instance representing the specified float value.

ValueOf(String)

Returns a Half instance representing the specified string value.

Wait()

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>.

(Inherited from Object)
Wait(Int64)

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>, or until a certain amount of real time has elapsed.

(Inherited from Object)
Wait(Int64, Int32)

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>, or until a certain amount of real time has elapsed.

(Inherited from Object)

Explicit Interface Implementations

IComparable.CompareTo(Object)
IJavaPeerable.Disposed() (Inherited from Object)
IJavaPeerable.DisposeUnlessReferenced() (Inherited from Object)
IJavaPeerable.Finalized() (Inherited from Object)
IJavaPeerable.JniManagedPeerState (Inherited from Object)
IJavaPeerable.SetJniIdentityHashCode(Int32) (Inherited from Object)
IJavaPeerable.SetJniManagedPeerState(JniManagedPeerStates) (Inherited from Object)
IJavaPeerable.SetPeerReference(JniObjectReference) (Inherited from Object)

Extension Methods

JavaCast<TResult>(IJavaObject)

Performs an Android runtime-checked type conversion.

JavaCast<TResult>(IJavaObject)
GetJniTypeName(IJavaPeerable)

Applies to