Skip to content

Commit 848c7dc

Browse files
committed
style: use int64_t to avoid narrowing conversions
1 parent 27a4bb4 commit 848c7dc

File tree

1 file changed

+22
-23
lines changed

1 file changed

+22
-23
lines changed

math/ncr_modulo_p.cpp

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,16 @@ namespace utils {
3333
* equation
3434
* @returns the gcd of a and b
3535
*/
36-
uint64_t gcdExtended(const uint64_t& a, const uint64_t& b, int64_t& x,
37-
int64_t& y) {
36+
int64_t gcdExtended(const int64_t& a, const int64_t& b, int64_t& x,
37+
int64_t& y) {
3838
if (a == 0) {
3939
x = 0;
4040
y = 1;
4141
return b;
4242
}
4343

4444
int64_t x1 = 0, y1 = 0;
45-
uint64_t gcd = gcdExtended(b % a, a, x1, y1);
45+
const int64_t gcd = gcdExtended(b % a, a, x1, y1);
4646

4747
x = y1 - (b / a) * x1;
4848
y = x1;
@@ -54,14 +54,13 @@ uint64_t gcdExtended(const uint64_t& a, const uint64_t& b, int64_t& x,
5454
* @params[in] the numbers 'a' and 'm' from above equation
5555
* @returns the modular inverse of a
5656
*/
57-
int64_t modInverse(const uint64_t& a, const uint64_t& m) {
57+
int64_t modInverse(const int64_t& a, const int64_t& m) {
5858
int64_t x = 0, y = 0;
59-
uint64_t g = gcdExtended(a, m, x, y);
59+
const int64_t g = gcdExtended(a, m, x, y);
6060
if (g != 1) { // modular inverse doesn't exist
6161
return -1;
6262
} else {
63-
int64_t res = ((x + m) % m);
64-
return res;
63+
return ((x + m) % m);
6564
}
6665
}
6766
}; // namespace utils
@@ -70,8 +69,8 @@ int64_t modInverse(const uint64_t& a, const uint64_t& m) {
7069
*/
7170
class NCRModuloP {
7271
private:
73-
const uint64_t p; /// the p from (nCr % p)
74-
const std::vector<uint64_t>
72+
const int64_t p; /// the p from (nCr % p)
73+
const std::vector<int64_t>
7574
fac; /// stores precomputed factorial(i) % p value
7675

7776
/**
@@ -81,11 +80,11 @@ class NCRModuloP {
8180
* @return vector storing factorials of the numbers 0, ..., max_arg_val
8281
* reduced modulo mod
8382
*/
84-
static std::vector<uint64_t> computeFactorialsMod(
85-
const uint64_t& max_arg_val, const uint64_t& mod) {
86-
auto res = std::vector<uint64_t>(max_arg_val + 1);
83+
static std::vector<int64_t> computeFactorialsMod(const int64_t& max_arg_val,
84+
const int64_t& mod) {
85+
auto res = std::vector<int64_t>(max_arg_val + 1);
8786
res[0] = 1;
88-
for (uint64_t i = 1; i <= max_arg_val; i++) {
87+
for (int64_t i = 1; i <= max_arg_val; i++) {
8988
res[i] = (res[i - 1] * i) % mod;
9089
}
9190
return res;
@@ -96,15 +95,15 @@ class NCRModuloP {
9695
* and stores them in vector 'fac'
9796
* @params[in] the numbers 'size', 'mod'
9897
*/
99-
NCRModuloP(const uint64_t& size, const uint64_t& mod)
98+
NCRModuloP(const int64_t& size, const int64_t& mod)
10099
: p(mod), fac(computeFactorialsMod(size, mod)) {}
101100

102101
/** Find nCr % p
103102
*
104103
* @params[in] the numbers 'n', 'r' and 'p'
105104
* @returns the value nCr % p
106105
*/
107-
int64_t ncr(const uint64_t& n, const uint64_t& r) const {
106+
int64_t ncr(const int64_t& n, const int64_t& r) const {
108107
// Base cases
109108
if (r > n) {
110109
return 0;
@@ -135,14 +134,14 @@ class NCRModuloP {
135134
*/
136135
static void tests() {
137136
struct TestCase {
138-
const uint64_t size;
139-
const uint64_t p;
140-
const uint64_t n;
141-
const uint64_t r;
137+
const int64_t size;
138+
const int64_t p;
139+
const int64_t n;
140+
const int64_t r;
142141
const int64_t expected;
143142

144-
TestCase(const uint64_t size, const uint64_t p, const uint64_t n,
145-
const uint64_t r, const int64_t expected)
143+
TestCase(const int64_t size, const int64_t p, const int64_t n,
144+
const int64_t r, const int64_t expected)
146145
: size(size), p(p), n(n), r(r), expected(expected) {}
147146
};
148147
const std::vector<TestCase> test_cases = {
@@ -167,8 +166,8 @@ static void tests() {
167166
*/
168167
int main() {
169168
// populate the fac array
170-
const uint64_t size = 1e6 + 1;
171-
const uint64_t p = 1e9 + 7;
169+
const int64_t size = 1e6 + 1;
170+
const int64_t p = 1e9 + 7;
172171
const auto ncrObj = math::ncr_modulo_p::NCRModuloP(size, p);
173172
// test 6Ci for i=0 to 7
174173
for (int i = 0; i <= 7; i++) {

0 commit comments

Comments
 (0)