diff --git a/lib/mathlib.cpp b/lib/mathlib.cpp index 2c67426ff..41541acb1 100644 --- a/lib/mathlib.cpp +++ b/lib/mathlib.cpp @@ -27,6 +27,20 @@ #include #include +template<> +std::string MathLib::toString(const double& d) +{ + std::ostringstream result; + result.precision(8); + result << d; + std::string temp = result.str(); + if (isNullValue(temp)) + return std::string("0.0"); + if (temp.find('.') == std::string::npos) + return temp + ".0"; + return temp; +} + MathLib::bigint MathLib::toLongNumber(const std::string &str) { // hexadecimal numbers: @@ -217,9 +231,6 @@ std::string MathLib::subtract(const std::string &first, const std::string &secon return toString(toLongNumber(first) - toLongNumber(second)); } - if (first == second) - return "0.0" ; - double d1 = toDoubleNumber(first); double d2 = toDoubleNumber(second); diff --git a/lib/mathlib.h b/lib/mathlib.h index 4c5a551c2..11ed77fa1 100644 --- a/lib/mathlib.h +++ b/lib/mathlib.h @@ -37,12 +37,9 @@ public: static double toDoubleNumber(const std::string & str); template - static std::string toString(const T &d) { + static std::string toString(const T& d) { std::ostringstream result; - result.precision(8); - result << d; - if (isNullValue(result.str())) - return std::string("0"); + result << std::noshowpos << std::dec << d; return result.str(); } @@ -78,6 +75,9 @@ public: static bool isOctalDigit(char c); }; +template<> +CPPCHECKLIB std::string MathLib::toString(const double& d); + /// @} #endif diff --git a/test/testmathlib.cpp b/test/testmathlib.cpp index 2837fb7cc..563af266d 100644 --- a/test/testmathlib.cpp +++ b/test/testmathlib.cpp @@ -83,22 +83,22 @@ private: ASSERT_EQUALS("256", MathLib::add("0xff", "1")); ASSERT_EQUALS("249", MathLib::add("250", "-1")); ASSERT_EQUALS("251", MathLib::add("250", "1")); - ASSERT_EQUALS("-2" , MathLib::add("-1.", "-1")); + ASSERT_EQUALS("-2.0", MathLib::add("-1.", "-1")); ASSERT_EQUALS("-1" , MathLib::add("0", "-1")); ASSERT_EQUALS("1" , MathLib::add("1", "0")); - ASSERT_EQUALS("0" , MathLib::add("0", "0.")); - ASSERT_EQUALS("1.0000001" , MathLib::add("1", "0.00000001")); // #4016 - ASSERT_EQUALS("30666.22" , MathLib::add("30666.22", "0.0")); // #4068 + ASSERT_EQUALS("0.0", MathLib::add("0", "0.")); + ASSERT_EQUALS("1.0000001", MathLib::add("1", "0.00000001")); // #4016 + ASSERT_EQUALS("30666.22" , MathLib::add("30666.22", "0.0")); // #4068 // subtraction ASSERT_EQUALS("254", MathLib::subtract("0xff", "1")); ASSERT_EQUALS("251", MathLib::subtract("250", "-1")); ASSERT_EQUALS("249", MathLib::subtract("250", "1")); - ASSERT_EQUALS("0" , MathLib::subtract("-1.", "-1")); + ASSERT_EQUALS("0.0", MathLib::subtract("-1.", "-1")); ASSERT_EQUALS("1" , MathLib::subtract("0", "-1")); ASSERT_EQUALS("1" , MathLib::subtract("1", "0")); - ASSERT_EQUALS("0" , MathLib::subtract("0", "0.")); - ASSERT_EQUALS("0.99999999" , MathLib::subtract("1", "0.00000001")); // #4016 + ASSERT_EQUALS("0.0", MathLib::subtract("0", "0.")); + ASSERT_EQUALS("0.99999999", MathLib::subtract("1", "0.00000001")); // #4016 ASSERT_EQUALS("30666.22" , MathLib::subtract("30666.22", "0.0")); // #4068 // multiply @@ -106,11 +106,11 @@ private: ASSERT_EQUALS("-11.96" , MathLib::multiply("-2.3", "5.2")); ASSERT_EQUALS("3000" , MathLib::multiply("1E3", "3")); ASSERT_EQUALS("3000" , MathLib::multiply("1E+3", "3")); - ASSERT_EQUALS("3000" , MathLib::multiply("1.0E3", "3")); - ASSERT_EQUALS("-3000" , MathLib::multiply("-1.0E3", "3")); - ASSERT_EQUALS("-3000" , MathLib::multiply("-1.0E+3", "3")); - ASSERT_EQUALS("0" , MathLib::multiply("-1.0E+3", "0")); - ASSERT_EQUALS("0" , MathLib::multiply("+1.0E+3", "0")); + ASSERT_EQUALS("3000.0" , MathLib::multiply("1.0E3", "3")); + ASSERT_EQUALS("-3000.0" , MathLib::multiply("-1.0E3", "3")); + ASSERT_EQUALS("-3000.0" , MathLib::multiply("-1.0E+3", "3")); + ASSERT_EQUALS("0.0" , MathLib::multiply("-1.0E+3", "0")); + ASSERT_EQUALS("0.0" , MathLib::multiply("+1.0E+3", "0")); ASSERT_EQUALS("2147483648" , MathLib::multiply("2","1073741824")); ASSERT_EQUALS("536870912" , MathLib::multiply("512","1048576")); @@ -120,10 +120,9 @@ private: ASSERT_EQUALS("5" , MathLib::divide("-10", "-2")); ASSERT_EQUALS("-2.5", MathLib::divide("-10.", "4")); ASSERT_EQUALS("2.5" , MathLib::divide("-10.", "-4")); - ASSERT_EQUALS("5" , MathLib::divide("25.5", "5.1")); - ASSERT_EQUALS("7" , MathLib::divide("21.", "3")); + ASSERT_EQUALS("5.0" , MathLib::divide("25.5", "5.1")); + ASSERT_EQUALS("7.0" , MathLib::divide("21.", "3")); ASSERT_EQUALS("1" , MathLib::divide("3", "2")); - ASSERT_EQUALS("0.0" , MathLib::subtract("0.0", "0.0")); // Unknown action should throw exception ASSERT_THROW(MathLib::calculate("1","2",'j'),InternalError); @@ -131,10 +130,10 @@ private: void calculate1() const { // mod ASSERT_EQUALS("0" , MathLib::calculate("2" , "1" , '%')); - ASSERT_EQUALS("0" , MathLib::calculate("2.0" , "1.0" , '%')); + ASSERT_EQUALS("0.0" , MathLib::calculate("2.0" , "1.0" , '%')); ASSERT_EQUALS("2" , MathLib::calculate("12" , "5" , '%')); ASSERT_EQUALS("1" , MathLib::calculate("100" , "3" , '%')); - ASSERT_EQUALS("12" , MathLib::calculate("12.0" , "13.0" , '%')); + ASSERT_EQUALS("12.0" , MathLib::calculate("12.0" , "13.0" , '%')); ASSERT_EQUALS("1.3" , MathLib::calculate("5.3" , "2.0" , '%')); ASSERT_EQUALS("1.7" , MathLib::calculate("18.5" , "4.2" , '%')); }