Fix the way in which median is calculated. If the data source has even

number of data points, value should be calculated by adding two middle
elements and dividing them by 2.

Approved by:	cognet (mentor)
This commit is contained in:
Wojciech A. Koszek 2006-02-23 20:46:10 +00:00
parent 28baad63bd
commit d2f4defee2
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=155960
2 changed files with 36 additions and 14 deletions

View File

@ -202,15 +202,26 @@ Avg(struct dataset *ds)
static double
Median(struct dataset *ds)
{
int i;
struct point *pp;
int even, i;
struct point *p1, *p2;
i = ds->n / 2;
TAILQ_FOREACH(pp, &ds->list, list) {
if (i--)
continue;
return (pp->val);
if ((ds->n % 2) == 1) {
i = (ds->n / 2) + 1;
even = 0;
} else {
i = ds->n / 2;
even = 1;
}
TAILQ_FOREACH(p1, &ds->list, list) {
--i;
if (i == 0)
break;
}
if (even) {
p2 = TAILQ_NEXT(p1, list);
return ((p2->val + p1->val) / 2);
}
return (p1->val);
}
static double

View File

@ -202,15 +202,26 @@ Avg(struct dataset *ds)
static double
Median(struct dataset *ds)
{
int i;
struct point *pp;
int even, i;
struct point *p1, *p2;
i = ds->n / 2;
TAILQ_FOREACH(pp, &ds->list, list) {
if (i--)
continue;
return (pp->val);
if ((ds->n % 2) == 1) {
i = (ds->n / 2) + 1;
even = 0;
} else {
i = ds->n / 2;
even = 1;
}
TAILQ_FOREACH(p1, &ds->list, list) {
--i;
if (i == 0)
break;
}
if (even) {
p2 = TAILQ_NEXT(p1, list);
return ((p2->val + p1->val) / 2);
}
return (p1->val);
}
static double