# HG changeset patch # User Giulio Moro # Date 1442176297 -3600 # Node ID aac018615930917a97097dfff5f7f33063b8c168 # Parent 772dbb57442b8db71e01a61ceda1ed736e28c8c4 MovingAverage now returns a meaningful value also when the buffer is not full diff -r 772dbb57442b -r aac018615930 include/stats.hpp --- a/include/stats.hpp Sun Sep 13 21:26:38 2015 +0100 +++ b/include/stats.hpp Sun Sep 13 21:31:37 2015 +0100 @@ -9,12 +9,13 @@ private: TYPE* array; int length; + bool bufferFull; int pointer; TYPE sum; double scale; double average; void dealloc(){ - free(array); + free(array); // delete array; } @@ -28,6 +29,7 @@ printf("Error while allocating array\n"); memset(array, 0, sizeof(TYPE)*length); pointer=0; + bufferFull=false; } public: MovingAverage(){ @@ -50,10 +52,17 @@ sum-=array[pointer]; array[pointer]=newElement; sum+=newElement; - average=sum*scale; + if(bufferFull==true){ + average=sum*scale; + } + else{ + average=sum/(double)(1+pointer); + } pointer++; - if(pointer==length) + if(pointer==length){ pointer=0; + bufferFull=true; + } return average; } double getAverage(){ @@ -61,4 +70,4 @@ } }; -#endif /* STATS_HPP_INCLUDED */ \ No newline at end of file +#endif /* STATS_HPP_INCLUDED */