diff --git a/include/etl/gamma.h b/include/etl/gamma.h index 3243677a..46713a47 100644 --- a/include/etl/gamma.h +++ b/include/etl/gamma.h @@ -63,7 +63,10 @@ namespace etl //********************************* TInput operator ()(TInput value) const { - return TInput(TInput(maximum * pow(double(value) / maximum, one_over_gamma))); + // Calculate intermediate result because rounding + optimization + // lead to wrong values when returning directly (test on i386) + const double result = maximum * pow(double(value) / maximum, one_over_gamma); + return TInput(result); } private: @@ -95,7 +98,10 @@ namespace etl //********************************* TInput operator ()(TInput value) const { - return TInput(TInput(maximum * pow(double(value) / maximum, gamma))); + // Calculate intermediate result because rounding + optimization + // lead to wrong values when returning directly (test on i386) + const double result = maximum * pow(double(value) / maximum, gamma); + return TInput(result); } private: diff --git a/test/test_gamma.cpp b/test/test_gamma.cpp index 68d943a2..db84cc5c 100644 --- a/test/test_gamma.cpp +++ b/test/test_gamma.cpp @@ -41,6 +41,7 @@ namespace using IntGammaEncode = etl::gamma_encode; using DoubleGammaEncode = etl::gamma_encode; + using IntGammaDecode = etl::gamma_decode; using DoubleGammaDecode = etl::gamma_decode; struct Compare @@ -62,6 +63,16 @@ namespace 0, 0, 0, 1, 1, 2, 4, 5, 7, 9 }; + const std::array input1b = + { + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 + }; + + const std::array result1b = + { + 0, 3, 4, 5, 6, 6, 7, 7, 8, 9 + }; + std::array output1; //*********************************** @@ -100,6 +111,17 @@ namespace CHECK(isEqual); } + //************************************************************************* + TEST(test_int_gamma_decode) + { + IntGammaDecode gamma(0.5, 9); + + std::transform(input1b.begin(), input1b.end(), output1.begin(), gamma); + + bool isEqual = std::equal(output1.begin(), output1.end(), result1b.begin()); + CHECK(isEqual); + } + //************************************************************************* TEST(test_double_gamma_encode) {