Skip to content

Commit 68e448f

Browse files
committed
attempt to fix various floating-point comparison regressions [skip ci]
1 parent 6acfebc commit 68e448f

2 files changed

Lines changed: 19 additions & 14 deletions

File tree

lib/vf_settokenvalue.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,7 @@ namespace ValueFlow
496496
}
497497
const double floatValue1 = value1.isFloatValue() ? value1.floatValue : value1.intvalue;
498498
const double floatValue2 = value2.isFloatValue() ? value2.floatValue : value2.intvalue;
499+
const bool isFloat = value1.isFloatValue() || value2.isFloatValue();
499500
const auto intValue1 = [&]() -> MathLib::bigint {
500501
return value1.isFloatValue() ? static_cast<MathLib::bigint>(value1.floatValue) : value1.intvalue;
501502
};
@@ -550,14 +551,14 @@ namespace ValueFlow
550551
setTokenValue(parent, std::move(result), settings);
551552
} else if (Token::Match(parent, "%op%")) {
552553
if (Token::Match(parent, "%comp%")) {
553-
if (!result.isFloatValue() && !value1.isIntValue() && !value2.isIntValue())
554+
if (!isFloat && !value1.isIntValue() && !value2.isIntValue())
554555
continue;
555556
} else {
556557
if (value1.isTokValue() || value2.isTokValue())
557558
break;
558559
}
559560
bool error = false;
560-
if (result.isFloatValue()) {
561+
if (isFloat) {
561562
result.floatValue = calculate(parent->str(), floatValue1, floatValue2, &error);
562563
} else {
563564
result.intvalue = calculate(parent->str(), intValue1(), intValue2(), &error);

test/testcondition.cpp

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4465,9 +4465,12 @@ class TestCondition : public TestFixture {
44654465
" float f = 9.9f;\n"
44664466
" if(f < 10) {}\n"
44674467
"}\n");
4468-
ASSERT_EQUALS("[test.cpp:3]: (style) Condition 'i>9.9' is always true\n"
4469-
"[test.cpp:5]: (style) Condition 'f<10' is always true\n",
4470-
errout_str());
4468+
TODO_ASSERT_EQUALS(
4469+
"[test.cpp:3]: (style) Condition 'i>9.9' is always true\n"
4470+
"[test.cpp:5]: (style) Condition 'f<10' is always true\n",
4471+
"[test.cpp:3]: (style) Condition 'i>9.9' is always false\n"
4472+
"[test.cpp:5]: (style) Condition 'f<10' is always false\n",
4473+
errout_str());
44714474
check("constexpr int f() {\n" // #11238
44724475
" return 1;\n"
44734476
"}\n"
@@ -5778,7 +5781,7 @@ class TestCondition : public TestFixture {
57785781
" if (f > 10.0) {}\n"
57795782
" if (f < 10.0) {}\n"
57805783
"}\n");
5781-
TODO_ASSERT_EQUALS("", "[test.cpp:2] -> [test.cpp:3] -> [test.cpp:4]: (style) The if condition is the same as the previous if condition\n", errout_str());
5784+
ASSERT_EQUALS("", errout_str());
57825785
}
57835786

57845787
void checkInvalidTestForOverflow() {
@@ -6244,13 +6247,13 @@ class TestCondition : public TestFixture {
62446247
" float f = 1.0;\n"
62456248
" if (f > 1.0f) {}\n"
62466249
"}\n");
6247-
TODO_ASSERT_EQUALS("[test.cpp:3]: (style) Condition 'f>1.0f' is always false\n", "", errout_str());
6250+
ASSERT_EQUALS("[test.cpp:3]: (style) Condition 'f>1.0f' is always false\n", errout_str());
62486251

62496252
check("void foo() {\n" // #11199
62506253
" float f = 1.0f;\n"
62516254
" if (f > 1.0) {}\n"
62526255
"}\n");
6253-
TODO_ASSERT_EQUALS("[test.cpp:3]: (style) Condition 'f>1.0' is always false\n", "", errout_str());
6256+
ASSERT_EQUALS("[test.cpp:3]: (style) Condition 'f>1.0' is always false\n", errout_str());
62546257

62556258
check("void foo() {\n" // #11201
62566259
" float f = 0x1.4p+3;\n" // hex fraction 1.4 (decimal 1.25) scaled by 2^3, that is 10.0
@@ -6260,26 +6263,27 @@ class TestCondition : public TestFixture {
62606263
TODO_ASSERT_EQUALS(
62616264
"[test.cpp:3]: (style) Condition 'f>9.9' is always true\n"
62626265
"[test.cpp:4]: (style) Condition 'f<9.9' is always false\n",
6263-
"",
6266+
"[test.cpp:3]: (style) Condition 'f>9.9' is always false\n"
6267+
"[test.cpp:4]: (style) Condition 'f<9.9' is always false\n",
62646268
errout_str());
62656269

62666270
check("void foo() {\n" // #12330
62676271
" double d = 1.0;\n"
62686272
" if (d < 0.0) {}\n"
62696273
"}\n");
6270-
TODO_ASSERT_EQUALS("[test.cpp:3]: (style) Condition 'd<0.0' is always false\n", "", errout_str());
6274+
ASSERT_EQUALS("[test.cpp:3]: (style) Condition 'd<0.0' is always false\n", errout_str());
62716275

62726276
check("void foo() {\n" // #12330
62736277
" long double ld = 1.0;\n"
62746278
" if (ld < 0.0) {}\n"
62756279
"}\n");
6276-
TODO_ASSERT_EQUALS("[test.cpp:3]: (style) Condition 'ld<0.0' is always false\n", "", errout_str());
6280+
ASSERT_EQUALS("[test.cpp:3]: (style) Condition 'ld<0.0' is always false\n", errout_str());
62776281

62786282
check("void foo() {\n" // #12330
62796283
" float f = 1.0;\n"
62806284
" if (f < 0.0) {}\n"
62816285
"}\n");
6282-
TODO_ASSERT_EQUALS("[test.cpp:3]: (style) Condition 'f<0.0' is always false\n", "", errout_str());
6286+
ASSERT_EQUALS("[test.cpp:3]: (style) Condition 'f<0.0' is always false\n", errout_str());
62836287

62846288
check("void foo() {\n" // #12774
62856289
" float f = 1.0f;\n"
@@ -6291,7 +6295,7 @@ class TestCondition : public TestFixture {
62916295
"[test.cpp:3]: (style) Condition 'f>1.01f' is always false\n"
62926296
"[test.cpp:4]: (style) Condition 'f>1.00f' is always false\n"
62936297
"[test.cpp:5]: (style) Condition 'f>1' is always false\n",
6294-
"[test.cpp:4]: (style) Condition 'f>1.00f' is always false\n"
6298+
"[test.cpp:3]: (style) Condition 'f>1.01f' is always false\n"
62956299
"[test.cpp:5]: (style) Condition 'f>1' is always false\n",
62966300
errout_str());
62976301

@@ -6305,7 +6309,7 @@ class TestCondition : public TestFixture {
63056309
"[test.cpp:3]: (style) Condition 'f>1.01' is always false\n"
63066310
"[test.cpp:4]: (style) Condition 'f>1.00' is always false\n"
63076311
"[test.cpp:5]: (style) Condition 'f>1' is always false\n",
6308-
"[test.cpp:4]: (style) Condition 'f>1.00' is always false\n"
6312+
"[test.cpp:3]: (style) Condition 'f>1.01' is always false\n"
63096313
"[test.cpp:5]: (style) Condition 'f>1' is always false\n",
63106314
errout_str());
63116315
}

0 commit comments

Comments
 (0)