The half Class¶
-
class half¶
class half represents a 16-bit floating point number
Type half can represent positive and negative numbers whose magnitude is between roughly 6.1e-5 and 6.5e+4 with a relative error of 9.8e-4; numbers smaller than 6.1e-5 can be represented with an absolute error of 6.0e-8. All integers from -2048 to +2048 can be represented exactly.
Type half behaves (almost) like the built-in C++ floating point types. In arithmetic expressions, half, float and double can be mixed freely. Here are a few examples:
half a (3.5); float b (a + sqrt (a)); a += b; b += a; b = a + 7;
Conversions from half to float are lossless; all half numbers are exactly representable as floats.
Conversions from float to half may not preserve a float’s value exactly. If a float is not representable as a half, then the float value is rounded to the nearest representable half. If a float value is exactly in the middle between the two closest representable half values, then the float value is rounded to the closest half whose least significant bit is zero.
Overflows during float-to-half conversions cause arithmetic exceptions. An overflow occurs when the float value to be converted is too large to be represented as a half, or if the float value is an infinity or a NAN.
The implementation of type half makes the following assumptions about the implementation of the built-in C++ types:
float is an IEEE 754 single-precision number
sizeof (float) == 4
sizeof (unsigned int) == sizeof (float)
alignof (unsigned int) == alignof (float)
sizeof (uint16_t) == 2
Constructors
-
half() noexcept = default¶
Default construction provides no initialization (hence it is not constexpr).
-
inline half(float f) noexcept¶
Construct from float.
-
inline constexpr half(FromBitsTag, uint16_t bits) noexcept¶
Construct from bit-vector.
-
~half() noexcept = default¶
Destructor.
Basic Algebra
Classification
-
inline constexpr bool isFinite() const noexcept¶
Return true if a normalized number, a denormalized number, or zero.
-
inline constexpr bool isNormalized() const noexcept¶
Return true if a normalized number.
-
inline constexpr bool isDenormalized() const noexcept¶
Return true if a denormalized number.
-
inline constexpr bool isZero() const noexcept¶
Return true if zero.
-
inline constexpr bool isNan() const noexcept¶
Return true if NAN.
-
inline constexpr bool isInfinity() const noexcept¶
Return true if a positive or a negative infinity.
-
inline constexpr bool isNegative() const noexcept¶
Return true if the sign bit is set (negative)
Special values
Access to the internal representation
-
inline constexpr uint16_t bits() const noexcept¶
Return the bit pattern.
-
inline constexpr void setBits(uint16_t bits) noexcept¶
Set the bit pattern.