fluffy/entropy.c

27 lines
345 B
C
Raw Normal View History

2018-01-08 16:39:44 -07:00
#include <stdio.h>
#include <math.h>
int
main(int argc, char *argv[])
{
size_t len = 0;
size_t hist[256] = {0};
int c;
while ((c = getchar()) != EOF) {
hist[c] += 1;
len += 1;
}
float H = 0;
for (int i = 0; i < 256; i += 1) {
if (hist[i]) {
float Hi = (float)hist[i]/len;
H -= Hi * log2(Hi);
}
}
printf("%f\n", H);
}