fix various errors from compiler warnings

This commit is contained in:
NunoSempere 2023-12-09 18:49:20 +00:00
parent e1af09b49a
commit 9cda19cbb5
35 changed files with 38 additions and 29 deletions

Binary file not shown.

View File

@ -22,7 +22,7 @@ int main()
double (*samplers[])(uint64_t*) = { sample_0, sample_1, sample_few, sample_many };
int n_samples = 1000000;
double* result_many = (double*)malloc(n_samples * sizeof(double));
double* result_many = (double*)malloc((size_t) n_samples * sizeof(double));
for (int i = 0; i < n_samples; i++) {
result_many[i] = sample_mixture(samplers, weights, n_dists, seed);
}

View File

@ -24,7 +24,7 @@ int main()
double weights[] = { 1 - p_c, p_c / 2, p_c / 4, p_c / 4 };
int n_samples = 1000000;
double* result_many = (double*)malloc(n_samples * sizeof(double));
double* result_many = (double*)malloc((size_t) n_samples * sizeof(double));
for (int i = 0; i < n_samples; i++) {
result_many[i] = sample_mixture(samplers, weights, n_dists, seed);
}

Binary file not shown.

View File

@ -19,7 +19,7 @@ int main()
printf("\n");
*/
double* gamma_1_array = malloc(sizeof(double) * n);
double* gamma_1_array = malloc(sizeof(double) * (size_t) n);
for (int i = 0; i < n; i++) {
double gamma_1 = sample_gamma(1.0, seed);
// printf("sample_gamma(1.0): %f\n", gamma_1);
@ -29,7 +29,7 @@ int main()
free(gamma_1_array);
printf("\n");
double* beta_1_2_array = malloc(sizeof(double) * n);
double* beta_1_2_array = malloc(sizeof(double) * (size_t) n);
for (int i = 0; i < n; i++) {
double beta_1_2 = sample_beta(1, 2.0, seed);
// printf("sample_beta(1.0, 2.0): %f\n", beta_1_2);

View File

@ -22,8 +22,10 @@ MATH=-lm
DEPS=$(SQUIGGLE) $(MATH)
## Flags
DEBUG= #'-g'
WARN=-Wall -Wextra -Wdouble-promotion
# DEBUG=-fsanitize=address,undefined
# DEBUG=-g
DEBUG=
WARN=-Wall -Wextra -Wdouble-promotion -Wconversion
STANDARD=-std=c99
OPTIMIZED=-O3 #-Ofast

Binary file not shown.

Binary file not shown.

View File

@ -49,7 +49,7 @@ int main()
int n = 1000 * 1000;
double* mixture_result = malloc(sizeof(double) * n);
double* mixture_result = malloc(sizeof(double) * (size_t) n);
for (int i = 0; i < n; i++) {
mixture_result[i] = mixture(seed);
}
@ -63,6 +63,7 @@ int main()
ci ci_90 = sampler_get_90_ci(mixture, 1000000, seed);
printf("mean: %f\n", array_mean(mixture_result, n));
printf("90%% confidence interval: [%f, %f]\n", ci_90.low, ci_90.high);
free(mixture_result);
free(seed);
}

View File

@ -28,7 +28,7 @@ int main()
int n = 1000 * 1000;
double* result = malloc(sizeof(double) * n);
double* result = malloc(sizeof(double) * (size_t) n);
for (int i = 0; i < n; i++) {
result[i] = sample_minutes_per_day_jumping_rope_needed_to_burn_10kg(seed);
}

View File

@ -53,7 +53,7 @@ int main()
ci ci_90_2023 = sampler_get_90_ci(yearly_probability_nuclear_collapse_2023, 1000000, seed);
printf("90%% confidence interval: [%f, %f]\n", ci_90_2023.low, ci_90_2023.high);
double* yearly_probability_nuclear_collapse_2023_samples = malloc(sizeof(double) * num_samples);
double* yearly_probability_nuclear_collapse_2023_samples = malloc(sizeof(double) * (size_t) num_samples);
for (int i = 0; i < num_samples; i++) {
yearly_probability_nuclear_collapse_2023_samples[i] = yearly_probability_nuclear_collapse_2023(seed);
}
@ -64,7 +64,7 @@ int main()
ci ci_90_2070 = sampler_get_90_ci(yearly_probability_nuclear_collapse_after_recovery_example, 1000000, seed);
printf("90%% confidence interval: [%f, %f]\n", ci_90_2070.low, ci_90_2070.high);
double* yearly_probability_nuclear_collapse_after_recovery_samples = malloc(sizeof(double) * num_samples);
double* yearly_probability_nuclear_collapse_after_recovery_samples = malloc(sizeof(double) * (size_t) num_samples);
for (int i = 0; i < num_samples; i++) {
yearly_probability_nuclear_collapse_after_recovery_samples[i] = yearly_probability_nuclear_collapse_after_recovery_example(seed);
}
@ -75,11 +75,14 @@ int main()
ci ci_90_antiinductive = sampler_get_90_ci(yearly_probability_nuclear_collapse_after_recovery_antiinductive, 1000000, seed);
printf("90%% confidence interval: [%f, %f]\n", ci_90_antiinductive.low, ci_90_antiinductive.high);
double* yearly_probability_nuclear_collapse_after_recovery_antiinductive_samples = malloc(sizeof(double) * num_samples);
double* yearly_probability_nuclear_collapse_after_recovery_antiinductive_samples = malloc(sizeof(double) * (size_t) num_samples);
for (int i = 0; i < num_samples; i++) {
yearly_probability_nuclear_collapse_after_recovery_antiinductive_samples[i] = yearly_probability_nuclear_collapse_after_recovery_antiinductive(seed);
}
printf("mean: %f\n", array_mean(yearly_probability_nuclear_collapse_after_recovery_antiinductive_samples, num_samples));
free(yearly_probability_nuclear_collapse_2023_samples);
free(yearly_probability_nuclear_collapse_after_recovery_samples);
free(yearly_probability_nuclear_collapse_after_recovery_antiinductive_samples);
free(seed);
}

