percentile method

double percentile(
  1. double p
)

Value at p-th percentile (0–100). Uses linear interpolation.

Implementation

double percentile(double p) {
  final sorted = _finiteValuesCopy()..sort();
  if (sorted.isEmpty) return 0;
  if (sorted.length == 1) return sorted.first;
  final normalizedP = _normalizePercentile(p);
  final pos = (normalizedP / 100) * (sorted.length - 1);
  final lo = pos.floor();
  final hi = pos.ceil();
  if (lo == hi) return sorted[lo];
  return sorted[lo] + (sorted[hi] - sorted[lo]) * (pos - lo);
}