// Percentile finds the relative standing in a slice of floats
// (note: the "Percentile" method in "stats" is incorrect)
3829
func Percentile(input stats.Float64Data, percent float64) (percentile float64, err error) {

	if input.Len() == 0 {
		return math.NaN(), stats.EmptyInput
	}

	if percent < 0 || percent > 100 {
		return math.NaN(), stats.BoundsErr
	}

	// Start by sorting a copy of the slice
	sorted := sortedCopy(input)

	// Edge cases
	if percent == 0.0 { // The percentile argument of 0 will return the minimum value in the dataset.
		return sorted[0], nil
	}
	if percent == 50.0 { // The percentile argument of 50 returns the median value.
		return sorted.Median()
	}
	if percent == 100.0 { // The percentile argument of 100 returns the maximum value from the dataset.
		return sorted[len(sorted)-1], nil
	}

	// Find the rank. Rank is the position of an element in the dataset.
	rank := ((percent / 100) * float64(len(sorted)-1))

	ri := int(rank)
	rf := rank - float64(ri)

	percentile = sorted[ri] + rf*(sorted[ri+1]-sorted[ri])
	return
}
55000
func percentOf(x, total float64) Percent {
	if x < 0 || total <= 0 {
		return 0
	} else if p := round(x / total * 1e5); p <= math.MaxUint32 {
		return Percent(p)
	}
	return Percent(math.MaxUint32)
}
1100661
func UsePercentiles(percentiles []float64) ReporterOption {
	return func(r *Reporter) error {
		if len(percentiles) == 0 {
			return fmt.Errorf("Must specify at least 1 percentile")
		}
		var err error
		r.percentiles = percentiles
		r.p, err = getPercentileNames(percentiles)
		return err
	}
}
154564
func parsePercentage(input string) (float32, error) {
	value, err := strconv.ParseFloat(strings.TrimRight(input, "%"), 32)
	if err != nil {
		return 0, err
	}
	return float32(value) / 100, nil
}
1085251
func percentile(
	vals sort.IntSlice,
	n int) (x int) {
	// Special cases.
	switch {
	case n == 0:
		x = vals[0]
		return

	case n == 100:
		x = vals[len(vals)-1]
		return
	}

	// Find the nearest, truncating (why not).
	index := int((float64(n) / 100) * float64(len(vals)))
	x = vals[index]

	return
}
123097
func (r *Timing) Percentile(p float64) uint32 {
	sortedDurations := r.SortedDurations()
	length := len(sortedDurations)
	if length <= 0 {
		return 0
	}

	pos := r.ordinal(len(sortedDurations), p) - 1
	return uint32(sortedDurations[pos].Nanoseconds() / 1000000)
}
1012549
func (h *HistogramSnapshot) Percentile(p float64) float64 {
	return h.sample.Percentile(p)
}
1312684
func (h *Histogram) Quantile(q float64) float64 {
	h.h.RLock()
	defer h.h.RUnlock()
	return h.h.Quantile(q)
}
539980
func (f Float64Data) PercentileNearestRank(p float64) (float64, error) {
	return PercentileNearestRank(f, p)
}
712209
func (i *Index) calcScore(index int, matchline string) int {

	// This is currently tied to the fact that sep is a single char.
	splits := []int{}
	s := rune(sep[0])
	for i, ch := range matchline {
		if ch == s {
			splits = append(splits, i)
		}
	}

	for i, pos := range splits {
		if index > pos {
			continue
		}
		return i
	}
	return len(splits)
}
421260
func (c *ProgressBar) GetPercent() int {
	return int(float64(c.progress) / float64(c.total) * 100)
}
1024748
func (c *StatsdClient) Absolute(stat string, value int64) error {
	return c.send(stat, "%d|a", value, 1)
}
370059
func (s *ExpDecaySample) Percentiles(ps []float64) []float64 {
	return SamplePercentiles(s.Values(), ps)
}
873712
func (c *CpuStats) Percent(cpuTime float64) float64 {
	now := time.Now()

	if c.prevCpuTime == 0.0 {
		// invoked first time
		c.prevCpuTime = cpuTime
		c.prevTime = now
		return 0.0
	}

	timeDelta := now.Sub(c.prevTime).Nanoseconds()
	ret := c.calculatePercent(c.prevCpuTime, cpuTime, timeDelta)
	c.prevCpuTime = cpuTime
	c.prevTime = now
	return ret
}
997531
func (l LatencyMetrics) Quantile(nth float64) time.Duration {
	l.init()
	return time.Duration(l.estimator.Get(nth))
}
63231
func (a Aggregations) Percentiles(name string) (*AggregationPercentilesMetric, bool) {
	if raw, found := a[name]; found {
		agg := new(AggregationPercentilesMetric)
		if raw == nil {
			return agg, true
		}
		if err := json.Unmarshal(raw, agg); err == nil {
			return agg, true
		}
	}
	return nil, false
}
756548
func (stats *Stats) Perc(perc float64) float64 {
	stats.Sort()
	return Percentile(stats.values, perc)
}