summaryrefslogtreecommitdiffstats
path: root/src/stats.h
blob: f9e5d7ac1f0ea1326f6459a13b3f49d5255c9561 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/* SPDX-License-Identifier: BSD-3-Clause */
/*
 * Copyright © 2010, Zed A. Shaw.
 * Copyright © 2020 rsiddharth <s@ricketyspace.net>
 */

#ifndef stats_h
#define stats_h

#include <math.h>
#include <stdlib.h>
#include <bstrlib.h>
#include <dbg.h>

typedef struct Stats {
    double sum;
    double sumsq;
    unsigned long n;
    double min;
    double max;
} Stats;

Stats *Stats_recreate(double sum, double sumsq, unsigned long n,
                      double min, double max);

Stats *Stats_create();

void Stats_sample(Stats *st, double s);

char *Stats_dump(Stats *st);

char *Stats_stringify(Stats *st);

Stats *Stats_unstringify(char *st_str);

static inline double Stats_mean(Stats *st)
{
    return st->sum / st->n;
}

static inline double Stats_stddev(Stats *st)
{
    return sqrt((st->sumsq - (st->sum * st->sum / st->n)) /
                (st->n - 1));
}

#endif