Magnum::Math::Half class

Half-precision float literal.

Represents a floating-point value in the binary16 format.

The sole purpose of this type is to make creation, conversion and visualization of half-float values easier. By design it doesn't support any arithmetic operations as not all CPU architecture have native support for half-floats and thus the operations would be done faster in a regular single-precision Float.

The class provides explicit conversion from and to Float, equality comparison with correct treatment of NaN values, promotion and negation operator, a Literals::HalfLiterals::operator""_h() literal and an operator<<(Debug&, Half) debug operator. Internally the class uses packHalf() and unpackHalf(). Example usage:

using namespace Math::Literals;

Half a = 3.14159_h;
Debug{} << a;                   // Prints 3.141
Debug{} << Float(a);            // Prints 3.14062
Debug{} << UnsignedShort(a);    // Prints 25675

Note that it is also possible to use this type inside Vector classes, though, again, only for passing data around and converting them, without any arithmetic operations:

Vector3h a{3.14159_h, -1.4142_h, 1.618_h};
Vector3 b{a};                   // converts to 32-bit floats
Debug{} << a;                   // prints {3.141, -1.414, 1.618}
Debug{} << Vector3us{a};        // prints {16968, 48552, 15993}

Constructors, destructors, conversion operators

Half() constexpr noexcept
Default constructor.
Half(ZeroInitT) explicit constexpr noexcept
Construct a zero value.
Half(UnsignedShort data) explicit constexpr noexcept
Construct a half value from underlying 16-bit representation.
Half(Float value) explicit noexcept
Construct a half value from a 32-bit float representation.
Half(Double value) explicit noexcept
Construct a half value from a 64-bit float representation.
Half(Magnum::NoInitT) explicit noexcept
Construct without initializing the contents.
operator UnsignedShort() const explicit constexpr
Conversion to underlying representation.
operator Float() const explicit
Conversion to 32-bit float representation.

Public functions

auto operator==(Half other) const -> bool constexpr
Equality comparison.
auto operator!=(Half other) const -> bool constexpr
Non-equality comparison.
auto operator+() const -> Half constexpr
Promotion.
auto operator-() const -> Half constexpr
Negation.
auto data() const -> UnsignedShort constexpr
Underlying representation.

Function documentation

Magnum::Math::Half::Half() constexpr noexcept

Default constructor.

Equivalent to Half(ZeroInitT).

Magnum::Math::Half::Half(Float value) explicit noexcept

Construct a half value from a 32-bit float representation.

Magnum::Math::Half::Half(Double value) explicit noexcept

Construct a half value from a 64-bit float representation.

Present only to aid generic code so e.g. T(1.0) works without being ambigous.

Magnum::Math::Half::operator UnsignedShort() const explicit constexpr

Conversion to underlying representation.

Magnum::Math::Half::operator Float() const explicit

Conversion to 32-bit float representation.

bool Magnum::Math::Half::operator==(Half other) const constexpr

Equality comparison.

Returns false if one of the values is half-float representation of NaN, otherwise does bitwise comparison.

bool Magnum::Math::Half::operator!=(Half other) const constexpr

Non-equality comparison.

Simply negates the result of operator==().

Half Magnum::Math::Half::operator+() const constexpr

Promotion.

Returns the value as-is.

UnsignedShort Magnum::Math::Half::data() const constexpr

Underlying representation.

Half operator""_h(long double value)

Half-float literal.

See Half for more information.

Debug& operator<<(Debug& debug, Half value)

Debug output operator.

Prints the value with 4 significant digits.