From 0d3c9e3422a0a154ec99f0c3f8e35fca82768ed0 Mon Sep 17 00:00:00 2001 From: Eric Mertens Date: Fri, 13 Jan 2023 17:03:44 -0800 Subject: [PATCH] fix divmod --- 2022/25.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/2022/25.cpp b/2022/25.cpp index 633b450..013117e 100644 --- a/2022/25.cpp +++ b/2022/25.cpp @@ -19,7 +19,7 @@ auto DivMod(T dividend, T divisor) -> std::pair { auto const quotient = dividend / divisor; auto const remainder = dividend % divisor; - if ((remainder >= 0) == (divisor >= 0)) { + if (remainder == 0 || (remainder > 0) == (divisor > 0)) { return {quotient, remainder}; } else { return {quotient - 1, remainder + divisor}; @@ -101,6 +101,11 @@ TEST_SUITE("2022-25 examples") { CHECK(DivMod(-2,5) == std::make_pair(-1,3)); CHECK(DivMod(2,-5) == std::make_pair(-1,-3)); CHECK(DivMod(-2,-5) == std::make_pair(0,-2)); + + CHECK(DivMod(-5,-5) == std::make_pair(1,0)); + CHECK(DivMod(-5,5) == std::make_pair(-1,0)); + CHECK(DivMod(5,-5) == std::make_pair(-1,0)); + CHECK(DivMod(-5,-5) == std::make_pair(1,0)); } }