Skip to content

Commit 794aebe

Browse files
committed
MathLib: Return inf.0 for NAN and INF calculations
1 parent f02e885 commit 794aebe

2 files changed

Lines changed: 9 additions & 0 deletions

File tree

lib/mathlib.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,8 @@ std::string MathLib::divide(const std::string &first, const std::string &second)
288288
if (b == 0)
289289
throw InternalError(0, "Internal Error: Division by zero");
290290
return longToString(toLongNumber(first) / b);
291+
} else if (second == "0.0") {
292+
return "inf.0";
291293
}
292294
return doubleToString(toDoubleNumber(first) / toDoubleNumber(second));
293295
}

test/testmathlib.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class TestMathLib : public TestFixture {
4141
TEST_CASE(isNotEqual)
4242
TEST_CASE(isLess)
4343
TEST_CASE(isLessEqual)
44+
TEST_CASE(naninf)
4445
}
4546

4647
void isGreater() const {
@@ -344,6 +345,12 @@ class TestMathLib : public TestFixture {
344345
ASSERT_EQUALS(true , MathLib::isFloat("1.0E-1"));
345346
ASSERT_EQUALS(true , MathLib::isFloat("-1.0E+1"));
346347
}
348+
349+
void naninf() {
350+
ASSERT_EQUALS("inf.0", MathLib::divide("0.0", "0.0")); // nan
351+
ASSERT_EQUALS("inf.0", MathLib::divide("3.0", "0.0")); // inf
352+
ASSERT_EQUALS("inf.0", MathLib::divide("-3.0", "0.0")); // -inf
353+
}
347354
};
348355

349356
REGISTER_TEST(TestMathLib)

0 commit comments

Comments
 (0)