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

Make it harder for Mean() and StdDev() to overflow #21

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions hdr.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,14 +155,15 @@ func (h *Histogram) Mean() float64 {
if h.totalCount == 0 {
return 0
}
var total int64
var mean float64
totalCount := float64(h.totalCount)
i := h.iterator()
for i.next() {
if i.countAtIdx != 0 {
total += i.countAtIdx * h.medianEquivalentValue(i.valueFromIdx)
mean += float64(i.countAtIdx*h.medianEquivalentValue(i.valueFromIdx)) / totalCount
}
}
return float64(total) / float64(h.totalCount)
return mean
}

// StdDev returns the approximate standard deviation of the recorded values.
Expand All @@ -173,16 +174,17 @@ func (h *Histogram) StdDev() float64 {

mean := h.Mean()
geometricDevTotal := 0.0
totalCount := float64(h.totalCount)

i := h.iterator()
for i.next() {
if i.countAtIdx != 0 {
dev := float64(h.medianEquivalentValue(i.valueFromIdx)) - mean
geometricDevTotal += (dev * dev) * float64(i.countAtIdx)
geometricDevTotal += (dev * dev) * float64(i.countAtIdx) / totalCount
}
}

return math.Sqrt(geometricDevTotal / float64(h.totalCount))
return math.Sqrt(geometricDevTotal)
}

// Reset deletes all recorded values and restores the histogram to its original
Expand Down