#5875 Fix 'lib/mathlib.cpp:559:43: runtime error: division by zero'. Several fixes to MathLib::divide(): avoid division by zero, handle various representation of 0 value, use 'nan.' instead of '-nan.0'
This commit is contained in:
parent
46bf23aa6d
commit
6f31ce334d
|
@ -549,12 +549,12 @@ std::string MathLib::divide(const std::string &first, const std::string &second)
|
|||
if (b == 0)
|
||||
throw InternalError(0, "Internal Error: Division by zero");
|
||||
return toString(toLongNumber(first) / b);
|
||||
} else if (second == "0.0") {
|
||||
if (first=="0.0" || first=="+0.0")
|
||||
} else if (isNullValue(second)) {
|
||||
if (isNullValue(first))
|
||||
return "nan.0";
|
||||
if (first=="-0.0")
|
||||
return "-nan.0";
|
||||
return (first[0] == '-') ? "-inf.0" : "inf.0";
|
||||
const int sign_first = (isPositive(first)) ? 1 : -1;
|
||||
const int sign_second = (isPositive(second)) ? 1 : -1;
|
||||
return (sign_first*sign_second == 1) ? "inf.0" : "-inf.0";
|
||||
}
|
||||
return toString(toDoubleNumber(first) / toDoubleNumber(second));
|
||||
}
|
||||
|
|
|
@ -599,8 +599,18 @@ private:
|
|||
|
||||
void naninf() const {
|
||||
ASSERT_EQUALS("nan.0", MathLib::divide("0.0", "0.0")); // nan
|
||||
ASSERT_EQUALS("nan.0", MathLib::divide("0.0", "0.f")); // nan (#5875)
|
||||
ASSERT_EQUALS("nan.0", MathLib::divide("-0.0", "0.f")); // nan (#5875)
|
||||
ASSERT_EQUALS("nan.0", MathLib::divide("-0.f", "0.f")); // nan (#5875)
|
||||
ASSERT_EQUALS("nan.0", MathLib::divide("-0.0", "-0.f")); // nan (#5875)
|
||||
ASSERT_EQUALS("nan.0", MathLib::divide("-.0", "-0.f")); // nan (#5875)
|
||||
ASSERT_EQUALS("nan.0", MathLib::divide("0.0", "-0.f")); // nan (#5875)
|
||||
ASSERT_EQUALS("nan.0", MathLib::divide("0.f", "-0.f")); // nan (#5875)
|
||||
ASSERT_EQUALS("inf.0", MathLib::divide("3.0", "0.0")); // inf
|
||||
ASSERT_EQUALS("inf.0", MathLib::divide("3.0", "0.f")); // inf (#5875)
|
||||
ASSERT_EQUALS("-inf.0", MathLib::divide("-3.0", "0.0")); // -inf (#5142)
|
||||
ASSERT_EQUALS("-inf.0", MathLib::divide("-3.0", "0.0f")); // -inf (#5142)
|
||||
ASSERT_EQUALS("inf.0", MathLib::divide("-3.0", "-0.0f")); // inf (#5142)
|
||||
}
|
||||
|
||||
void isNullValue() const {
|
||||
|
|
Loading…
Reference in New Issue