Browse Source

Math: make it possible to construct Half from a double.

A lot of generic code uses T(1.0) and right now that caused an ambiguity
between the Float and UnsignedShort constructors.
pull/369/head
Vladimír Vondruš 6 years ago
parent
commit
50e21015eb
  1. 11
      src/Magnum/Math/Half.h
  2. 6
      src/Magnum/Math/Test/HalfTest.cpp

11
src/Magnum/Math/Half.h

@ -86,12 +86,21 @@ class Half {
constexpr explicit Half(UnsignedShort data) noexcept: _data{data} {}
/**
* @brief Construct a half value from 32-bit float representation
* @brief Construct a half value from a 32-bit float representation
*
* @see @ref packHalf()
*/
explicit Half(Float value) noexcept: _data{packHalf(value)} {}
/**
* @brief Construct a half value from a 64-bit float representation
*
* Present only to aid generic code so e.g. @cpp T(1.0) @ce works
* without being ambigous.
* @see @ref packHalf()
*/
explicit Half(Double value) noexcept: _data{packHalf(Float(value))} {}
/** @brief Construct without initializing the contents */
explicit Half(NoInitT) noexcept {}

6
src/Magnum/Math/Test/HalfTest.cpp

@ -513,14 +513,20 @@ void HalfTest::constructDefault() {
void HalfTest::constructValue() {
Half a{3.5f};
Half b{3.5};
CORRADE_COMPARE(Float(a), 3.5f);
CORRADE_COMPARE(Float(b), 3.5f);
CORRADE_COMPARE(UnsignedShort(a), 0x4300);
CORRADE_COMPARE(UnsignedShort(b), 0x4300);
CORRADE_COMPARE(a.data(), 0x4300);
CORRADE_COMPARE(b.data(), 0x4300);
CORRADE_VERIFY((std::is_nothrow_constructible<Half, Float>::value));
CORRADE_VERIFY((std::is_nothrow_constructible<Half, Double>::value));
/* Implicit conversion is not allowed */
CORRADE_VERIFY(!(std::is_convertible<Float, Half>::value));
CORRADE_VERIFY(!(std::is_convertible<Double, Half>::value));
}
void HalfTest::constructData() {

Loading…
Cancel
Save