03568c88e3
- frontend/functions/core/format_datetime_short.md - frontend/functions/core/format_datetime_short.test.ts - frontend/functions/core/format_datetime_short.ts - frontend/functions/core/format_duration.md - frontend/functions/core/format_duration.test.ts - frontend/functions/core/format_duration.ts - frontend/functions/core/month_grid.md - frontend/functions/core/month_grid.test.ts - frontend/functions/core/month_grid.ts - frontend/functions/core/string_hash_palette.md - ... Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
20 lines
465 B
Go
20 lines
465 B
Go
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]
|
||
}
|