Handle edge case

Subtracting from `n` doesn't work in literally every case: it fails for
`0` input.
This commit is contained in:
Chip Hogg
2024-11-15 12:58:22 -05:00
parent d963ce8f93
commit 82040c1788

View File

@@ -100,7 +100,7 @@ namespace mp_units::detail {
return add_mod(
// Transform into "negative space" to make the first parameter as small as possible;
// then, transform back.
n - mul_mod(n % a, num_batches, n),
(n - mul_mod(n % a, num_batches, n)) % n,
// Handle the leftover product (which is guaranteed to fit in the integer type).
(a * (b % batch_size)) % n,