tile method

num tile(
  1. num p
)

The p-tile of the stored data, where 0 ≤ p ≤ 1.

For example, lowerQuartile is equivalent to tile(0.25).

Implementation

num tile(num p) {
  if (p > 1) {
    p /= 100;
  }

  if (p < 0 || p > 1) {
    throw PackhorseError.badArgument(
        "p = $p should be in [0, 1] or [0, 100].");
  }
  final d = p * (length - 1),
      index = d.toInt(),
      r = d - index,
      sortedValues = values..sort(),
      dy = sortedValues[index + 1] - sortedValues[index];
  return sortedValues[index] + r * dy;
}