fix divmod
This commit is contained in:
parent
978a8ad39d
commit
0d3c9e3422
@ -19,7 +19,7 @@ auto DivMod(T dividend, T divisor) -> std::pair<T, T>
|
||||
{
|
||||
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<int>(-2,5) == std::make_pair(-1,3));
|
||||
CHECK(DivMod<int>(2,-5) == std::make_pair(-1,-3));
|
||||
CHECK(DivMod<int>(-2,-5) == std::make_pair(0,-2));
|
||||
|
||||
CHECK(DivMod<int>(-5,-5) == std::make_pair(1,0));
|
||||
CHECK(DivMod<int>(-5,5) == std::make_pair(-1,0));
|
||||
CHECK(DivMod<int>(5,-5) == std::make_pair(-1,0));
|
||||
CHECK(DivMod<int>(-5,-5) == std::make_pair(1,0));
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user