diff --git a/lib/mathlib.cpp b/lib/mathlib.cpp index 011423c35..a98f7480c 100644 --- a/lib/mathlib.cpp +++ b/lib/mathlib.cpp @@ -287,6 +287,10 @@ std::string MathLib::divide(const std::string &first, const std::string &second) 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") + return "nan.0"; + if (first=="-0.0") + return "-nan.0"; return (first[0] == '-') ? "-inf.0" : "inf.0"; } return toString(toDoubleNumber(first) / toDoubleNumber(second)); diff --git a/test/testmathlib.cpp b/test/testmathlib.cpp index 27882cf5c..c4afa63d0 100644 --- a/test/testmathlib.cpp +++ b/test/testmathlib.cpp @@ -375,7 +375,7 @@ private: } void naninf() { - ASSERT_EQUALS("inf.0", MathLib::divide("0.0", "0.0")); // nan + ASSERT_EQUALS("nan.0", MathLib::divide("0.0", "0.0")); // nan ASSERT_EQUALS("inf.0", MathLib::divide("3.0", "0.0")); // inf ASSERT_EQUALS("-inf.0", MathLib::divide("-3.0", "0.0")); // -inf (#5142) }