HdrHistogram / hdrhistogram-go

A pure Go implementation of Gil Tene's HDR Histogram.
MIT License
439 stars 64 forks source link

Overflow while calculating mean #20

Open cassava opened 8 years ago

cassava commented 8 years ago

I am dealing with a large distribution of values. In your mean calculation here total overflows because you keep adding without keeping it down. May I suggest to use a running mean? Because this is anyway an approximate mean, the rounding error that occurs should be negligible.

// Mean returns the approximate arithmetic mean of the recorded values.
func (h *Histogram) Mean() float64 {
    if h.totalCount == 0 {
        return 0
    }
    var total int64
    i := h.iterator()
    for i.next() {
        if i.countAtIdx != 0 {
            total += i.countAtIdx * h.medianEquivalentValue(i.valueFromIdx)
        }
    }
    return float64(total) / float64(h.totalCount)
}