From b3fd6ddf73aeefd9d3bb2e92f463ce20be35bb8e Mon Sep 17 00:00:00 2001 From: emaste Date: Wed, 21 Feb 2018 15:54:23 +0000 Subject: [PATCH] ministat: disallow negative variance / nan Stddev With all values identical it was possible for Var() to return a negative value due to limited floating point precision, resulting in "nan" reported as Stddev. Variance cannot actually be negative, so just return 0. We can later investigate alternate algorithms for calculating variance to reduce the effect of catastrophic cancellation here. Reported by: Arshan Khanifar Approved by: phk Sponsored by: The FreeBSD Foundation --- usr.bin/ministat/ministat.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/usr.bin/ministat/ministat.c b/usr.bin/ministat/ministat.c index 625fb329e2b8..150593e5be95 100644 --- a/usr.bin/ministat/ministat.c +++ b/usr.bin/ministat/ministat.c @@ -208,6 +208,12 @@ static double Var(struct dataset *ds) { + /* + * Due to limited precision it is possible that sy^2/n > syy, + * but variance cannot actually be negative. + */ + if (ds->syy <= ds->sy * ds->sy / ds->n) + return (0); return (ds->syy - ds->sy * ds->sy / ds->n) / (ds->n - 1.0); }