percentile static method

double percentile(
  1. Iterable<Object?> values,
  2. double percentile
)

Percentile (0..100) using linear interpolation over sorted byte values.

Implementation

static double percentile(Iterable<Object?> values, double percentile) {
  if (percentile < 0 || percentile > 100) {
    throw ArgumentError('Percentile must be between 0 and 100 inclusive');
  }
  final sorted = values.map(_toDouble).toList()..sort();
  if (sorted.isEmpty) {
    throw ArgumentError('Cannot compute percentile of empty collection');
  }
  if (sorted.length == 1) {
    return sorted.first;
  }
  final rank = (percentile / 100) * (sorted.length - 1);
  final lowerIndex = rank.floor();
  final upperIndex = rank.ceil();
  if (lowerIndex == upperIndex) {
    return sorted[lowerIndex];
  }
  final lowerValue = sorted[lowerIndex];
  final upperValue = sorted[upperIndex];
  final weight = rank - lowerIndex;
  return lowerValue + (upperValue - lowerValue) * weight;
}