histogram: Fix a couple of bugs.
authorBen Pfaff <blp@cs.stanford.edu>
Sat, 27 May 2023 23:17:22 +0000 (16:17 -0700)
committerBen Pfaff <blp@cs.stanford.edu>
Sun, 28 May 2023 02:53:16 +0000 (19:53 -0700)
The bug report wasn't specific enough to know that this fixes
the problem, which was an assertion failure for
  assert (*adjusted_min <= min);
in hist_find_pretty_no_of_bins().

Thanks to Stefan Huber for reporting these bugs.

src/math/chart-geometry.c
src/math/histogram.c

index 7029ddd73714a1f453c85943d4048a7387fce438..eaa2c0159d6979fcb2f41e240fe9336e72696b4c 100644 (file)
@@ -57,7 +57,6 @@ chart_get_scale (double high, double low,
                 double *lower, double *interval,
                 int *n_ticks)
 {
-  int i;
   double fitness = DBL_MAX;
   double logrange;
   *n_ticks = 0;
@@ -74,7 +73,7 @@ chart_get_scale (double high, double low,
   logrange = floor(log10(high-low));
 
   /* Find the most pleasing interval */
-  for (i = 1; i < 4; ++i)
+  for (int i = 0; i < sizeof standard_tick / sizeof *standard_tick; i++)
     {
       double cinterval = standard_tick[i] * pow(10.0,logrange-1);
       double clower = floor(low/cinterval) * cinterval;
index 264b0785351b99c9da8c3397294a0ef1bb1f716f..f054f824b8f2c5c7cbc72615d8582fae58ad0ee9 100644 (file)
@@ -107,6 +107,10 @@ hist_find_pretty_no_of_bins(double bin_width_in, double min, double max,
       *adjusted_min = floor((min - lower)/binwidth)*binwidth + lower;
     }
 
+  /* This should not happen, but sometimes it does. */
+  if (*adjusted_min > min)
+    *adjusted_min = min;
+
   nbins = ceil((max-*adjusted_min)/binwidth);
   *adjusted_max = nbins*binwidth + *adjusted_min;