mirror of
https://github.com/freebsd/freebsd-src.git
synced 2024-12-01 08:33:24 +00:00
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:
parent
28baad63bd
commit
d2f4defee2
Notes:
svn2git
2020-12-20 02:59:44 +00:00
svn path=/head/; revision=155960
@ -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
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user