Got sick of dealing with errors

- just return 0.0 for empty OnlineStats
This commit is contained in:
Stephen McQuay 2014-01-01 10:46:34 -08:00
parent ca6120d392
commit 84b41cf7fe
2 changed files with 32 additions and 29 deletions

View File

@ -1,7 +1,6 @@
package ostat package ostat
import ( import (
"errors"
"math" "math"
) )
@ -46,21 +45,20 @@ func (os *OnlineStat) Push(v float64) {
os.m2 = os.m2 + delta*(v-os.mean) os.m2 = os.m2 + delta*(v-os.mean)
} }
func (os *OnlineStat) Mean() (float64, error) { func (os *OnlineStat) Mean() float64 {
if os.n == 0 { if os.n == 0 {
return 0.0, errors.New("no data") return 0.0
} }
return os.mean, nil return os.mean
} }
func (os *OnlineStat) Variance() (float64, error) { func (os *OnlineStat) Variance() float64 {
if os.n == 0 { if os.n == 0 {
return 0.0, errors.New("no data") return 0.0
} }
return os.m2 / float64(os.n-os.typ), nil return os.m2 / float64(os.n-os.typ)
} }
func (os *OnlineStat) StdDev() (float64, error) { func (os *OnlineStat) StdDev() float64 {
variance, err := os.Variance() return math.Sqrt(os.Variance())
return math.Sqrt(variance), err
} }

View File

@ -1,6 +1,7 @@
package ostat package ostat
import ( import (
"log"
"math" "math"
"testing" "testing"
) )
@ -93,43 +94,47 @@ func TestInsert(t *testing.T) {
t.Errorf("incorrectly calculated max: %f != %f", ps.Max, test.max) t.Errorf("incorrectly calculated max: %f != %f", ps.Max, test.max)
} }
pmean, _ := ps.Mean() if ps.Mean() != ss.Mean() {
smean, _ := ss.Mean()
if pmean != smean {
t.Errorf("Means don't match") t.Errorf("Means don't match")
} }
if m, _ := ps.Mean(); math.Abs(m-test.mean) > tolerance { if math.Abs(ps.Mean()-test.mean) > tolerance {
t.Errorf("incorrect mean: %f != %f", m, test.mean) t.Errorf("incorrect mean: %f != %f", ps.Mean(), test.mean)
} }
if variance, _ := ps.Variance(); math.Abs(variance-test.pvariance) > tolerance { if math.Abs(ps.Variance()-test.pvariance) > tolerance {
t.Errorf("incorrect variance: %f != %f", variance, test.pvariance) t.Errorf("incorrect variance: %f != %f", ps.Variance(), test.pvariance)
} }
if stdev, _ := ps.StdDev(); math.Abs(stdev-test.pstdev) > tolerance { if math.Abs(ps.StdDev()-test.pstdev) > tolerance {
t.Errorf("incorrect stdev: %f != %f", stdev, test.pstdev) t.Errorf("incorrect stdev: %f != %f", ps.StdDev(), test.pstdev)
} }
if variance, _ := ss.Variance(); math.Abs(variance-test.svariance) > tolerance { if math.Abs(ss.Variance()-test.svariance) > tolerance {
t.Errorf("incorrect variance: %f != %f", variance, test.svariance) t.Errorf("incorrect variance: %f != %f", ss.Variance(), test.svariance)
} }
if stdev, _ := ss.StdDev(); math.Abs(stdev-test.sstdev) > tolerance { if math.Abs(ss.StdDev()-test.sstdev) > tolerance {
t.Errorf("incorrect stdev: %f != %f", stdev, test.sstdev) t.Errorf("incorrect stdev: %f != %f", ss.StdDev(), test.sstdev)
} }
} }
} }
func TestEmpty(t *testing.T) { func TestEmpty(t *testing.T) {
ps := NewSampleStat() ps := NewSampleStat()
_, err := ps.Mean() if ps.Mean() != 0.0 {
if err == nil {
t.Errorf("failure to notify the running stat was empty") t.Errorf("failure to notify the running stat was empty")
} }
_, err = ps.Variance() if ps.Variance() != 0.0 {
if err == nil {
t.Errorf("failure to notify the running stat was empty") t.Errorf("failure to notify the running stat was empty")
} }
_, err = ps.StdDev() if ps.StdDev() != 0.0 {
if err == nil {
t.Errorf("failure to notify the running stat was empty") t.Errorf("failure to notify the running stat was empty")
} }
} }
func TestReconstitutedStat(t *testing.T) {
os := OnlineStat{
n: 3,
mean: 8,
m2: 21 * 3,
}
log.Printf("%+v: mean: %f, stddev: %f", os, os.Mean(), os.StdDev())
}