Skip to content

Commit 818d6b8

Browse files
Fix gamma.h
Add tests for gamma decode
1 parent 293c7df commit 818d6b8

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

include/etl/gamma.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,10 @@ namespace etl
6363
//*********************************
6464
TInput operator ()(TInput value) const
6565
{
66-
return TInput(TInput(maximum * pow(double(value) / maximum, one_over_gamma)));
66+
// Calculate intermediate result because rounding + optimization
67+
// lead to wrong values when returning directly (test on i386)
68+
const double result = maximum * pow(double(value) / maximum, one_over_gamma);
69+
return TInput(result);
6770
}
6871

6972
private:
@@ -95,7 +98,10 @@ namespace etl
9598
//*********************************
9699
TInput operator ()(TInput value) const
97100
{
98-
return TInput(TInput(maximum * pow(double(value) / maximum, gamma)));
101+
// Calculate intermediate result because rounding + optimization
102+
// lead to wrong values when returning directly (test on i386)
103+
const double result = maximum * pow(double(value) / maximum, gamma);
104+
return TInput(result);
99105
}
100106

101107
private:

test/test_gamma.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ namespace
4141
using IntGammaEncode = etl::gamma_encode<int>;
4242
using DoubleGammaEncode = etl::gamma_encode<double>;
4343

44+
using IntGammaDecode = etl::gamma_decode<int>;
4445
using DoubleGammaDecode = etl::gamma_decode<double>;
4546

4647
struct Compare
@@ -62,6 +63,16 @@ namespace
6263
0, 0, 0, 1, 1, 2, 4, 5, 7, 9
6364
};
6465

66+
const std::array<int, Size> input1b =
67+
{
68+
0, 1, 2, 3, 4, 5, 6, 7, 8, 9
69+
};
70+
71+
const std::array<int, Size> result1b =
72+
{
73+
0, 3, 4, 5, 6, 6, 7, 7, 8, 9
74+
};
75+
6576
std::array<int, Size> output1;
6677

6778
//***********************************
@@ -100,6 +111,17 @@ namespace
100111
CHECK(isEqual);
101112
}
102113

114+
//*************************************************************************
115+
TEST(test_int_gamma_decode)
116+
{
117+
IntGammaDecode gamma(0.5, 9);
118+
119+
std::transform(input1b.begin(), input1b.end(), output1.begin(), gamma);
120+
121+
bool isEqual = std::equal(output1.begin(), output1.end(), result1b.begin());
122+
CHECK(isEqual);
123+
}
124+
103125
//*************************************************************************
104126
TEST(test_double_gamma_encode)
105127
{

0 commit comments

Comments
 (0)