All contributions

montanaflynn/stats

stats

A Go statistics library with no dependencies: means, median, quartiles, percentiles, outliers and statistical tests.

Language
Go
GitHub stars
3,031
Merged changes
1

What we changed

Quartile and the functions built on it, InterQuartileRange, Midhinge, Trimean and QuartileOutliers, return an error for single-element input instead of NaN with a nil error.

What this work shows

  • Error handling in Go
  • Descriptive statistics: quartiles and IQR
  • NaN values and edge-case input
What was broken

One number gave NaN quartiles, no error

Quartile([]float64{1}) returned {Q1:NaN Q2:1 Q3:NaN} with a nil err, because the errors from Median were assigned to _. The same NaN flowed on into InterQuartileRange, Midhinge and Trimean, so code that only checked err != nil got NaN as its result.

How we fixed it

A single element is now an error

Quartile returns EmptyInputErr for fewer than two elements, and InterQuartileRange, Midhinge, Trimean and QuartileOutliers pass that error on instead of dropping it. Each of these functions has a single-element test, and the docs say at least two elements are needed.

#135

Merged change

1 9 September 2026

What it taught us

We catch errors swallowed in Go by assigning them to _: here NaN passed through several functions as a valid result. The fix passes the error up and has a test for every function it touches.

More about this technology: Golang

Other projects we write code for

Describe your project.

A free quote comes back within an hour during working hours.