package datascience

// Percentile returns the value at percentile p (0.0–1.0) from a pre-sorted
// ascending slice of int64 values.
// Returns 0 for an empty slice.
// idx is computed as int(float64(len-1)*p), clamped to [0, len-1].
func Percentile(sorted []int64, p float64) int64 {
	if len(sorted) == 0 {
		return 0
	}
	idx := int(float64(len(sorted)-1) * p)
	if idx < 0 {
		idx = 0
	}
	if idx >= len(sorted) {
		idx = len(sorted) - 1
	}
	return sorted[idx]
}
