libzahl

big integer library
git clone git://git.suckless.org/libzahl
Log | Files | Refs | README | LICENSE

commit a049db580776ed41bb4583bfb331d97c5a80900a
parent 7132e2b0f31ca0520465baf3caa75650c5b1bf2f
Author: Mattias Andrée <maandree@kth.se>
Date:   Sun, 19 Jun 2016 02:50:31 +0200

Add examples: sum, prod, avg, median

Signed-off-by: Mattias Andrée <maandree@kth.se>

Diffstat:
Aexamples/01-sum.c | 36++++++++++++++++++++++++++++++++++++
Aexamples/02-prod.c | 36++++++++++++++++++++++++++++++++++++
Aexamples/03-avg.c | 38++++++++++++++++++++++++++++++++++++++
Aexamples/04-median.c | 63+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 173 insertions(+), 0 deletions(-)

diff --git a/examples/01-sum.c b/examples/01-sum.c @@ -0,0 +1,36 @@ +/* Calculates the sum of $@ */ + +#include <stdio.h> +#include <stdlib.h> + +#include <zahl.h> + +int +main(int argc, char *argv[]) +{ + z_t sum, term; + jmp_buf env; + char *buf; + int i; + + if (setjmp(env)) + return zperror(argv[0]), 1; + + zsetup(env); + zinit(sum); + zinit(term); + zsetu(sum, 0); + + for (i = 1; i < argc; i++) { + zsets(term, argv[i]); + zadd(sum, sum, term); + } + + printf("%s\n", buf = zstr(sum, NULL, 0)); + free(buf); + + zfree(term); + zfree(sum); + zunsetup(); + return 0; +} diff --git a/examples/02-prod.c b/examples/02-prod.c @@ -0,0 +1,36 @@ +/* Calculates the product of $@ */ + +#include <stdio.h> +#include <stdlib.h> + +#include <zahl.h> + +int +main(int argc, char *argv[]) +{ + z_t prod, factor; + jmp_buf env; + char *buf; + int i; + + if (setjmp(env)) + return zperror(argv[0]), 1; + + zsetup(env); + zinit(prod); + zinit(factor); + zsetu(prod, 1); + + for (i = 1; i < argc; i++) { + zsets(factor, argv[i]); + zmul(prod, prod, factor); + } + + printf("%s\n", buf = zstr(prod, NULL, 0)); + free(buf); + + zfree(factor); + zfree(prod); + zunsetup(); + return 0; +} diff --git a/examples/03-avg.c b/examples/03-avg.c @@ -0,0 +1,38 @@ +/* Calculates the truncated average of $@ */ + +#include <stdio.h> +#include <stdlib.h> + +#include <zahl.h> + +int +main(int argc, char *argv[]) +{ + z_t sum, term; + jmp_buf env; + char *buf; + int i; + + if (setjmp(env)) + return zperror(argv[0]), 1; + + zsetup(env); + zinit(sum); + zinit(term); + zsetu(sum, 0); + + for (i = 1; i < argc; i++) { + zsets(term, argv[i]); + zadd(sum, sum, term); + } + zseti(term, argc); + zdiv(sum, sum, term); + + printf("%s\n", buf = zstr(sum, NULL, 0)); + free(buf); + + zfree(term); + zfree(sum); + zunsetup(); + return 0; +} diff --git a/examples/04-median.c b/examples/04-median.c @@ -0,0 +1,63 @@ +/* Calculates the median of $@ */ + +#include <stdio.h> +#include <stdlib.h> + +#include <zahl.h> + +int +main(int argc, char *argv[]) +{ + struct zahl *values; + z_t med, medmod; + jmp_buf env; + char *buf, *argv0; + int i, j; + + argv0 = *argv++, argc--; + + if (!argc) { + fprintf(stderr, + "%s: cannot calculate median of the empty bag\n", + argv0); + return 1; + } + + values = calloc(argc, sizeof(*values)); + if (!values) + return perror(argv0), 1; + + if (setjmp(env)) + return zperror(argv0), 1; + + zsetup(env); + zinit(med); + zinit(medmod); + + /* Since `values` where allocated with + * `calloc` it is already cleared and + * `zinit` is not necessary. */ + + for (i = 0; i < argc; i++) + zsets(&values[i], argv[i]); + + qsort(values, argc, sizeof(*values), + (int (*)(const void *, const void *))zcmp); + i = argc / 2; + j = i - !(argc & 1); + zadd(med, &values[i], &values[j]); + zsetu(medmod, 2); + zdivmod(med, medmod, med, medmod); + + printf("%s%s\n", buf = zstr(med, NULL, 0), + (const char *[]){"", ".5"}[zodd(medmod)]); + free(buf); + + zfree(medmod); + zfree(med); + for (i = 0; i < argc; i++) + zfree(&values[i]); + free(values); + zunsetup(); + return 0; +}