Browse Source

Using round() in denormalization.

Gives more precise results for smaller types, but creates overflow for
large types.
vectorfields
Vladimír Vondruš 14 years ago
parent
commit
3bcaed6322
  1. 5
      src/Math/Math.h
  2. 12
      src/Math/Test/MathTest.cpp

5
src/Math/Math.h

@ -16,6 +16,7 @@
*/
#include <cstddef>
#include <cmath>
#include <type_traits>
#include <limits>
@ -120,8 +121,8 @@ resulting `Integral` type (e.g. `double` to `int`, `long double` to `long long`)
*/
template<class Integral, class FloatingPoint> inline constexpr typename std::enable_if<std::is_floating_point<FloatingPoint>::value && std::is_integral<Integral>::value, Integral>::type denormalize(FloatingPoint value) {
return std::numeric_limits<Integral>::min() +
Integral(value*std::numeric_limits<Integral>::max()) -
Integral(value*std::numeric_limits<Integral>::min());
round(FloatingPoint(value*std::numeric_limits<Integral>::max()) -
FloatingPoint(value*std::numeric_limits<Integral>::min()));
}
/**

12
src/Math/Test/MathTest.cpp

@ -68,8 +68,8 @@ void MathTest::denormalize() {
CORRADE_COMPARE(Math::denormalize<unsigned char>(1.0f), 255);
/* Between */
CORRADE_COMPARE(Math::denormalize<short>(0.33f), -11142);
CORRADE_COMPARE(Math::denormalize<short>(0.66f), 10484);
CORRADE_COMPARE(Math::denormalize<short>(0.33f), -11141);
CORRADE_COMPARE(Math::denormalize<short>(0.66f), 10485);
/* Test overflow for large types */
CORRADE_COMPARE(Math::denormalize<int>(0.0f), numeric_limits<int>::min());
@ -79,8 +79,12 @@ void MathTest::denormalize() {
CORRADE_COMPARE(Math::denormalize<int>(1.0), numeric_limits<int>::max());
CORRADE_COMPARE(Math::denormalize<unsigned int>(1.0), numeric_limits<unsigned int>::max());
CORRADE_COMPARE((Math::denormalize<long long, long double>(1.0)), numeric_limits<long long>::max());
CORRADE_COMPARE((Math::denormalize<unsigned long long, long double>(1.0)), numeric_limits<unsigned long long>::max());
{
CORRADE_EXPECT_FAIL("Denormalize doesn't work for large types well");
CORRADE_COMPARE((Math::denormalize<long long, long double>(1.0)), numeric_limits<long long>::max());
CORRADE_COMPARE((Math::denormalize<unsigned long long, long double>(1.0)), numeric_limits<unsigned long long>::max());
}
}
void MathTest::pow() {

Loading…
Cancel
Save