diff --git a/sliceops.go b/sliceops.go index 9affb433..66e02516 100644 --- a/sliceops.go +++ b/sliceops.go @@ -115,7 +115,7 @@ func CumProd(receiver, s []float64) []float64 { } receiver[0] = s[0] for i := 1; i < len(s); i++ { - receiver[i] = receiver[i-1] + s[i] + receiver[i] = receiver[i-1] * s[i] } return receiver } diff --git a/sliceops_test.go b/sliceops_test.go index 6163e44c..c1451eab 100644 --- a/sliceops_test.go +++ b/sliceops_test.go @@ -115,3 +115,16 @@ func TestCumSum(t *testing.T) { t.Errorf("Wrong cumsum returned with non-nil receiver. Expected %v, returned %v", truth, val) } } + +func TestCumProd(t *testing.T) { + s := []float64{3, 4, 1, 7, 5} + val := CumProd(nil, s) + truth := []float64{3, 12, 12, 84, 420} + if !Eq(val, truth, 1E-15) { + t.Errorf("Wrong cumprod returned with nil receiver. Expected %v, returned %v", truth, val) + } + val = CumProd(val, s) + if !Eq(val, truth, 1E-15) { + t.Errorf("Wrong cumprod returned with non-nil receiver. Expected %v, returned %v", truth, val) + } +}