ping: Avoid reporting NaNs

Avoid calculating the square root of negative zero, which can easily
happen on certain architectures when calculating the population standard
deviation with a sample size of one, e.g., 0.01 - (0.1 * 0.1) =
-0.000000.

Avoid returning a NaN by capping the minimum possible variance value to
zero (positive).

In the future, maybe skip reporting statistics at all for a single
sample.

Reported by:	Jenkins
Reviewed by:	asomers
MFC after:	1 week
Pull Request:	https://github.com/freebsd/freebsd-src/pull/863
Differential Revision:	https://reviews.freebsd.org/D42114

(cherry picked from commit 4d348e83b7)
This commit is contained in:
Jose Luis Duran 2023-10-06 17:55:06 +00:00 committed by Mark Johnston
parent 221a60a426
commit 29667c6fcc
2 changed files with 4 additions and 4 deletions

View file

@ -1521,10 +1521,10 @@ finish(void)
if (nreceived && timing) {
double n = nreceived + nrepeats;
double avg = tsum / n;
double vari = tsumsq / n - avg * avg;
double stddev = sqrt(fmax(0, tsumsq / n - avg * avg));
(void)printf(
"round-trip min/avg/max/stddev = %.3f/%.3f/%.3f/%.3f ms\n",
tmin, avg, tmax, sqrt(vari));
tmin, avg, tmax, stddev);
}
if (nreceived)

View file

@ -2349,10 +2349,10 @@ summary(void)
/* Only display average to microseconds */
double num = nreceived + nrepeats;
double avg = tsum / num;
double dev = sqrt(tsumsq / num - avg * avg);
double stddev = sqrt(fmax(0, tsumsq / num - avg * avg));
(void)printf(
"round-trip min/avg/max/std-dev = %.3f/%.3f/%.3f/%.3f ms\n",
tmin, avg, tmax, dev);
tmin, avg, tmax, stddev);
(void)fflush(stdout);
}
(void)fflush(stdout);