+ *n_half_bins = ipart;
+
+ return remainder * half_bin_width;
+}
+
+
+/* This functions adjusts the upper and lower range of the histogram to make them fit BIN_WIDTH
+ MIN and MAX are the lowest and highest data to be plotted in the histogram.
+ ADJ_MIN and ADJ_MAX are locations of the adjusted values of MIN and MAX (the range will
+ always be equal or slightly larger).
+ Returns the number of bins.
+
+ The "testing_assert" expressions in this function should be algebraically correct.
+ However, due to floating point rounding they could fail, especially when small numbers
+ are involved. In normal use, therefore, testing_assert does nothing.
+ */
+static int
+adjust_bin_ranges (double bin_width, double min, double max, double *adj_min, double *adj_max)
+{
+ const double half_bin_width = bin_width / 2.0;
+
+ /* The lower and upper limits of the histogram, in units of half
+ bin widths */
+ int lower_limit, upper_limit;
+
+ double lower_slack = get_slack (min, half_bin_width, &lower_limit);
+ double upper_slack = -get_slack (max, half_bin_width, &upper_limit);
+
+ testing_assert (max > min);
+
+ /* If min is negative, then lower_slack may be less than zero.
+ In this case, the lower bound must be extended in the negative direction
+ so that it is less than OR EQUAL to min.
+ */
+ if (lower_slack < 0)
+ {
+ lower_limit--;
+ lower_slack += half_bin_width;
+ }
+ testing_assert (lower_limit * half_bin_width <= min);
+
+ /* However, the upper bound must be extended regardless, because histogram bins
+ span the range [lower, upper). In other words, the upper bound must be
+ greater than max.
+ */
+ upper_limit++;;
+ upper_slack += half_bin_width;
+ testing_assert (upper_limit * half_bin_width > max);
+
+ /* The range must be an EVEN number of half bin_widths */
+ if ( (upper_limit - lower_limit) % 2)