Skip to content

Commit e160e7a

Browse files
authored
Eliminating C/C++ implicit conversions (#987)
1 parent ffab1cb commit e160e7a

File tree

15 files changed

+60
-59
lines changed

15 files changed

+60
-59
lines changed

SConstruct

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ for language, tools in languages_to_import.items():
6060

6161
Export('env')
6262

63-
env['CCFLAGS'] = '-Wall -Wextra -Werror -pedantic'
63+
env['CCFLAGS'] = '-Wall -Wextra -Werror -pedantic -Wconversion'
6464
env['CFLAGS'] = '-std=gnu99'
6565
env['CXXFLAGS'] = '-std=c++17 -Wold-style-cast'
6666
env['ASFLAGS'] = '--64'

contents/IFS/code/c/IFS.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,18 @@ void chaos_game(struct point *in, size_t in_n, struct point *out,
2929
}
3030

3131
int main() {
32-
const int point_count = 10000;
32+
const size_t point_count = 10000;
3333

3434
struct point shape_points [3] = {{0.0,0.0}, {0.5,sqrt(0.75)}, {1.0,0.0}};
3535
struct point out_points[point_count];
3636

37-
srand(time(NULL));
37+
srand((unsigned int)time(NULL));
3838

3939
chaos_game(shape_points, 3, out_points, point_count);
4040

4141
FILE *fp = fopen("sierpinksi.dat", "w+");
4242

43-
for (int i = 0; i < point_count; ++i) {
43+
for (size_t i = 0; i < point_count; ++i) {
4444
fprintf(fp, "%f\t%f\n", out_points[i].x, out_points[i].y);
4545
}
4646

contents/approximate_counting/code/c/approximate_counting.c

+5-4
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ double increment(double v, double a)
4040
// It returns n(v, a), the approximate count
4141
double approximate_count(size_t n_items, double a)
4242
{
43-
int v = 0;
43+
double v = 0;
4444
for (size_t i = 0; i < n_items; ++i) {
4545
v = increment(v, a);
4646
}
@@ -61,9 +61,10 @@ void test_approximation_count(size_t n_trials, size_t n_items, double a,
6161
for (size_t i = 0; i < n_trials; ++i) {
6262
sum += approximate_count(n_items, a);
6363
}
64-
double avg = sum / n_trials;
64+
double avg = sum / (double)n_trials;
6565

66-
if (fabs((avg - n_items) / n_items) < threshold){
66+
double items = (double)n_items;
67+
if (fabs((avg - items) / items) < threshold){
6768
printf("passed\n");
6869
}
6970
else{
@@ -73,7 +74,7 @@ void test_approximation_count(size_t n_trials, size_t n_items, double a,
7374

7475
int main()
7576
{
76-
srand(time(NULL));
77+
srand((unsigned int)time(NULL));
7778

7879
printf("[#]\nCounting Tests, 100 trials\n");
7980
printf("[#]\ntesting 1,000, a = 30, 10%% error\n");

contents/cooley_tukey/code/c/fft.c

+8-8
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ void fft(double complex *x, size_t n) {
1111
memset(y, 0, sizeof(y));
1212
fftw_plan p;
1313

14-
p = fftw_plan_dft_1d(n, (fftw_complex*)x, (fftw_complex*)y,
14+
p = fftw_plan_dft_1d((int)n, (fftw_complex*)x, (fftw_complex*)y,
1515
FFTW_FORWARD, FFTW_ESTIMATE);
1616

1717
fftw_execute(p);
@@ -27,7 +27,7 @@ void dft(double complex *X, const size_t N) {
2727
for (size_t i = 0; i < N; ++i) {
2828
tmp[i] = 0;
2929
for (size_t j = 0; j < N; ++j) {
30-
tmp[i] += X[j] * cexp(-2.0 * M_PI * I * j * i / N);
30+
tmp[i] += X[j] * cexp(-2.0 * M_PI * I * (double)j * (double)i / (double)N);
3131
}
3232
}
3333

@@ -49,7 +49,7 @@ void cooley_tukey(double complex *X, const size_t N) {
4949
cooley_tukey(X + N / 2, N / 2);
5050

5151
for (size_t i = 0; i < N / 2; ++i) {
52-
X[i + N / 2] = X[i] - cexp(-2.0 * I * M_PI * i / N) * X[i + N / 2];
52+
X[i + N / 2] = X[i] - cexp(-2.0 * I * M_PI * (double)i / (double)N) * X[i + N / 2];
5353
X[i] -= (X[i + N / 2]-X[i]);
5454
}
5555
}
@@ -58,7 +58,7 @@ void cooley_tukey(double complex *X, const size_t N) {
5858
void bit_reverse(double complex *X, size_t N) {
5959
for (size_t i = 0; i < N; ++i) {
6060
size_t n = i;
61-
int a = i;
61+
size_t a = i;
6262
int count = (int)log2((double)N) - 1;
6363

6464
n >>= 1;
@@ -67,7 +67,7 @@ void bit_reverse(double complex *X, size_t N) {
6767
count--;
6868
n >>= 1;
6969
}
70-
n = (a << count) & ((1 << (int)log2((double)N)) - 1);
70+
n = (a << count) & (size_t)((1 << (size_t)log2((double)N)) - 1);
7171

7272
if (n > i) {
7373
double complex tmp = X[i];
@@ -81,8 +81,8 @@ void iterative_cooley_tukey(double complex *X, size_t N) {
8181
bit_reverse(X, N);
8282

8383
for (int i = 1; i <= log2((double)N); ++i) {
84-
size_t stride = pow(2, i);
85-
double complex w = cexp(-2.0 * I * M_PI / stride);
84+
size_t stride = (size_t)pow(2, i);
85+
double complex w = cexp(-2.0 * I * M_PI / (double)stride);
8686
for (size_t j = 0; j < N; j += stride) {
8787
double complex v = 1.0;
8888
for (size_t k = 0; k < stride / 2; ++k) {
@@ -105,7 +105,7 @@ void approx(double complex *X, double complex *Y, size_t N) {
105105
}
106106

107107
int main() {
108-
srand(time(NULL));
108+
srand((unsigned int)time(NULL));
109109
double complex x[64], y[64], z[64];
110110
for (size_t i = 0; i < 64; ++i) {
111111
x[i] = rand() / (double) RAND_MAX;

contents/cooley_tukey/code/cpp/fft.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ void cooley_tukey(Iter first, Iter last) {
5555

5656
// now combine each of those halves with the butterflies
5757
for (int k = 0; k < size / 2; ++k) {
58-
auto w = std::exp(complex(0, -2.0 * pi * k / size));
58+
auto w = std::exp(complex(0, -2.0 * pi * k / static_cast<double>(size)));
5959

6060
auto& bottom = first[k];
6161
auto& top = first[k + size / 2];
@@ -78,7 +78,7 @@ void sort_by_bit_reverse(Iter first, Iter last) {
7878
b = (((b & 0xcccccccc) >> 2) | ((b & 0x33333333) << 2));
7979
b = (((b & 0xf0f0f0f0) >> 4) | ((b & 0x0f0f0f0f) << 4));
8080
b = (((b & 0xff00ff00) >> 8) | ((b & 0x00ff00ff) << 8));
81-
b = ((b >> 16) | (b << 16)) >> (32 - std::uint32_t(log2(size)));
81+
b = ((b >> 16) | (b << 16)) >> (32 - std::uint32_t(log2(static_cast<double>(size))));
8282
if (b > i) {
8383
swap(first[b], first[i]);
8484
}

contents/forward_euler_method/code/c/euler.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ void solve_euler(double timestep, double *result, size_t n) {
1313
int check_result(double *result, size_t n, double threshold, double timestep) {
1414
int is_approx = 1;
1515
for (size_t i = 0; i < n; ++i) {
16-
double solution = exp(-3.0 * i * timestep);
16+
double solution = exp(-3.0 * (double)i * timestep);
1717
if (fabs(result[i] - solution) > threshold) {
1818
printf("%f %f\n", result[i], solution);
1919
is_approx = 0;

contents/forward_euler_method/code/cpp/euler.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ template <typename Iter>
2727
bool check_result(Iter first, Iter last, double threshold, double timestep) {
2828
auto it = first;
2929
for (size_t idx = 0; it != last; ++idx, ++it) {
30-
double solution = std::exp(-3.0 * idx * timestep);
30+
double solution = std::exp(-3.0 * static_cast<double>(idx) * timestep);
3131
if (std::abs(*it - solution) > threshold) {
3232
std::cout << "We found a value outside the threshold; the " << idx
3333
<< "-th value was " << *it << ", but the expected solution was "

contents/gaussian_elimination/code/cpp/gaussian_elimination.cpp

+12-12
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77

88
void gaussianElimination(std::vector<std::vector<double> > &eqns) {
99
// 'eqns' is the matrix, 'rows' is no. of vars
10-
int rows = eqns.size(), cols = eqns[0].size();
10+
std::size_t rows = eqns.size(), cols = eqns[0].size();
1111

12-
for (int i = 0; i < rows - 1; i++) {
13-
int pivot = i;
12+
for (std::size_t i = 0; i < rows - 1; i++) {
13+
std::size_t pivot = i;
1414

15-
for (int j = i + 1; j < rows; j++) {
15+
for (std::size_t j = i + 1; j < rows; j++) {
1616
if (fabs(eqns[j][i]) > fabs(eqns[pivot][i])) pivot = j;
1717
}
1818

@@ -22,10 +22,10 @@ void gaussianElimination(std::vector<std::vector<double> > &eqns) {
2222
if (i != pivot) // Swapping the rows if new row with higher maxVals is found
2323
std::swap(eqns[pivot], eqns[i]); // C++ swap function
2424

25-
for (int j = i + 1; j < rows; j++) {
25+
for (std::size_t j = i + 1; j < rows; j++) {
2626
double scale = eqns[j][i] / eqns[i][i];
2727

28-
for (int k = i + 1; k < cols; k++) // k doesn't start at 0, since
28+
for (std::size_t k = i + 1; k < cols; k++) // k doesn't start at 0, since
2929
eqns[j][k] -= scale * eqns[i][k]; // values before from 0 to i
3030
// are already 0
3131
eqns[j][i] = 0.0;
@@ -35,17 +35,17 @@ void gaussianElimination(std::vector<std::vector<double> > &eqns) {
3535

3636
void gaussJordan(std::vector<std::vector<double> > &eqns) {
3737
// 'eqns' is the (Row-echelon) matrix, 'rows' is no. of vars
38-
int rows = eqns.size();
38+
std::size_t rows = eqns.size();
3939

40-
for (int i = rows - 1; i >= 0; i--) {
40+
for (std::size_t i = rows - 1; i < rows; i--) {
4141

4242
if (eqns[i][i] != 0) {
4343

4444
eqns[i][rows] /= eqns[i][i];
4545
eqns[i][i] = 1; // We know that the only entry in this row is 1
4646

4747
// subtracting rows from below
48-
for (int j = i - 1; j >= 0; j--) {
48+
for (std::size_t j = i - 1; j < i; j--) {
4949
eqns[j][rows] -= eqns[j][i] * eqns[i][rows];
5050
eqns[j][i] = 0; // We also set all the other values in row to 0 directly
5151
}
@@ -55,13 +55,13 @@ void gaussJordan(std::vector<std::vector<double> > &eqns) {
5555

5656
std::vector<double> backSubs(const std::vector<std::vector<double> > &eqns) {
5757
// 'eqns' is matrix, 'rows' is no. of variables
58-
int rows = eqns.size();
58+
std::size_t rows = eqns.size();
5959

6060
std::vector<double> ans(rows);
61-
for (int i = rows - 1; i >= 0; i--) {
61+
for (std::size_t i = rows - 1; i < rows; i--) {
6262
double sum = 0.0;
6363

64-
for (int j = i + 1; j < rows; j++) sum += eqns[i][j] * ans[j];
64+
for (std::size_t j = i + 1; j < rows; j++) sum += eqns[i][j] * ans[j];
6565

6666
if (eqns[i][i] != 0)
6767
ans[i] = (eqns[i][rows] - sum) / eqns[i][i];

contents/graham_scan/code/c/graham.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ void polar_angles_sort(struct point *points, struct point origin, size_t size) {
3636

3737
double pivot_angle = polar_angle(origin, points[size / 2]);
3838

39-
int i = 0;
40-
int j = size - 1;
39+
size_t i = 0;
40+
size_t j = size - 1;
4141
while (1) {
4242
while (polar_angle(origin, points[i]) < pivot_angle) {
4343
i++;

contents/huffman_encoding/code/c/huffman.c

+12-12
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ struct codebook {
2323

2424
struct heap {
2525
struct tree** data;
26-
int length;
27-
int capacity;
26+
size_t length;
27+
size_t capacity;
2828
};
2929

3030
bool is_leaf(const struct tree* t) {
@@ -39,21 +39,21 @@ void swap(struct tree** lhs, struct tree** rhs) {
3939

4040
/* The two concat functions are horribly inefficient */
4141
void concat(char** dst, const char* src) {
42-
int dst_len = strlen(*dst);
43-
int src_len = strlen(src);
42+
size_t dst_len = strlen(*dst);
43+
size_t src_len = strlen(src);
4444
*dst = realloc(*dst, src_len + dst_len + 1);
4545
strcat(*dst, src);
4646
}
4747

4848
void concat_char(char** dst, char c) {
49-
int len = strlen(*dst);
49+
size_t len = strlen(*dst);
5050
*dst = realloc(*dst, len + 2);
5151
(*dst)[len] = c;
5252
(*dst)[len + 1] = '\0';
5353
}
5454

5555
char* duplicate(const char* src) {
56-
int length = strlen(src);
56+
size_t length = strlen(src);
5757
char* dst = malloc(length + 1);
5858
memcpy(dst, src, length + 1);
5959
return dst;
@@ -66,9 +66,9 @@ void heap_push(struct heap* heap, struct tree* value) {
6666
}
6767
heap->data[heap->length++] = value;
6868

69-
int index = heap->length - 1;
69+
size_t index = heap->length - 1;
7070
while (index) {
71-
int parent_index = (index - 1) / 2;
71+
size_t parent_index = (index - 1) / 2;
7272
if (heap->data[parent_index]->count <= heap->data[index]->count) {
7373
break;
7474
}
@@ -86,11 +86,11 @@ struct tree* heap_pop(struct heap* heap) {
8686
struct tree* result = heap->data[0];
8787
swap(&heap->data[0], &heap->data[--heap->length]);
8888

89-
int index = 0;
89+
size_t index = 0;
9090
for (;;) {
91-
int target = index;
92-
int left = 2 * index + 1;
93-
int right = left + 1;
91+
size_t target = index;
92+
size_t left = 2 * index + 1;
93+
size_t right = left + 1;
9494

9595
if (left < heap->length &&
9696
heap->data[left]->count < heap->data[target]->count) {

contents/monte_carlo_integration/code/c/monte_carlo.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ double monte_carlo(unsigned int samples) {
2424
}
2525

2626
int main() {
27-
srand(time(NULL));
27+
srand((unsigned int)time(NULL));
2828

2929
double estimate = monte_carlo(1000000);
3030

contents/split-operator_method/code/c/split_op.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ void fft(double complex *x, size_t n, bool inverse) {
3434
fftw_plan p;
3535

3636
if (inverse) {
37-
p = fftw_plan_dft_1d(n, (fftw_complex*)x, (fftw_complex*)y,
37+
p = fftw_plan_dft_1d((int)n, (fftw_complex*)x, (fftw_complex*)y,
3838
FFTW_BACKWARD, FFTW_ESTIMATE);
3939
} else {
40-
p = fftw_plan_dft_1d(n, (fftw_complex*)x, (fftw_complex*)y,
40+
p = fftw_plan_dft_1d((int)n, (fftw_complex*)x, (fftw_complex*)y,
4141
FFTW_FORWARD, FFTW_ESTIMATE);
4242
}
4343

@@ -63,9 +63,9 @@ void init_params(struct params *par, double xmax, unsigned int res, double dt,
6363
par->im_time = im;
6464

6565
for (size_t i = 0; i < res; ++i) {
66-
par->x[i] = xmax / res - xmax + i * (2.0 * xmax / res);
66+
par->x[i] = xmax / res - xmax + (double)i * (2.0 * xmax / res);
6767
if (i < res / 2) {
68-
par->k[i] = i * M_PI / xmax;
68+
par->k[i] = (double)i * M_PI / xmax;
6969
} else {
7070
par->k[i] = ((double)i - res) * M_PI / xmax;
7171
}

contents/split-operator_method/code/cpp/split_op.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ struct Params {
2828
im_time = im;
2929

3030
for (size_t i = 0; i < res; ++i) {
31-
x.emplace_back(xmax / res - xmax + i * (2.0 * xmax / res));
31+
x.emplace_back(xmax / res - xmax + static_cast<double>(i) * (2.0 * xmax / res));
3232
if (i < res / 2) {
33-
k.push_back(i * M_PI / xmax);
33+
k.push_back(static_cast<double>(i) * M_PI / xmax);
3434
} else {
3535
k.push_back((static_cast<double>(i) - res) * M_PI / xmax);
3636
}
@@ -85,7 +85,7 @@ void fft(vector_complex &x, bool inverse) {
8585

8686
fftw_complex *in = reinterpret_cast<fftw_complex*>(x.data());
8787
fftw_complex *out = reinterpret_cast<fftw_complex*>(y.data());
88-
p = fftw_plan_dft_1d(x.size(), in, out,
88+
p = fftw_plan_dft_1d(static_cast<int>(x.size()), in, out,
8989
(inverse ? FFTW_BACKWARD : FFTW_FORWARD), FFTW_ESTIMATE);
9090

9191
fftw_execute(p);

contents/stable_marriage_problem/code/c/stable_marriage.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55
#include <time.h>
66

77
struct person {
8-
int id;
8+
size_t id;
99
struct person *partner;
1010
size_t *prefers;
1111
size_t index;
1212
};
1313

1414
void shuffle(size_t *array, size_t size) {
1515
for (size_t i = size - 1; i > 0; --i) {
16-
size_t j = rand() % (i + 1);
16+
size_t j = (size_t)rand() % (i + 1);
1717
size_t tmp = array[i];
1818
array[i] = array[j];
1919
array[j] = tmp;
@@ -82,7 +82,7 @@ void free_group(struct person *group, size_t size) {
8282
}
8383

8484
int main() {
85-
srand(time(NULL));
85+
srand((unsigned int)time(NULL));
8686

8787
struct person men[5], women[5];
8888

@@ -114,7 +114,7 @@ int main() {
114114
printf("\n");
115115

116116
for (size_t i = 0; i < 5; ++i) {
117-
printf("the partner of man %zu is woman %d\n", i, men[i].partner->id);
117+
printf("the partner of man %zu is woman %ld\n", i, men[i].partner->id);
118118
}
119119

120120
free_group(men, 5);

0 commit comments

Comments
 (0)