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

Default construction provides no initialization (hence it is not constexpr).

half(float f)

Construct from float.

constexpr half(FromBitsTag, uint16_t bits)

Construct from bit-vector.

constexpr half(const half&)

Copy constructor.

constexpr half(half&&)

Move constructor.

~half()

Destructor.

Basic Algebra

constexpr half operator-() const

Unary minus.

half &operator=(const half &h)

Assignment.

half &operator=(half &&h)

Move assignment.

half &operator=(float f)

Assignment from float.

half &operator+=(half h)

Addition assignment.

half &operator+=(float f)

Addition assignment from float.

half &operator-=(half h)

Subtraction assignment.

half &operator-=(float f)

Subtraction assignment from float.

half &operator*=(half h)

Multiplication assignment.

half &operator*=(float f)

Multiplication assignment from float.

half &operator/=(half h)

Division assignment.

half &operator/=(float f)

Division assignment from float.

Classification

constexpr bool isFinite() const

Return true if a normalized number, a denormalized number, or zero.

constexpr bool isNormalized() const

Return true if a normalized number.

constexpr bool isDenormalized() const

Return true if a denormalized number.

constexpr bool isZero() const

Return true if zero.

constexpr bool isNan() const

Return true if NAN.

constexpr bool isInfinity() const

Return true if a positive or a negative infinity.

constexpr bool isNegative() const

Return true if the sign bit is set (negative)

Access to the internal representation

constexpr uint16_t bits() const

Return the bit pattern.

constexpr void setBits(uint16_t bits)

Set the bit pattern.

Special values

constexpr half posInf()

Return +infinity.

constexpr half negInf()

Return -infinity.

constexpr half qNan()

Returns a NAN with the bit pattern 0111111111111111.

constexpr half sNan()

Return a NAN with the bit pattern 0111110111111111.

Public Types

enum FromBitsTag

A special tag that lets us initialize a half from the raw bits.

Values:

FromBits
using uif = imath_half_uif

Public Functions

operator float() const

Conversion to float.

constexpr half round(unsigned int n) const

Round to n-bit precision (n should be between 0 and 10).

After rounding, the significand’s 10-n least significant bits will be zero.

Warning

doxygenfunction: Unable to resolve multiple matches for function “operator<<” with arguments (std::ostream& os, Imath::half h) in doxygen xml output for project “Imath” from directory: /build/ilmbase-pisqTC/ilmbase-3.1.11/obj-x86_64-linux-gnu/website/doxygen/xml. Potential matches:

- std::ostream &operator<<(std::ostream&, Imath::half)
- template<class T>
  std::ostream &Imath::operator<<(std::ostream&, const Color4<T>&)
- template<class T>
  std::ostream &Imath::operator<<(std::ostream&, const Euler<T>&)
- template<class T>
  std::ostream &Imath::operator<<(std::ostream&, const Interval<T>&)
- template<class T>
  std::ostream &Imath::operator<<(std::ostream&, const Line3<T>&)
- template<class T>
  std::ostream &Imath::operator<<(std::ostream&, const Matrix22<T>&)
- template<class T>
  std::ostream &Imath::operator<<(std::ostream&, const Matrix33<T>&)
- template<class T>
  std::ostream &Imath::operator<<(std::ostream&, const Matrix44<T>&)
- template<class T>
  std::ostream &Imath::operator<<(std::ostream&, const Plane3<T>&)
- template<class T>
  std::ostream &Imath::operator<<(std::ostream&, const Quat<T>&)
- template<class T>
  std::ostream &Imath::operator<<(std::ostream&, const Shear6<T>&)
- template<class T>
  std::ostream &Imath::operator<<(std::ostream&, const Vec2<T>&)
- template<class T>
  std::ostream &Imath::operator<<(std::ostream&, const Vec3<T>&)
- template<class T>
  std::ostream &Imath::operator<<(std::ostream&, const Vec4<T>&)
std::istream &operator>>(std::istream &is, Imath::half &h)

Input h from is.