Throw an InternalError when trying to divide (or modulo) by zero in MathLib.

This commit is contained in:
PKEuS 2012-09-06 20:15:32 +02:00
parent 20f989b6c4
commit 0c812c5ac3
2 changed files with 14 additions and 4 deletions

View File

@ -235,7 +235,10 @@ std::string MathLib::subtract(const std::string &first, const std::string &secon
std::string MathLib::divide(const std::string &first, const std::string &second) std::string MathLib::divide(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)); bigint b = toLongNumber(second);
if (b == 0)
throw InternalError(0, "Internal Error: Division by zero");
return toString<bigint>(toLongNumber(first) / b);
} }
return toString<double>(toDoubleNumber(first) / toDoubleNumber(second)); return toString<double>(toDoubleNumber(first) / toDoubleNumber(second));
} }
@ -251,7 +254,10 @@ std::string MathLib::multiply(const std::string &first, const std::string &secon
std::string MathLib::mod(const std::string &first, const std::string &second) std::string MathLib::mod(const std::string &first, const std::string &second)
{ {
if (MathLib::isInt(first) && MathLib::isInt(second)) { if (MathLib::isInt(first) && MathLib::isInt(second)) {
return toString<MathLib::bigint>(toLongNumber(first) % toLongNumber(second)); bigint b = toLongNumber(second);
if (b == 0)
throw InternalError(0, "Internal Error: Division by zero");
return toString<MathLib::bigint>(toLongNumber(first) % b);
} }
return toString<double>(fmod(toDoubleNumber(first),toDoubleNumber(second))); return toString<double>(fmod(toDoubleNumber(first),toDoubleNumber(second)));
} }

View File

@ -100,6 +100,7 @@ private:
ASSERT_EQUALS("0" , MathLib::subtract("0", "0.")); ASSERT_EQUALS("0" , MathLib::subtract("0", "0."));
ASSERT_EQUALS("0.99999999" , MathLib::subtract("1", "0.00000001")); // #4016 ASSERT_EQUALS("0.99999999" , MathLib::subtract("1", "0.00000001")); // #4016
ASSERT_EQUALS("30666.22" , MathLib::subtract("30666.22", "0.0")); // #4068 ASSERT_EQUALS("30666.22" , MathLib::subtract("30666.22", "0.0")); // #4068
ASSERT_EQUALS("0.0" , MathLib::subtract("0.0", "0.0"));
// multiply // multiply
ASSERT_EQUALS("-0.003" , MathLib::multiply("-1e-3", "3")); ASSERT_EQUALS("-0.003" , MathLib::multiply("-1e-3", "3"));
@ -123,7 +124,8 @@ private:
ASSERT_EQUALS("5" , MathLib::divide("25.5", "5.1")); ASSERT_EQUALS("5" , MathLib::divide("25.5", "5.1"));
ASSERT_EQUALS("7" , MathLib::divide("21.", "3")); ASSERT_EQUALS("7" , MathLib::divide("21.", "3"));
ASSERT_EQUALS("1" , MathLib::divide("3", "2")); ASSERT_EQUALS("1" , MathLib::divide("3", "2"));
ASSERT_EQUALS("0.0" , MathLib::subtract("0.0", "0.0")); ASSERT_THROW(MathLib::divide("123", "0"), InternalError); // throw
MathLib::divide("123", "0.0"); // don't throw
// Unknown action should throw exception // Unknown action should throw exception
ASSERT_THROW(MathLib::calculate("1","2",'j'),InternalError); ASSERT_THROW(MathLib::calculate("1","2",'j'),InternalError);
@ -137,6 +139,8 @@ private:
ASSERT_EQUALS("12" , MathLib::calculate("12.0" , "13.0" , '%')); ASSERT_EQUALS("12" , MathLib::calculate("12.0" , "13.0" , '%'));
ASSERT_EQUALS("1.3" , MathLib::calculate("5.3" , "2.0" , '%')); ASSERT_EQUALS("1.3" , MathLib::calculate("5.3" , "2.0" , '%'));
ASSERT_EQUALS("1.7" , MathLib::calculate("18.5" , "4.2" , '%')); ASSERT_EQUALS("1.7" , MathLib::calculate("18.5" , "4.2" , '%'));
ASSERT_THROW(MathLib::calculate("123", "0", '%'), InternalError); // throw
MathLib::calculate("123", "0.0", '%'); // don't throw
} }
void convert() const { void convert() const {