Skip to content

Commit

Permalink
Merge pull request #11 from christineyen/master
Browse files Browse the repository at this point in the history
Ensure returned values stay within bounds
  • Loading branch information
caio authored Oct 11, 2017
2 parents d604b3c + 2481326 commit a72570c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
4 changes: 3 additions & 1 deletion tdigest.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ func (t *TDigest) Quantile(q float64) float64 {
})

if found {
return result
min := t.summary.keys[0]
max := t.summary.keys[len(t.summary.keys)-1]
return math.Min(max, math.Max(result, min))
}
return t.summary.Max().mean
}
Expand Down
19 changes: 19 additions & 0 deletions tdigest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,25 @@ func TestNonSequentialInsertion(t *testing.T) {
}
}

func TestRespectBounds(t *testing.T) {
tdigest := New(10)

data := []float64{0, 279, 2, 281}
for _, f := range data {
tdigest.Add(f, 1)
}

quantiles := []float64{0.01, 0.25, 0.5, 0.75, 0.999}
for _, q := range quantiles {
if tdigest.Quantile(q) < 0 {
t.Errorf("should never return a result less than the min")
}
if tdigest.Quantile(q) > 281 {
t.Errorf("should never return a result larger than the max")
}
}
}

func TestWeights(t *testing.T) {
tdigest := New(10)

Expand Down

0 comments on commit a72570c

Please sign in to comment.