function getPercentile()

in packages/core/src/utils.js [8:21]


function getPercentile(sorted, percentile) {
  const n = sorted.length;
  if (n === 0) return undefined;

  const rank = (percentile / 100) * (n - 1);
  const lowerIndex = Math.floor(rank);
  const upperIndex = Math.ceil(rank);
  const weight = rank - lowerIndex;

  if (upperIndex >= n) {
    return sorted[lowerIndex];
  }
  return sorted[lowerIndex] * (1 - weight) + sorted[upperIndex] * weight;
}