Fixed #4016 (false positive: incorrectLogicOperator)

This commit is contained in:
Daniel Marjamäki 2012-08-14 16:17:18 +02:00
parent e534ee0426
commit 1601baa974
3 changed files with 21 additions and 2 deletions

View File

@ -198,7 +198,15 @@ std::string MathLib::add(const std::string & first, const std::string & second)
if (MathLib::isInt(first) && MathLib::isInt(second)) { if (MathLib::isInt(first) && MathLib::isInt(second)) {
return toString<bigint>(toLongNumber(first) + toLongNumber(second)); return toString<bigint>(toLongNumber(first) + toLongNumber(second));
} }
return toString<double>(toDoubleNumber(first) + toDoubleNumber(second));
double d1 = toDoubleNumber(first);
double d2 = toDoubleNumber(second);
while (d1 > 100000.0 * d2 && toString<double>(d1+d2)==first)
d2 *= 10.0;
while (d2 > 100000.0 * d1 && toString<double>(d1+d2)==second)
d1 *= 10.0;
return toString<double>(d1 + d2);
} }
std::string MathLib::subtract(const std::string &first, const std::string &second) std::string MathLib::subtract(const std::string &first, const std::string &second)
@ -206,7 +214,15 @@ std::string MathLib::subtract(const std::string &first, const std::string &secon
if (MathLib::isInt(first) && MathLib::isInt(second)) { if (MathLib::isInt(first) && MathLib::isInt(second)) {
return toString<bigint>(toLongNumber(first) - toLongNumber(second)); return toString<bigint>(toLongNumber(first) - toLongNumber(second));
} }
return toString<double>(toDoubleNumber(first) - toDoubleNumber(second));
double d1 = toDoubleNumber(first);
double d2 = toDoubleNumber(second);
while (d1 > 100000.0 * d2 && toString<double>(d1-d2)==first)
d2 *= 10.0;
while (d2 > 100000.0 * d1 && toString<double>(d1-d2)==second)
d1 *= 10.0;
return toString<double>(d1 - d2);
} }
std::string MathLib::divide(const std::string &first, const std::string &second) std::string MathLib::divide(const std::string &first, const std::string &second)

View File

@ -39,6 +39,7 @@ public:
template<typename T> template<typename T>
static std::string toString(const T &d) { static std::string toString(const T &d) {
std::ostringstream result; std::ostringstream result;
result.precision(8);
result << d; result << d;
if (isNullValue(result.str())) if (isNullValue(result.str()))
return std::string("0"); return std::string("0");

View File

@ -87,6 +87,7 @@ private:
ASSERT_EQUALS("-1" , MathLib::add("0", "-1")); ASSERT_EQUALS("-1" , MathLib::add("0", "-1"));
ASSERT_EQUALS("1" , MathLib::add("1", "0")); ASSERT_EQUALS("1" , MathLib::add("1", "0"));
ASSERT_EQUALS("0" , MathLib::add("0", "0.")); ASSERT_EQUALS("0" , MathLib::add("0", "0."));
ASSERT_EQUALS("1.0000001" , MathLib::add("1", "0.00000001")); // #4016
// subtraction // subtraction
ASSERT_EQUALS("254", MathLib::subtract("0xff", "1")); ASSERT_EQUALS("254", MathLib::subtract("0xff", "1"));
@ -96,6 +97,7 @@ private:
ASSERT_EQUALS("1" , MathLib::subtract("0", "-1")); ASSERT_EQUALS("1" , MathLib::subtract("0", "-1"));
ASSERT_EQUALS("1" , MathLib::subtract("1", "0")); ASSERT_EQUALS("1" , MathLib::subtract("1", "0"));
ASSERT_EQUALS("0" , MathLib::subtract("0", "0.")); ASSERT_EQUALS("0" , MathLib::subtract("0", "0."));
ASSERT_EQUALS("0.99999999" , MathLib::subtract("1", "0.00000001")); // #4016
// multiply // multiply
ASSERT_EQUALS("-0.003" , MathLib::multiply("-1e-3", "3")); ASSERT_EQUALS("-0.003" , MathLib::multiply("-1e-3", "3"));