Skip to content

Commit

Permalink
Fix percentile when only one element (#47)
Browse files Browse the repository at this point in the history
* fix percentile return error when only one element

* add percentile test

* update percentile

Closes #46
  • Loading branch information
shiyan2016 authored and montanaflynn committed Jul 9, 2019
1 parent 38304a2 commit dfab42c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
8 changes: 6 additions & 2 deletions percentile.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@ import (

// Percentile finds the relative standing in a slice of floats
func Percentile(input Float64Data, percent float64) (percentile float64, err error) {

if input.Len() == 0 {
length := input.Len()
if length == 0 {
return math.NaN(), EmptyInputErr
}

if length == 1 {
return input[0], nil
}

if percent <= 0 || percent > 100 {
return math.NaN(), BoundsErr
}
Expand Down
4 changes: 4 additions & 0 deletions percentile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ func TestPercentile(t *testing.T) {
if m != 64.0 {
t.Errorf("%.1f != %.1f", m, 64.0)
}
m, _ = Percentile([]float64{43}, 90)
if m != 43.0 {
t.Errorf("%.1f != %.1f", m, 43.0)
}
m, _ = Percentile([]float64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, 50)
if m != 5.0 {
t.Errorf("%.1f != %.1f", m, 5.0)
Expand Down

0 comments on commit dfab42c

Please sign in to comment.