Binary file not shown.

View File

@ -17,7 +17,7 @@ int main()
{
return sample_lognormal(0, 10, seed);
}
double* results = malloc(n_samples * sizeof(double));
double* results = malloc((size_t) n_samples * sizeof(double));
sampler_parallel(sampler, results, n_threads, n_samples);
double avg = array_sum(results, n_samples) / n_samples;

View File

@ -23,7 +23,7 @@ int main()
}
int n_samples = 1000 * 1000, n_threads = 16;
double* results = malloc(n_samples * sizeof(double));
double* results = malloc((size_t) n_samples * sizeof(double));
sampler_parallel(sampler_result, results, n_threads, n_samples);
printf("Avg: %f\n", array_sum(results, n_samples) / n_samples);
free(results);

View File

@ -30,7 +30,7 @@ int main()
}
int n_samples = 1000000, n_threads = 16;
double* results = malloc(n_samples * sizeof(double));
double* results = malloc((size_t) n_samples * sizeof(double));
sampler_parallel(sample_min_of_1000, results, n_threads, n_samples);
printf("Mean of the distribution of (taking the min of 1000 samples of a normal(5,2)): %f\n", array_mean(results, n_samples));
free(results);
@ -51,7 +51,7 @@ int main()
{
return sample_min_of_n(seed, quotient);
}
double* results_quotient = malloc(quotient * sizeof(double));
double* results_quotient = malloc((size_t) quotient * sizeof(double));
sampler_parallel(sample_min_of_quotient, results_quotient, n_threads, quotient);
double min = results_quotient[0];

View File

@ -10,12 +10,13 @@ int main()
*seed = 1000; // xorshift can't start with a seed of 0
int n = 1000000;
double* xs = malloc(sizeof(double) * n);
double* xs = malloc(sizeof(double) * (size_t) n);
for (int i = 0; i < n; i++) {
xs[i] = sample_to(10, 100, seed);
}
ci ci_90 = array_get_90_ci(xs, n);
printf("Recovering confidence interval of sample_to(10, 100):\n low: %f, high: %f\n", ci_90.low, ci_90.high);
free(xs);
free(seed);
}

View File

