Add entropy tool

This commit is contained in:
Neale Pickett 2018-01-08 23:39:44 +00:00
parent 69ee969702
commit cd3d143d84
1 changed files with 26 additions and 0 deletions

26
entropy.c Normal file
View File

@ -0,0 +1,26 @@
#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);
}