gnuastro-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[gnuastro-commits] master 0e34981: statistics library: properly accounti


From: Mohammad Akhlaghi
Subject: [gnuastro-commits] master 0e34981: statistics library: properly accounting for maximum in histogram
Date: Wed, 12 Jun 2019 13:08:18 -0400 (EDT)

branch: master
commit 0e349812dabea5cb908b5052e02aa120010d2a1e
Author: Mohammad Akhlaghi <address@hidden>
Commit: Mohammad Akhlaghi <address@hidden>

    statistics library: properly accounting for maximum in histogram
    
    Until now, the last element's count would be written into a patch of memory
    right outside the allocated space for the histogram values. This has been
    corrected with a better implementation of the counting step to account for
    situations when floating point errors cause this situation.
    
    This bug was reported by Raul Infante-Sainz.
    
    This fixes bug #56480.
---
 NEWS             |  1 +
 lib/statistics.c | 14 ++++++++------
 2 files changed, 9 insertions(+), 6 deletions(-)

diff --git a/NEWS b/NEWS
index 038ab54..b80c6d2 100644
--- a/NEWS
+++ b/NEWS
@@ -100,6 +100,7 @@ See the end of the file for license conditions.
   bug #56299: CosmicCalculator fails at z=0.
   bug #56324: Column metadata not usable when input is from pipe/stdin.
   bug #56424: Warp crashes with empty string given to options.
+  bug #56480: Segfault in statistics library's histogram function.
 
 
 
diff --git a/lib/statistics.c b/lib/statistics.c
index f65bf4e..e6e6024 100644
--- a/lib/statistics.c
+++ b/lib/statistics.c
@@ -1634,11 +1634,14 @@ gal_statistics_regular_bins(gal_data_t *input, 
gal_data_t *inrange,
 #define HISTOGRAM_TYPESET(IT) {                                         \
     IT *a=input->array, *af=a+input->size;                              \
     do                                                                  \
-      if(*a>=min)                                                       \
+      if(*a>=min && *a<=max)                                            \
         {                                                               \
           h_i=(*a-min)/binwidth;                                        \
-          if(h_i == last_i) { if( *a<=max ) ++h[ h_i ]; }               \
-          else              { if( *a< max ) ++h[ h_i ]; }               \
+          /* When `*a' is the largest element (within floating point */ \
+          /* errors), `h_i' can be one element larger than the       */ \
+          /* number of bins. But since its in the dataset, we need   */ \
+          /* to count it. So we'll put it in the last bin.           */ \
+          ++h[ h_i - (h_i==hist->size ? 1 : 0) ];                       \
         }                                                               \
     while(++a<af);                                                      \
   }
@@ -1648,8 +1651,8 @@ gal_statistics_histogram(gal_data_t *input, gal_data_t 
*bins, int normalize,
                          int maxone)
 {
   float *f, *ff;
+  size_t *h, h_i;
   gal_data_t *hist;
-  size_t *h, h_i, last_i;
   double *d, min, max, ref=NAN, binwidth;
 
 
@@ -1681,9 +1684,8 @@ gal_statistics_histogram(gal_data_t *input, gal_data_t 
*bins, int normalize,
   /* Set the minimum and maximum range of the histogram from the bins. */
   d=bins->array;
   binwidth=d[1]-d[0];
-  last_i=bins->size-1;
   min = d[ 0      ] - binwidth/2;
-  max = d[ last_i ] + binwidth/2;
+  max = d[ bins->size-1 ] + binwidth/2;
 
 
   /* Go through all the elements and find out which bin they belong to. */



reply via email to

[Prev in Thread] Current Thread [Next in Thread]