Histograms: Put hard limit on the number of histogram bins
authorJohn Darrington <john@darrington.wattle.id.au>
Sun, 25 Mar 2012 08:47:27 +0000 (10:47 +0200)
committerJohn Darrington <john@darrington.wattle.id.au>
Sun, 25 Mar 2012 08:47:27 +0000 (10:47 +0200)
Histograms with less than one bin are not useful (and tend to cause crashes!)
Histograms with very large numbers of bins are also of little use, and cause
other implementation headaches.

src/math/histogram.c

index 2f19f310954c5d767a12ee309986c9f87cc87ede..5441df6bbc50b22068df11cc0977bb83bde6f599 100644 (file)
@@ -1,5 +1,5 @@
 /* PSPP - a program for statistical analysis.
-   Copyright (C) 2004, 2008, 2009, 2011 Free Software Foundation, Inc.
+   Copyright (C) 2004, 2008, 2009, 2011, 2012 Free Software Foundation, Inc.
 
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -40,8 +40,6 @@ histogram_add (struct histogram *h, double y, double c)
   stat->accumulate (stat, NULL, c, 0, y);
 }
 
-
-
 static void
 acc (struct statistic *s, const struct ccase *cx UNUSED, double c, double cc UNUSED, double y)
 {
@@ -50,7 +48,6 @@ acc (struct statistic *s, const struct ccase *cx UNUSED, double c, double cc UNU
   gsl_histogram_accumulate (hist->gsl_hist, y, c);
 }
 
-
 static void
 destroy (struct statistic *s)
 {
@@ -114,6 +111,13 @@ histogram_create (double bin_width, double min, double max)
 
   bins = (upper_limit - lower_limit) / 2.0;
 
+  /* Force the number of bins to lie in a sensible range */
+  if (bins > 25) 
+    bins = 25;
+
+  if (bins < 1)
+    bins = 1;
+
   upper_limit *= half_bin_width;
   lower_limit *= half_bin_width;