Fixed #7849 (Tokenizer: Wrong simplification of floating point equality comparison)

This commit is contained in:
Frank Zingsheim 2017-05-28 10:53:50 +02:00 committed by Daniel Marjamäki
parent bbe90bdbdb
commit aa937e426d
2 changed files with 21 additions and 1 deletions

View File

@ -4777,9 +4777,14 @@ bool Tokenizer::simplifyConditions()
bool eq = false;
if (MathLib::isInt(op1) && MathLib::isInt(op2))
eq = (MathLib::toLongNumber(op1) == MathLib::toLongNumber(op2));
else
else {
eq = (op1 == op2);
// It is inconclusive whether two unequal float representations are numerically equal
if (!eq && MathLib::isFloat(op1))
cmp = "";
}
if (cmp == "==")
result = eq;
else

View File

@ -2624,6 +2624,21 @@ private:
ASSERT_EQUALS("void f ( ) { ; }", tok(code));
}
{
// #7849
const char code[] =
"void f() {\n"
"if (-1e-2 == -0.01) \n"
" g();\n"
"else\n"
" h();\n"
"}";
// condition cannot be safely be calculated to be true due to numerics
TODO_ASSERT_EQUALS("void f ( ) { g ( ) ; }",
"void f ( ) { if ( -1e-2 == -0.01 ) { g ( ) ; } else { h ( ) ; } }",
tok(code));
}
}
void simplify_condition() {