diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 9ae03afcb..29c52e771 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -2562,6 +2562,13 @@ void CheckOther::checkMathFunctions() std::fabs(MathLib::toDoubleNumber(tok->tokAt(2)->str())) > 1.0) { mathfunctionCallError(tok); + } + // atan2 ( x , y): x and y can not be zero, because this is mathematically not defined + else if (Token::Match(tok, "atan2 ( %num% , %num% )") && + MathLib::isNullValue(tok->tokAt(2)->str()) && + MathLib::isNullValue(tok->tokAt(4)->str())) + { + mathfunctionCallError(tok,2); } } } @@ -2710,10 +2717,15 @@ void CheckOther::zerodivError(const Token *tok) reportError(tok, Severity::error, "zerodiv", "Division by zero"); } -void CheckOther::mathfunctionCallError(const Token *tok) +void CheckOther::mathfunctionCallError(const Token *tok, const unsigned int numParam) { if (tok) - reportError(tok, Severity::error, "wrongmathcall", "Passing value " + tok->tokAt(2)->str() + " to " + tok->str() + "() leads to undefined result"); + { + if(numParam == 1) + reportError(tok, Severity::error, "wrongmathcall", "Passing value " + tok->tokAt(2)->str() + " to " + tok->str() + "() leads to undefined result"); + else if (numParam == 2) + reportError(tok, Severity::error, "wrongmathcall", "Passing value " + tok->tokAt(2)->str() + " and " + tok->tokAt(4)->str() + " to " + tok->str() + "() leads to undefined result"); + } else reportError(tok, Severity::error, "wrongmathcall", "Passing value " " to " "() leads to undefined result"); } diff --git a/lib/checkother.h b/lib/checkother.h index 06f6c55db..c763735f9 100644 --- a/lib/checkother.h +++ b/lib/checkother.h @@ -189,7 +189,7 @@ public: void uninitdataError(const Token *tok, const std::string &varname); void uninitvarError(const Token *tok, const std::string &varname); void zerodivError(const Token *tok); - void mathfunctionCallError(const Token *tok); + void mathfunctionCallError(const Token *tok, const unsigned int numParam=1); void postIncrementError(const Token *tok, const std::string &var_name, const bool isIncrement); void getErrorMessages() diff --git a/lib/mathlib.cpp b/lib/mathlib.cpp index 7a5695bb1..14287b7ce 100644 --- a/lib/mathlib.cpp +++ b/lib/mathlib.cpp @@ -300,6 +300,7 @@ bool MathLib::isGreater(const std::string &first, const std::string &second) bool MathLib::isNullValue(const std::string &str) { return (str == "-0" || str == "-0.0" + || str == "0" || str == "-0." || str == "-0E-00" || str == "-0E+00" || str == "+0E+00" || str == "+0E-00" || str == "+0" diff --git a/test/testother.cpp b/test/testother.cpp index b52bd9295..a5023fb19 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -2281,6 +2281,32 @@ private: " std::cout << acos(-110) << std::endl;\n" "}"); ASSERT_EQUALS("[test.cpp:3]: (error) Passing value -110 to acos() leads to undefined result\n", errout.str()); + + + // atan2 + check("void foo()\n" + "{\n" + " std::cout << atan2(1,1) << std::endl;\n" + " std::cout << atan2(-1,-1) << std::endl;\n" + " std::cout << atan2(0.1,1) << std::endl;\n" + " std::cout << atan2(0.0001,100) << std::endl;\n" + " std::cout << atan2(0.01m-1) << std::endl;\n" + " std::cout << atan2(1.0E-1,-3) << std::endl;\n" + " std::cout << atan2(-1.0E-1,+2) << std::endl;\n" + " std::cout << atan2(+1.0E-1,0) << std::endl;\n" + " std::cout << atan2(0.1E-1,3) << std::endl;\n" + " std::cout << atan2(+0.1E-1,1) << std::endl;\n" + " std::cout << atan2(-0.1E-1,8) << std::endl;\n" + "}"); + ASSERT_EQUALS("", errout.str()); + + check("void foo()\n" + "{\n" + " std::cout << atan2(0,0) << std::endl;\n" + "}"); + ASSERT_EQUALS("[test.cpp:3]: (error) Passing value 0 and 0 to atan2() leads to undefined result\n", errout.str()); + + } };