@ -24,8 +24,10 @@ OPENMP=-fopenmp
DEPS=$(SQUIGGLE) $(SQUIGGLE_MORE) $(MATH) $(OPENMP)
## Flags
DEBUG= #'-g'
WARN=-Wall -Wextra -Wdouble-promotion
# DEBUG=-fsanitize=address,undefined
# DEBUG=-g
DEBUG=
WARN=-Wall -Wextra -Wdouble-promotion -Wconversion
STANDARD=-std=c99
WARNINGS=-Wall
OPTIMIZED=-O3 #-Ofast

View File

@ -102,8 +102,8 @@ double sample_to(double low, double high, uint64_t* seed)
// Key idea: If we want a lognormal with 90% confidence interval [a, b]
// we need but get a normal with 90% confidence interval [log(a), log(b)].
// Then see code for sample_normal_from_90_confidence_interval
double loglow = logf(low);
double loghigh = logf(high);
double loglow = log(low);
double loghigh = log(high);
return exp(sample_normal_from_90_confidence_interval(loglow, loghigh, seed));
}
@ -204,7 +204,7 @@ double sample_mixture(double (*samplers[])(uint64_t*), double* weights, int n_di
{
// Sample from samples with frequency proportional to their weights.
double sum_weights = array_sum(weights, n_dists);
double* cumsummed_normalized_weights = (double*)malloc(n_dists * sizeof(double));
double* cumsummed_normalized_weights = (double*)malloc((size_t) n_dists * sizeof(double));
cumsummed_normalized_weights[0] = weights[0] / sum_weights;
for (int i = 1; i < n_dists; i++) {
cumsummed_normalized_weights[i] = cumsummed_normalized_weights[i - 1] + weights[i] / sum_weights;

View File

@ -28,7 +28,7 @@ void sampler_parallel(double (*sampler)(uint64_t* seed), double* results, int n_
int quotient = n_samples / n_threads;
int divisor_multiple = quotient * n_threads;
uint64_t** seeds = malloc(n_threads * sizeof(uint64_t*));
uint64_t** seeds = malloc((size_t) n_threads * sizeof(uint64_t*));
srand(1);
for (int i = 0; i < n_threads; i++) {
seeds[i] = malloc(sizeof(uint64_t));
@ -88,7 +88,7 @@ static int partition(int low, int high, double xs[], int length)
exit(1);
}
// Note: the scratchpad/ folder in commit 578bfa27 has printfs sprinkled throughout
int pivot = low + floor((high - low) / 2);
int pivot = low + (int) floor((high - low) / 2);
double pivot_value = xs[pivot];
swp(pivot, high, xs);
int gt = low; /* This pointer will iterate until finding an element which is greater than the pivot. Then it will move elements that are smaller before it--more specifically, it will move elements to its position and then increment. As a result all elements between gt and i will be greater than the pivot. */
@ -125,8 +125,8 @@ static double quickselect(int k, double xs[], int n)
ci array_get_ci(ci interval, double* xs, int n)
{
int low_k = floor(interval.low * n);
int high_k = ceil(interval.high * n);
int low_k = (int) floor(interval.low * n);
int high_k = (int) ceil(interval.high * n);
ci result = {
.low = quickselect(low_k, xs, n),
.high = quickselect(high_k, xs, n),
@ -141,7 +141,7 @@ ci array_get_90_ci(double xs[], int n)
ci sampler_get_ci(ci interval, double (*sampler)(uint64_t*), int n, uint64_t* seed)
{
UNUSED(seed); // don't want to use it right now, but want to preserve ability to do so (e.g., remove parallelism from internals). Also nicer for consistency.
double* xs = malloc(n * sizeof(double));
double* xs = malloc((size_t) n * sizeof(double));
sampler_parallel(sampler, xs, 16, n);
ci result = array_get_ci(interval, xs, n);
free(xs);
@ -186,8 +186,8 @@ lognormal_params algebra_product_lognormals(lognormal_params a, lognormal_params
lognormal_params convert_ci_to_lognormal_params(ci x)
{
double loghigh = logf(x.high);
double loglow = logf(x.low);
double loghigh = log(x.high);
double loglow = log(x.low);
double logmean = (loghigh + loglow) / 2.0;
double logstd = (loghigh - loglow) / (2.0 * NORMAL90CONFIDENCE);
lognormal_params result = { .logmean = logmean, .logstd = logstd };