librfn
An ad-hoc utility library
stats.c
Go to the documentation of this file.
1 /*
2  * stats.c
3  *
4  * Part of librfn (a general utility library from redfelineninja.org.uk)
5  *
6  * Copyright (C) 2015 Daniel Thompson <daniel@redfelineninja.org.uk>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License as published
10  * by the Free Software Foundation; either version 3 of the License, or
11  * (at your option) any later version.
12  */
13 
14 #include "librfn/stats.h"
15 
17 {
18  memset(s, 0, sizeof(*s));
19 #ifdef STATS_USE_FLOAT
20  s->min = FLT_MIN;
21  s->max = FLT_MAX;
22 #elif defined STATS_USE_DOUBLE
23  s->min = DBL_MIN;
24  s->max = DBL_MAX;
25 #else
26  s->min = (statval_t) -1ull;
27 #endif
28 
29 }
31 {
32  if (d < s->min)
33  s->min = d;
34 
35  if (d > s->max)
36  s->max = d;
37 
38  s->accumulator += d;
39  s->count++;
40 }
41 
43 {
44 #if defined STATS_USE_FLOAT || defined STATS_USE_DOUBLE
45  return s->accumulator / s->count;
46 #else
47  return (s->accumulator + s->count / 2) / s->count;
48 #endif
49 }
50 
52 {
53  /* having multiplied by a million we're prepared to overlook
54  * mathematical rounding when statval_t is an integer type...
55  */
56  return 1000000ull * s->accumulator / total;
57 }
58 
void stats_add(stats_t *s, statval_t d)
Definition: stats.c:30
void stats_init(stats_t *s)
Definition: stats.c:16
statval_t min
Definition: stats.h:42
statval_t max
Definition: stats.h:43
statval_t accumulator
Definition: stats.h:44
statval_t count
Definition: stats.h:45
Definition: stats.h:41
statval_t stats_per_million(stats_t *s, statval_t total)
Definition: stats.c:51
uint32_t statval_t
Definition: stats.h:38
statval_t stats_mean(stats_t *s)
Definition: stats.c:42