trandcounts(1): add option to display output as ratios - numtools - perform numerical operations on vectors and matrices in unix pipes
 (HTM) git clone git://src.adamsgaard.dk/numtools
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit 817421dee16bed3f985a841c575be086cfb3254a
 (DIR) parent d7ec07cf8c41de0a001237eded7af64e8868b345
 (HTM) Author: Anders Damsgaard <anders@adamsgaard.dk>
       Date:   Wed, 11 May 2022 10:42:44 +0200
       
       randcounts(1): add option to display output as ratios
       
       Diffstat:
         M randcounts.1                        |       3 +++
         M randcounts.c                        |      12 +++++++++---
       
       2 files changed, 12 insertions(+), 3 deletions(-)
       ---
 (DIR) diff --git a/randcounts.1 b/randcounts.1
       t@@ -9,6 +9,7 @@
        .Op Fl h
        .Op Fl n Ar num
        .Op Fl r Ar repeats
       +.Op Fl R
        .Op Fl s Ar seed
        .Ar weight1
        .Op Ar weight2 ...
       t@@ -37,6 +38,8 @@ Number of points to place in the bins.
        The default is 1.
        .It Fl r Ar repeats
        Repeat the binning several times, with one realization per line of output.
       +.It Fl R
       +Show the output as ratios (in the range [0;1]) instead of counts.
        .It Fl s Ar seed
        Seed the pseudo-random number generator with this value, which is used
        to generate reproducable binning.
 (DIR) diff --git a/randcounts.c b/randcounts.c
       t@@ -14,7 +14,7 @@ char *argv0;
        static void
        usage(void)
        {
       -        errx(1, "usage: %s [-h] [-n num] [-r repeats] [-s seed] "
       +        errx(1, "usage: %s [-h] [-n num] [-r repeats] [-R] [-s seed] "
                        "weight1 [weight2 ...]\n", argv0);
        }
        
       t@@ -22,7 +22,7 @@ int
        main(int argc, char *argv[])
        {
                int i, s = 0, N;
       -        long j, seed, *counts = NULL, n = 1, r, repeats = 1;
       +        long j, seed, *counts = NULL, n = 1, r, repeats = 1, ratio = 0;
                double val = 0.0, weightsum = 0.0, *weights = NULL;
                struct timeval t1;
        
       t@@ -39,6 +39,9 @@ main(int argc, char *argv[])
                case 'r':
                        repeats = atol(EARGF(usage()));
                        break;
       +        case 'R':
       +                ratio = 1;
       +                break;
                case 's':
                        s = 1;
                        seed = atol(EARGF(usage()));
       t@@ -94,7 +97,10 @@ main(int argc, char *argv[])
                                }
                        }
                        for (i = 0; i < N; i++) {
       -                        printf("%ld", counts[i]);
       +                        if (ratio)
       +                                printf("%.17g", (double)counts[i] / n);
       +                        else
       +                                printf("%ld", counts[i]);
                                if (i < N - 1)
                                        putchar('\t');
                                else