libzahl

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

03-avg.c (532B)


      1 /* Calculates the truncated average of $@ */
      2 
      3 #include <stdio.h>
      4 #include <stdlib.h>
      5 
      6 #include <zahl.h>
      7 
      8 int
      9 main(int argc, char *argv[])
     10 {
     11 	z_t sum, term;
     12 	jmp_buf env;
     13 	char *buf;
     14 	int i;
     15 
     16 	if (setjmp(env))
     17 		return zperror(argv[0]), 1;
     18 
     19 	zsetup(env);
     20 	zinit(sum);
     21 	zinit(term);
     22 	zsetu(sum, 0);
     23 
     24 	for (i = 1; i < argc; i++) {
     25 		zsets(term, argv[i]);
     26 		zadd(sum, sum, term);
     27 	}
     28 	zseti(term, argc);
     29 	zdiv(sum, sum, term);
     30 
     31 	printf("%s\n", buf = zstr(sum, NULL, 0));
     32 	free(buf);
     33 
     34 	zfree(term);
     35 	zfree(sum);
     36 	zunsetup();
     37 	return 0;
     38 }