| 97 | } |
| 98 | |
| 99 | func TestStatVar_NumericalStability(t *testing.T) { |
| 100 | // Test with large values close together (catastrophic cancellation case). |
| 101 | // Mean = 1e9 + 0.5, stddev should be ~0.5 |
| 102 | var s StatVar |
| 103 | for range 1000 { |
| 104 | v := 1e9 + float64(s.Count()%2) // alternates between 1e9 and 1e9+1 |
| 105 | s.Add(v) |
| 106 | } |
| 107 | // 500 observations at 1e9 and 500 at 1e9+1 → mean = 1e9 + 0.5 |
| 108 | assert.InDelta(t, 1e9+0.5, s.Mean(), 1e-6, "stability: mean should be ~1e9+0.5") |
| 109 | } |
| 110 | |
| 111 | func TestStatVar_MedianDoesNotMutateState(t *testing.T) { |
| 112 | var s StatVar |