Skip to content

Commit

Permalink
Modified median() to return avg of 2 middle values when list of value…
Browse files Browse the repository at this point in the history
…s is an even number
  • Loading branch information
DanAtFh committed Jan 9, 2015
1 parent e0c0c26 commit b4c1add
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion udf_median.cc
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,12 @@ double median( UDF_INIT* initid, UDF_ARGS* args, char* is_null, char* is_error )
qsort(buffer->values,buffer->abscount,sizeof(double),compare_doubles);

unsigned long bufindex = (ulong) ((((double)buffer->abscount - 1.0) * (double)buffer->percentile) / 100.0);
return buffer->values[bufindex];
if ( buffer->abscount % 2 == 0)
{
return (buffer->values[bufindex] + buffer->values[bufindex+1]) / 2;
} else {
return buffer->values[bufindex];
}
}

/* #endif */
Expand Down

0 comments on commit b4c1add

Please sign in to comment.