]> git.tuebingen.mpg.de Git - tfortune.git/commitdiff
com_stats(): Avoid division by zero.
authorAndre Noll <maan@tuebingen.mpg.de>
Mon, 17 Jun 2019 11:27:36 +0000 (13:27 +0200)
committerAndre Noll <maan@tuebingen.mpg.de>
Tue, 18 Jun 2019 11:37:55 +0000 (13:37 +0200)
If no epigrams are defined yet, the arguments to printf() contain
a division by zero which results in output like:

average number of epigrams per file     -nan
average number of tags per epigram.     -nan
average number of tag recurrence...     -nan

This is not a wrong per se, but it seems to be safer to special case
this and print zero.

tfortune.c

index 751334dd2b91d2f49f077a3aa0b1383db3a32bae..ec4c323f7c29040b0bbb7d1e883f303c6559951c 100644 (file)
@@ -895,11 +895,11 @@ static int com_stats(void)
        printf("number of tags..................... %5lu\n", num_tags);
        printf("number of unique tags.............. %5u\n", num_unique_tags);
        printf("average number of epigrams per file %8.02f\n",
        printf("number of tags..................... %5lu\n", num_tags);
        printf("number of unique tags.............. %5u\n", num_unique_tags);
        printf("average number of epigrams per file %8.02f\n",
-               (float)num_epis / num_epi_files);
-       printf("average number of tags per epigram. %8.02f\n",
-               (float)num_tags / num_epis);
-       printf("average number of tag recurrence... %8.02f\n",
-               (float)num_tags / num_unique_tags);
+               num_epi_files > 0?  (float)num_epis / num_epi_files : 0);
+       printf("average number of tags per epigram. %8.02f\n", num_epis > 0?
+               (float)num_tags / num_epis : 0);
+       printf("average number of tag recurrence... %8.02f\n", num_unique_tags > 0?
+               (float)num_tags / num_unique_tags : 0);
        if (verbose) {
                printf("\nlinear hashing statistics:\n%s\n", lh_stats);
                free(lh_stats);
        if (verbose) {
                printf("\nlinear hashing statistics:\n%s\n", lh_stats);
                free(lh_stats);