squiggle.c

Self-contained Monte Carlo estimation in C99
Log | Files | Refs | README

commit f65699a688138c024647241a0236e3a272f5dfc0
parent 6e228dcc6bfdab03cf0259ac1f7b45431960ccaf
Author: NunoSempere <nuno.sempere@protonmail.com>
Date:   Sun, 23 Jul 2023 13:17:40 +0200

fix floats.h bug, fix std bug, add tests for std.

Diffstat:
Mexamples/01_one_sample/example | 0
Mexamples/02_many_samples/example | 0
Mexamples/03_gcc_nested_function/example | 0
Mexamples/04_sample_from_cdf_simple/example | 0
Mexamples/05_sample_from_cdf_beta/example | 0
Mexamples/06_gamma_beta/example | 0
Msquiggle.c | 5++---
Mtest/makefile | 3+++
Mtest/test | 0
Mtest/test.c | 31++++++++++++++++++++++---------
10 files changed, 27 insertions(+), 12 deletions(-)

diff --git a/examples/01_one_sample/example b/examples/01_one_sample/example Binary files differ. diff --git a/examples/02_many_samples/example b/examples/02_many_samples/example Binary files differ. diff --git a/examples/03_gcc_nested_function/example b/examples/03_gcc_nested_function/example Binary files differ. diff --git a/examples/04_sample_from_cdf_simple/example b/examples/04_sample_from_cdf_simple/example Binary files differ. diff --git a/examples/05_sample_from_cdf_beta/example b/examples/05_sample_from_cdf_beta/example Binary files differ. diff --git a/examples/06_gamma_beta/example b/examples/06_gamma_beta/example Binary files differ. diff --git a/squiggle.c b/squiggle.c @@ -1,4 +1,4 @@ -#include <double.h> +#include <float.h> #include <limits.h> #include <math.h> #include <stdint.h> @@ -161,8 +161,7 @@ double array_std(double* array, int length) double mean = array_mean(array, length); double std = 0.0; for (int i = 0; i < length; i++) { - std += (array[i] - mean); - std *= std; + std += (array[i] - mean) * (array[i] - mean); } std = sqrt(std / length); return std; diff --git a/test/makefile b/test/makefile @@ -36,6 +36,9 @@ format: $(SRC) run: $(SRC) $(OUTPUT) ./$(OUTPUT) +verify: $(SRC) $(OUTPUT) + ./$(OUTPUT) | grep "NOT passed" || true + time-linux: @echo "Requires /bin/time, found on GNU/Linux systems" && echo diff --git a/test/test b/test/test Binary files differ. diff --git a/test/test.c b/test/test.c @@ -4,7 +4,8 @@ #include <stdlib.h> #include <stdio.h> -#define N 100 +#define N 1000 * 1000 +#define PERCENTAGE_TOLERANCE 1.0/1000.0 void test_unit_uniform(uint64_t* seed){ double* unit_uniform_array = malloc(sizeof(double) * N); @@ -24,13 +25,13 @@ void test_unit_uniform(uint64_t* seed){ printf("Mean of unit uniform: %f, vs expected mean: %f, delta: %f\n", mean, expected_mean, delta_mean); printf("Std of unit uniform: %f, vs expected std: %f, delta: %f\n", std, expected_std, delta_std); - if(fabs(delta_mean) > 1.0/1000.0){ + if(fabs(delta_mean) > PERCENTAGE_TOLERANCE){ printf("[-] Mean test for unit uniform NOT passed.\n"); }else { printf("[x] Mean test for unit uniform PASSED\n"); } - if(fabs(delta_std) > 1.0/1000.0){ + if(fabs(delta_std) > PERCENTAGE_TOLERANCE){ printf("[-] Std test for unit uniform NOT passed.\n"); }else { printf("[x] Std test for unit uniform PASSED\n"); @@ -56,22 +57,22 @@ void test_uniform(double start, double end, uint64_t* seed){ double delta_std = std - expected_std; double width = fabs(end - start); - if(fabs(delta_mean) > width * 1.0/1000.0){ + if(fabs(delta_mean) > width * PERCENTAGE_TOLERANCE){ printf("[-] Mean test for [%.1f, %.1f] uniform NOT passed.\n", start, end); printf("Mean of [%.1f, %.1f] uniform: %f, vs expected mean: %f, delta: %f\n", start, end, mean, expected_mean, mean - expected_mean); }else { - printf("[x] Mean test for unit uniform PASSED\n"); + printf("[x] Mean test for [%.1f, %.1f] uniform PASSED\n", start, end); } - if(fabs(delta_std) > width * 1.0/1000.0){ + if(fabs(delta_std) > width * PERCENTAGE_TOLERANCE){ printf("[-] Std test for [%.1f, %.1f] uniform NOT passed.\n", start, end); printf("Std of [%.1f, %.1f] uniform: %f, vs expected std: %f, delta: %f\n", start, end, std, expected_std, std - expected_std); - for(int i=0; i<N; i++){ + for(int i=0; i<10; i++){ printf("%.1f, ", uniform_array[i]); } }else { - printf("[x] Std test for unit uniform PASSED\n"); + printf("[x] Std test for [%.1f, %.1f] uniform PASSED\n", start, end); } printf("\n"); @@ -82,15 +83,27 @@ int main(){ uint64_t* seed = malloc(sizeof(uint64_t)); *seed = 1000; // xorshift can't start with a seed of 0 + printf("Testing unit uniform\n"); test_unit_uniform(seed); - for(int i=0; i<1; i++){ + printf("Testing small uniforms\n"); + for(int i=0; i<100; i++){ double start = sample_uniform(-10, 10, seed); double end = sample_uniform(-10, 10, seed); if ( end > start){ test_uniform(start, end, seed); } } + + printf("Testing wide uniforms\n"); + for(int i=0; i<100; i++){ + double start = sample_uniform(-1000 * 1000, 1000 * 1000, seed); + double end = sample_uniform(-1000 * 1000, 1000 * 1000, seed); + if ( end > start){ + test_uniform(start, end, seed); + } + } + free(seed); }