Fixed roundEven implementation, tests are now passing

This commit is contained in:
Christophe Riccio 2011-10-20 18:13:07 +01:00
parent 774bd01b92
commit 6c00ffa1b4
2 changed files with 46 additions and 37 deletions

View File

@ -148,18 +148,21 @@ namespace detail
*/
// roundEven
template <typename genType>
GLM_FUNC_QUALIFIER genType roundEven(genType const& x)
GLM_FUNC_QUALIFIER genType roundEven(genType const & x)
{
GLM_STATIC_ASSERT(detail::type<genType>::is_float, "'roundEven' only accept floating-point inputs");
genType RoundValue(0.5);
if(fract(x) == genType(0.5) && int(x) % 2)
RoundValue = genType(-0.5);
if(x < genType(0.0))
return genType(int(x - RoundValue));
return genType(int(x + RoundValue));
}
int Integer = int(x);
genType IntegerPart = genType(Integer);
genType FractionalPart = fract(x);
if(FractionalPart > genType(0.5) || FractionalPart < genType(0.5))
return round(x);
else if(!(Integer % 2))
return IntegerPart;
else
return IntegerPart + mix(genType(-1), genType(1), x >= genType(0));
}
VECTORIZE_VEC(roundEven)

View File

@ -167,70 +167,76 @@ int test_roundEven()
int Error = 0;
{
float A = glm::round(0.0f);
float A = glm::roundEven(-1.5f);
Error += A == -2.0f ? 0 : 1;
Error += 0;
}
{
float A = glm::roundEven(0.0f);
Error += A == 0.0f ? 0 : 1;
float B = glm::round(0.5f);
float B = glm::roundEven(0.5f);
Error += B == 0.0f ? 0 : 1;
float C = glm::round(1.0f);
float C = glm::roundEven(1.0f);
Error += C == 1.0f ? 0 : 1;
float D = glm::round(0.1f);
float D = glm::roundEven(0.1f);
Error += D == 0.0f ? 0 : 1;
float E = glm::round(0.9f);
float E = glm::roundEven(0.9f);
Error += E == 1.0f ? 0 : 1;
float F = glm::round(1.5f);
float F = glm::roundEven(1.5f);
Error += F == 2.0f ? 0 : 1;
float G = glm::round(1.9f);
float G = glm::roundEven(1.9f);
Error += G == 2.0f ? 0 : 1;
}
{
float A = glm::round(-0.0f);
float A = glm::roundEven(-0.0f);
Error += A == 0.0f ? 0 : 1;
float B = glm::round(-0.5f);
float B = glm::roundEven(-0.5f);
Error += B == -0.0f ? 0 : 1;
float C = glm::round(-1.0f);
float C = glm::roundEven(-1.0f);
Error += C == -1.0f ? 0 : 1;
float D = glm::round(-0.1f);
float D = glm::roundEven(-0.1f);
Error += D == 0.0f ? 0 : 1;
float E = glm::round(-0.9f);
float E = glm::roundEven(-0.9f);
Error += E == -1.0f ? 0 : 1;
float F = glm::round(-1.5f);
float F = glm::roundEven(-1.5f);
Error += F == -2.0f ? 0 : 1;
float G = glm::round(-1.9f);
float G = glm::roundEven(-1.9f);
Error += G == -2.0f ? 0 : 1;
}
{
float A = glm::round(1.5f);
float A = glm::roundEven(1.5f);
Error += A == 2.0f ? 0 : 1;
float B = glm::round(2.5f);
float B = glm::roundEven(2.5f);
Error += B == 2.0f ? 0 : 1;
float C = glm::round(3.5f);
float C = glm::roundEven(3.5f);
Error += C == 4.0f ? 0 : 1;
float D = glm::round(4.5f);
float D = glm::roundEven(4.5f);
Error += D == 4.0f ? 0 : 1;
float E = glm::round(5.5f);
float E = glm::roundEven(5.5f);
Error += E == 6.0f ? 0 : 1;
float F = glm::round(6.5f);
float F = glm::roundEven(6.5f);
Error += F == 6.0f ? 0 : 1;
float G = glm::round(7.5f);
float G = glm::roundEven(7.5f);
Error += G == 8.0f ? 0 : 1;
}
{
float A = glm::round(-1.5f);
float A = glm::roundEven(-1.5f);
Error += A == -2.0f ? 0 : 1;
float B = glm::round(-2.5f);
float B = glm::roundEven(-2.5f);
Error += B == -2.0f ? 0 : 1;
float C = glm::round(-3.5f);
float C = glm::roundEven(-3.5f);
Error += C == -4.0f ? 0 : 1;
float D = glm::round(-4.5f);
float D = glm::roundEven(-4.5f);
Error += D == -4.0f ? 0 : 1;
float E = glm::round(-5.5f);
float E = glm::roundEven(-5.5f);
Error += E == -6.0f ? 0 : 1;
float F = glm::round(-6.5f);
float F = glm::roundEven(-6.5f);
Error += F == -6.0f ? 0 : 1;
float G = glm::round(-7.5f);
float G = glm::roundEven(-7.5f);
Error += G == -8.0f ? 0 : 1;
}