Browse Source

Math: debug output for Half with proper amount of decimal places.

The totally random places after were not exactly useful. This file was
sitting here for a while, not sure why I didn't commit that earlier.
pull/267/head
Vladimír Vondruš 8 years ago
parent
commit
a77a46c926
  1. 1
      src/Magnum/CMakeLists.txt
  2. 41
      src/Magnum/Math/Half.cpp
  3. 14
      src/Magnum/Math/Half.h
  4. 6
      src/Magnum/Math/Test/HalfTest.cpp

1
src/Magnum/CMakeLists.txt

@ -133,6 +133,7 @@ endif()
# Files shared between main library and math unit test library
set(MagnumMath_SRCS
Math/Color.cpp
Math/Half.cpp
Math/Functions.cpp
Math/Packing.cpp
Math/instantiation.cpp)

41
src/Magnum/Math/Half.cpp

@ -0,0 +1,41 @@
/*
This file is part of Magnum.
Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018
Vladimír Vondruš <mosra@centrum.cz>
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#include "Magnum/Math/Half.h"
#include <iomanip>
#include <sstream>
namespace Magnum { namespace Math {
Corrade::Utility::Debug& operator<<(Corrade::Utility::Debug& debug, Half value) {
std::ostringstream out;
/* Wikipedia says it's 3 or 4 decimal places:
https://en.wikipedia.org/wiki/Half-precision_floating-point_format */
out << std::setprecision(4) << Float(value);
return debug << out.str();
}
}}

14
src/Magnum/Math/Half.h

@ -150,10 +150,16 @@ inline Half operator "" _h(long double value) { return Half(Float(value)); }
}
/** @debugoperator{Half} */
inline Corrade::Utility::Debug& operator<<(Corrade::Utility::Debug& debug, Half value) {
return debug << Float(value);
}
/**
@debugoperator{Half}
Prints the value with 4 significant digits.
@see @ref Corrade::Utility::Debug::operator<<(float),
@ref Corrade::Utility::Debug::operator<<(double),
@ref Corrade::Utility::Debug::operator<<(long double value)
@todoc remove `long double value` once doxygen can link to long double overloads properly
*/
MAGNUM_EXPORT Corrade::Utility::Debug& operator<<(Corrade::Utility::Debug& debug, Half value);
}}

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

@ -609,12 +609,12 @@ void HalfTest::debug() {
std::ostringstream out;
Debug{&out} << -3.64_h << Half{Constants::inf()}
Debug{&out} << -36.41_h << Half{Constants::inf()}
<< Math::Vector3<Half>{3.14159_h, -1.4142_h, 1.618_h};
#ifdef _MSC_VER
CORRADE_COMPARE(out.str(), "-3.64063 inf Vector(3.14063, -1.41406, 1.61816)\n");
CORRADE_COMPARE(out.str(), "-36.41 inf Vector(3.141, -1.414, 1.618)\n");
#else
CORRADE_COMPARE(out.str(), "-3.64062 inf Vector(3.14062, -1.41406, 1.61816)\n");
CORRADE_COMPARE(out.str(), "-36.41 inf Vector(3.141, -1.414, 1.618)\n");
#endif
}

Loading…
Cancel
Save