diff --git a/examples/core/00_example_template/example b/examples/core/00_example_template/example index 1475883..a285123 100755 Binary files a/examples/core/00_example_template/example and b/examples/core/00_example_template/example differ diff --git a/examples/core/01_one_sample/example b/examples/core/01_one_sample/example index 61c0990..87202b0 100755 Binary files a/examples/core/01_one_sample/example and b/examples/core/01_one_sample/example differ diff --git a/examples/core/02_time_to_botec/example b/examples/core/02_time_to_botec/example index 1361cf5..bb2b7f3 100755 Binary files a/examples/core/02_time_to_botec/example and b/examples/core/02_time_to_botec/example differ diff --git a/examples/core/02_time_to_botec/example.c b/examples/core/02_time_to_botec/example.c index 199135f..21aa12c 100644 --- a/examples/core/02_time_to_botec/example.c +++ b/examples/core/02_time_to_botec/example.c @@ -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); } diff --git a/examples/core/03_gcc_nested_function/example b/examples/core/03_gcc_nested_function/example index 0a43bb0..62c051d 100755 Binary files a/examples/core/03_gcc_nested_function/example and b/examples/core/03_gcc_nested_function/example differ diff --git a/examples/core/03_gcc_nested_function/example.c b/examples/core/03_gcc_nested_function/example.c index 8c8bb30..b376bea 100644 --- a/examples/core/03_gcc_nested_function/example.c +++ b/examples/core/03_gcc_nested_function/example.c @@ -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); } diff --git a/examples/core/04_gamma_beta/example b/examples/core/04_gamma_beta/example index e9d2bb0..1d69d8b 100755 Binary files a/examples/core/04_gamma_beta/example and b/examples/core/04_gamma_beta/example differ diff --git a/examples/core/04_gamma_beta/example.c b/examples/core/04_gamma_beta/example.c index 024ab3e..2612662 100644 --- a/examples/core/04_gamma_beta/example.c +++ b/examples/core/04_gamma_beta/example.c @@ -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); diff --git a/examples/core/05_hundred_lognormals/example b/examples/core/05_hundred_lognormals/example index fc3ed8f..56ff496 100755 Binary files a/examples/core/05_hundred_lognormals/example and b/examples/core/05_hundred_lognormals/example differ diff --git a/examples/core/makefile b/examples/core/makefile index 3b2da54..6fbd39a 100644 --- a/examples/core/makefile +++ b/examples/core/makefile @@ -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 diff --git a/examples/more/00_example_template/example b/examples/more/00_example_template/example index 9fc934d..77fbe44 100755 Binary files a/examples/more/00_example_template/example and b/examples/more/00_example_template/example differ diff --git a/examples/more/01_sample_from_cdf/example b/examples/more/01_sample_from_cdf/example index 1ca3031..772bc6d 100755 Binary files a/examples/more/01_sample_from_cdf/example and b/examples/more/01_sample_from_cdf/example differ diff --git a/examples/more/02_sample_from_cdf_beta/example b/examples/more/02_sample_from_cdf_beta/example index 0a10f4a..7cd2a01 100755 Binary files a/examples/more/02_sample_from_cdf_beta/example and b/examples/more/02_sample_from_cdf_beta/example differ diff --git a/examples/more/03_ci_beta/example b/examples/more/03_ci_beta/example index 3c5259a..67cdb1e 100755 Binary files a/examples/more/03_ci_beta/example and b/examples/more/03_ci_beta/example differ diff --git a/examples/more/04_nuclear_war/example b/examples/more/04_nuclear_war/example index cf3f9bb..d1d4f13 100755 Binary files a/examples/more/04_nuclear_war/example and b/examples/more/04_nuclear_war/example differ diff --git a/examples/more/04_nuclear_war/example.c b/examples/more/04_nuclear_war/example.c index 66ec5a7..be62bde 100644 --- a/examples/more/04_nuclear_war/example.c +++ b/examples/more/04_nuclear_war/example.c @@ -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); } diff --git a/examples/more/05_burn_10kg_fat/example b/examples/more/05_burn_10kg_fat/example index 6fbb807..7b15990 100755 Binary files a/examples/more/05_burn_10kg_fat/example and b/examples/more/05_burn_10kg_fat/example differ diff --git a/examples/more/05_burn_10kg_fat/example.c b/examples/more/05_burn_10kg_fat/example.c index 6a71ea2..2fb7298 100644 --- a/examples/more/05_burn_10kg_fat/example.c +++ b/examples/more/05_burn_10kg_fat/example.c @@ -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); } diff --git a/examples/more/06_nuclear_recovery/example b/examples/more/06_nuclear_recovery/example index 85eae17..c639611 100755 Binary files a/examples/more/06_nuclear_recovery/example and b/examples/more/06_nuclear_recovery/example differ diff --git a/examples/more/06_nuclear_recovery/example.c b/examples/more/06_nuclear_recovery/example.c index 572120e..0ff68b7 100644 --- a/examples/more/06_nuclear_recovery/example.c +++ b/examples/more/06_nuclear_recovery/example.c @@ -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); } diff --git a/examples/more/07_algebra/example b/examples/more/07_algebra/example index 9c77e7e..4cb7b84 100755 Binary files a/examples/more/07_algebra/example and b/examples/more/07_algebra/example differ diff --git a/examples/more/08_algebra_and_conversion/example b/examples/more/08_algebra_and_conversion/example index f77d3f3..dd43e51 100755 Binary files a/examples/more/08_algebra_and_conversion/example and b/examples/more/08_algebra_and_conversion/example differ diff --git a/examples/more/09_ergonomic_algebra/example b/examples/more/09_ergonomic_algebra/example index 96572c8..1c01b25 100755 Binary files a/examples/more/09_ergonomic_algebra/example and b/examples/more/09_ergonomic_algebra/example differ diff --git a/examples/more/10_twitter_thread_example/example b/examples/more/10_twitter_thread_example/example index eefd98b..37640a3 100755 Binary files a/examples/more/10_twitter_thread_example/example and b/examples/more/10_twitter_thread_example/example differ diff --git a/examples/more/11_billion_lognormals_paralell/example b/examples/more/11_billion_lognormals_paralell/example index ea80802..19bbf96 100755 Binary files a/examples/more/11_billion_lognormals_paralell/example and b/examples/more/11_billion_lognormals_paralell/example differ diff --git a/examples/more/11_billion_lognormals_paralell/example.c b/examples/more/11_billion_lognormals_paralell/example.c index 1988157..cd74903 100644 --- a/examples/more/11_billion_lognormals_paralell/example.c +++ b/examples/more/11_billion_lognormals_paralell/example.c @@ -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; diff --git a/examples/more/12_time_to_botec_parallel/example b/examples/more/12_time_to_botec_parallel/example index e283e2d..e540b3e 100755 Binary files a/examples/more/12_time_to_botec_parallel/example and b/examples/more/12_time_to_botec_parallel/example differ diff --git a/examples/more/12_time_to_botec_parallel/example.c b/examples/more/12_time_to_botec_parallel/example.c index 82c344f..682d69c 100644 --- a/examples/more/12_time_to_botec_parallel/example.c +++ b/examples/more/12_time_to_botec_parallel/example.c @@ -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); diff --git a/examples/more/13_parallelize_min/example b/examples/more/13_parallelize_min/example index 7f1fcea..d86e0fb 100755 Binary files a/examples/more/13_parallelize_min/example and b/examples/more/13_parallelize_min/example differ diff --git a/examples/more/13_parallelize_min/example.c b/examples/more/13_parallelize_min/example.c index ec8f3e3..b12eb70 100644 --- a/examples/more/13_parallelize_min/example.c +++ b/examples/more/13_parallelize_min/example.c @@ -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]; diff --git a/examples/more/14_check_confidence_interval/example b/examples/more/14_check_confidence_interval/example index 6774af4..ea0c180 100755 Binary files a/examples/more/14_check_confidence_interval/example and b/examples/more/14_check_confidence_interval/example differ diff --git a/examples/more/14_check_confidence_interval/example.c b/examples/more/14_check_confidence_interval/example.c index 24953fa..e99a0d3 100644 --- a/examples/more/14_check_confidence_interval/example.c +++ b/examples/more/14_check_confidence_interval/example.c @@ -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); } diff --git a/examples/more/makefile b/examples/more/makefile index 5ae4927..14d7ac2 100644 --- a/examples/more/makefile +++ b/examples/more/makefile @@ -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 diff --git a/squiggle.c b/squiggle.c index 3207db2..f1c647e 100644 --- a/squiggle.c +++ b/squiggle.c @@ -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; diff --git a/squiggle_more.c b/squiggle_more.c index 4d488af..1806bb0 100644 --- a/squiggle_more.c +++ b/squiggle_more.c @@ -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 };