From 82040c1788236e72674f54a8c74acdde71fd605c Mon Sep 17 00:00:00 2001 From: Chip Hogg Date: Fri, 15 Nov 2024 12:58:22 -0500 Subject: [PATCH] Handle edge case Subtracting from `n` doesn't work in literally every case: it fails for `0` input. --- src/core/include/mp-units/ext/prime.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/include/mp-units/ext/prime.h b/src/core/include/mp-units/ext/prime.h index 918d1967..3c3dc22d 100644 --- a/src/core/include/mp-units/ext/prime.h +++ b/src/core/include/mp-units/ext/prime.h @@ -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,