Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overflow while calculating mean #20

Open
cassava opened this issue Aug 24, 2016 · 0 comments · May be fixed by #21
Open

Overflow while calculating mean #20

cassava opened this issue Aug 24, 2016 · 0 comments · May be fixed by #21

Comments

@cassava
Copy link

cassava commented Aug 24, 2016

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)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants