Fixed #4068 (Endless loop inside MathLib::add())

This commit is contained in:
Daniel Marjamäki 2012-08-22 20:50:39 +02:00
parent f133c9e8ec
commit d24badbfda
2 changed files with 10 additions and 4 deletions

View File

@ -201,9 +201,11 @@ std::string MathLib::add(const std::string & first, const std::string & second)
double d1 = toDoubleNumber(first);
double d2 = toDoubleNumber(second);
while (d1 > 100000.0 * d2 && toString<double>(d1+d2)==first)
int count = 0;
while (d1 > 100000.0 * d2 && toString<double>(d1+d2)==first && ++count<5)
d2 *= 10.0;
while (d2 > 100000.0 * d1 && toString<double>(d1+d2)==second)
while (d2 > 100000.0 * d1 && toString<double>(d1+d2)==second && ++count<5)
d1 *= 10.0;
return toString<double>(d1 + d2);
@ -217,9 +219,11 @@ std::string MathLib::subtract(const std::string &first, const std::string &secon
double d1 = toDoubleNumber(first);
double d2 = toDoubleNumber(second);
while (d1 > 100000.0 * d2 && toString<double>(d1-d2)==first)
int count = 0;
while (d1 > 100000.0 * d2 && toString<double>(d1-d2)==first && ++count<5)
d2 *= 10.0;
while (d2 > 100000.0 * d1 && toString<double>(d1-d2)==second)
while (d2 > 100000.0 * d1 && toString<double>(d1-d2)==second && ++count<5)
d1 *= 10.0;
return toString<double>(d1 - d2);

View File

@ -88,6 +88,7 @@ private:
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
// subtraction
ASSERT_EQUALS("254", MathLib::subtract("0xff", "1"));
@ -98,6 +99,7 @@ private:
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("30666.22" , MathLib::subtract("30666.22", "0.0")); // #4068
// multiply
ASSERT_EQUALS("-0.003" , MathLib::multiply("-1e-3", "3"));