percentiles.c 510 Bytes
Newer Older
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1
#include <math.h>
2
#include "util.h"
Uwe Schulzweida's avatar
Uwe Schulzweida committed
3
4
5
#include "percentiles.h"
#include "nth_element.h"

6
7

double percentile(double *array, unsigned len, double pn)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
8
{
9
10
11
12
  int element = (int)ceil(len*(pn/100.0))-1;
  if ( element <    0 ) element = 0;
  if ( element >= len ) element = len-1;
  double percentil = nth_element(array, len, element);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
13
14
  return percentil;
}
15
16
17
18
19
20
21


void percentile_check_number(double pn)
{
  if ( pn < 0 || pn > 100 )
    cdoAbort("Illegal argument: percentile number %g is not in the range 0..100!", pn);
}