squiggle.c

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

example.c (752B)


      1 #include "../../../squiggle.h"
      2 #include "../../../squiggle_more.h"
      3 #include <math.h>
      4 #include <stdio.h>
      5 #include <stdlib.h>
      6 
      7 #define ln lognormal_params
      8 #define to(...) convert_ci_to_lognormal_params((ci)__VA_ARGS__)
      9 #define from(...) convert_lognormal_params_to_ci((ln)__VA_ARGS__)
     10 #define times(a, b) algebra_product_lognormals(a, b)
     11 
     12 int main()
     13 {
     14     // set randomness seed
     15     uint64_t* seed = malloc(sizeof(uint64_t));
     16     *seed = 1000; // xorshift can't start with 0
     17 
     18     ln a = to({ .low = 1, .high = 10 });
     19     ln b = to({ .low = 5, .high = 500 });
     20     ln c = times(a, b);
     21 
     22     printf("Result: to(%f, %f)\n", from(c).low, from(c).high);
     23     printf("One sample from it is: %f\n", sample_lognormal(c.logmean, c.logstd, seed));
     24 
     25     free(seed);
     26 }