diff --git a/loadtester/variance.go b/loadtester/variance.go index 7a063b6..ede67fa 100644 --- a/loadtester/variance.go +++ b/loadtester/variance.go @@ -47,13 +47,17 @@ func (wv *welfordVariance) Variance(count int) float64 { // // Note that if you copy this function it really should panic if the input is negative. Variance cannot be negative! func varianceFloatString(f float64) string { + // round first thing because it can cause an overflow on various CPU architectures + f = math.Round(f) + if math.IsNaN(f) { return "" } v := int64(math.MaxInt64) if !math.IsInf(f, 1) { - v = int64(math.Round(f)) + // converting to int64 requires a "& math.MaxInt64" because it can cause an overflow on various CPU architectures + v = int64(f) & math.MaxInt64 } return strconv.FormatInt(v, 10) diff --git a/loadtester/variance_test.go b/loadtester/variance_test.go index c821fce..fac20a3 100644 --- a/loadtester/variance_test.go +++ b/loadtester/variance_test.go @@ -20,7 +20,7 @@ func Test_varianceFloatString(t *testing.T) { } { // garbage in - garbage out case - exp := "-9223372036854775808" + exp := "0" if v := varianceFloatString(math.Inf(-1)); v != exp { t.Fatalf("expected %s string, got %s", exp, v) } @@ -33,7 +33,7 @@ func Test_varianceFloatString(t *testing.T) { } { // garbage in - garbage out case - exp := "-1" + exp := "9223372036854775807" if v := varianceFloatString(-1.0); v != exp { t.Fatalf("expected %s string, got %s", exp, v) }