Debian Codegraph @

src/bins.c

raw
1 /* Copyright 2009-2012 Thomas Schoebel-Theuer / 1&1 Internet AG
2 *
3 * Email: [email protected]
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
19
20 #define _GNU_SOURCE
21 #include <config.h>
22
23 #include <stdio.h>
24
25 #include <math.h>
26
27 #if !HAVE_DECL_EXP10
28 # define exp10(x) (exp((x) * log(10)))
29 #endif
30
31 #define MAX_BINS 1024
32
33 double bin_size = 10.0;
34 int bin_min = MAX_BINS;
35 int bin_max = 0;
36 int bin_count[MAX_BINS];
37
38 void put_bin(double val)
39 {
40 int bin;
41
42 if (val <= 0)
43 return;
44
45 bin = log10(val) * bin_size;
46 bin += MAX_BINS/2;
47 if (bin < 0 || bin >= MAX_BINS)
48 return;
49
50 bin_count[bin]++;
51
52 if (bin < bin_min)
53 bin_min = bin;
54 if (bin >= bin_max)
55 bin_max = bin + 1;
56 }
57
58 int main()
59 {
60 int i;
61 char buf[4096];
62
63 while (fgets(buf, sizeof(buf), stdin)) {
64 double val = 0;
65 sscanf(buf, " %lf", &val);
66 put_bin(val);
67 }
68
69 for (i = bin_min; i < bin_max; i++) {
70 printf("%le %6d\n", exp10((double)(i - MAX_BINS/2) / bin_size), bin_count[i]);
71 }
72
73 return 0;
74 }