// FmtPercent returns 'num' with digits/precision of 'v' for 'sbp' and handles both Whole and Real numbers based on 'v'
// NOTE: 'num' passed into FmtPercent is assumed to be in percent already
779933
func (sbp *sbp) FmtPercent(num float64, v uint64) string {
	s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
	l := len(s) + 1
	b := make([]byte, 0, l)

	for i := len(s) - 1; i >= 0; i-- {

		if s[i] == '.' {
			b = append(b, sbp.decimal[0])
			continue
		}

		b = append(b, s[i])
	}

	if num < 0 {
		b = append(b, sbp.minus[0])
	}

	// reverse
	for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
		b[i], b[j] = b[j], b[i]
	}

	b = append(b, sbp.percent...)

	return string(b)
}
1293992
func percentage(value, total int64) string {
	var ratio float64
	if total != 0 {
		ratio = float64(value) / float64(total) * 100
	}
	switch {
	case ratio >= 99.95:
		return "  100%"
	case ratio >= 1.0:
		return fmt.Sprintf("%5.2f%%", ratio)
	default:
		return fmt.Sprintf("%5.2g%%", ratio)
	}
}
708428
func PropValNum(reply *xproto.GetPropertyReply, err error) (uint, error) {
	if err != nil {
		return 0, err
	}
	if reply.Format != 32 {
		return 0, fmt.Errorf("PropValNum: Expected format 32 but got %d",
			reply.Format)
	}
	return uint(xgb.Get32(reply.Value)), nil
}
422599
func fmtB(dst EncodingBuffer, neg bool, mant uint64, exp int, flt *floatInfo) {
	// sign
	if neg {
		dst.WriteByte('-')
	}

	// mantissa
	formatBits(dst, mant, 10, false)

	// p
	dst.WriteByte('p')

	// ±exponent
	exp -= int(flt.mantbits)
	if exp >= 0 {
		dst.WriteByte('+')
	}
	formatBits(dst, uint64(exp), 10, exp < 0)

	return
}
1367366
func (ds *DefaultSimilarity) lengthNorm(state *index.FieldInvertState) float32 {
	var numTerms int
	if ds.discountOverlaps {
		numTerms = state.Length() - state.NumOverlap()
	} else {
		numTerms = state.Length()
	}
	return state.Boost() * float32(1.0/math.Sqrt(float64(numTerms)))
}
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
}
421260
func (c *ProgressBar) GetPercent() int {
	return int(float64(c.progress) / float64(c.total) * 100)
}
631047
func (a *Attribute) numberComparator(b *Attribute) (int, bool) {
	// If they are both integers we do perfect precision comparisons
	if a.Int != nil && b.Int != nil {
		return a.intComparator(b)
	}

	// Push both into the float space
	af := a.getBigFloat()
	bf := b.getBigFloat()
	if af == nil || bf == nil {
		return 0, false
	}

	return af.Cmp(bf), true
}
199238
func SamplingPercent(p int) TracerOption {
	if p < 0 || p > 100 {
		panic("sampling rate must be between 0 and 100")
	}
	return func(o *tracerOptions) *tracerOptions {
		o.samplingPercent = p
		return o
	}
}
1208213
func Percentage(value, total int64) string {
	var ratio float64
	if total != 0 {
		ratio = math.Abs(float64(value)/float64(total)) * 100
	}
	switch {
	case math.Abs(ratio) >= 99.95 && math.Abs(ratio) <= 100.05:
		return "  100%"
	case math.Abs(ratio) >= 1.0:
		return fmt.Sprintf("%5.2f%%", ratio)
	default:
		return fmt.Sprintf("%5.2g%%", ratio)
	}
}
668101
func PercentValueFormatter(v interface{}) string {
	if typed, isTyped := v.(float64); isTyped {
		return FloatValueFormatterWithFormat(typed*100.0, DefaultPercentValueFormat)
	}
	return ""
}
984022
func (a *CompleteVersion) cmp(b *CompleteVersion) int8 {
	if a.Epoch > b.Epoch {
		return 1
	}

	if a.Epoch < b.Epoch {
		return -1
	}

	if a.Version.bigger(b.Version) {
		return 1
	}

	if b.Version.bigger(a.Version) {
		return -1
	}

	if a.Pkgrel == "" || b.Pkgrel == "" {
		return 0
	}

	if a.Pkgrel.bigger(b.Pkgrel) {
		return 1
	}

	if b.Pkgrel.bigger(a.Pkgrel) {
		return -1
	}

	return 0
}
827437
func (beng BernoulliGenerator) Bernoulli_P(p float64) bool {
	if !(0.0 <= p && p <= 1.0) {
		panic(fmt.Sprintf("Invalid probability: %.2f", p))
	}
	return beng.uniform.Float64() < p
}
92047
func fmtF(dst []byte, neg bool, d decimalSlice, prec int) []byte {
	// sign
	if neg {
		dst = append(dst, '-')
	}

	// integer, padded with zeros as needed.
	if d.dp > 0 {
		var i int
		for i = 0; i < d.dp && i < d.nd; i++ {
			dst = append(dst, d.d[i])
		}
		for ; i < d.dp; i++ {
			dst = append(dst, '0')
		}
	} else {
		dst = append(dst, '0')
	}

	// fraction
	if prec > 0 {
		dst = append(dst, '.')
		for i := 0; i < prec; i++ {
			ch := byte('0')
			if j := d.dp + i; 0 <= j && j < d.nd {
				ch = d.d[j]
			}
			dst = append(dst, ch)
		}
	}

	return dst
}
1042417
func (s *InstancesDistribution) SetOnDemandPercentageAboveBaseCapacity(v int64) *InstancesDistribution {
	s.OnDemandPercentageAboveBaseCapacity = &v
	return s
}
290624
func Number(num []byte, prec int) []byte {
	// omit first + and register mantissa start and end, whether it's negative and the exponent
	neg := false
	start := 0
	dot := -1
	end := len(num)
	origExp := 0
	if 0 < end && (num[0] == '+' || num[0] == '-') {
		if num[0] == '-' {
			neg = true
		}
		start++
	}
	for i, c := range num[start:] {
		if c == '.' {
			dot = start + i
		} else if c == 'e' || c == 'E' {
			end = start + i
			i += start + 1
			if i < len(num) && num[i] == '+' {
				i++
			}
			if tmpOrigExp, n := strconv.ParseInt(num[i:]); n > 0 && tmpOrigExp >= int64(MinInt) && tmpOrigExp <= int64(MaxInt) {
				// range checks for when int is 32 bit
				origExp = int(tmpOrigExp)
			} else {
				return num
			}
			break
		}
	}
	if dot == -1 {
		dot = end
	}

	// trim leading zeros but leave at least one digit
	for start < end-1 && num[start] == '0' {
		start++
	}
	// trim trailing zeros
	i := end - 1
	for ; i > dot; i-- {
		if num[i] != '0' {
			end = i + 1
			break
		}
	}
	if i == dot {
		end = dot
		if start == end {
			num[start] = '0'
			return num[start : start+1]
		}
	} else if start == end-1 && num[start] == '0' {
		return num[start:end]
	}

	// n is the number of significant digits
	// normExp would be the exponent if it were normalised (0.1 <= f < 1)
	n := 0
	normExp := 0
	if dot == start {
		for i = dot + 1; i < end; i++ {
			if num[i] != '0' {
				n = end - i
				normExp = dot - i + 1
				break
			}
		}
	} else if dot == end {
		normExp = end - start
		for i = end - 1; i >= start; i-- {
			if num[i] != '0' {
				n = i + 1 - start
				end = i + 1
				break
			}
		}
	} else {
		n = end - start - 1
		normExp = dot - start
	}

	if origExp < 0 && (normExp < MinInt-origExp || normExp-n < MinInt-origExp) || origExp > 0 && (normExp > MaxInt-origExp || normExp-n > MaxInt-origExp) {
		return num
	}
	normExp += origExp

	// intExp would be the exponent if it were an integer
	intExp := normExp - n
	lenIntExp := 1
	if intExp <= -10 || intExp >= 10 {
		lenIntExp = strconv.LenInt(int64(intExp))
	}

	// there are three cases to consider when printing the number
	// case 1: without decimals and with an exponent (large numbers)
	// case 2: with decimals and without an exponent (around zero)
	// case 3: without decimals and with a negative exponent (small numbers)
	if normExp >= n {
		// case 1
		if dot < end {
			if dot == start {
				start = end - n
			} else {
				// TODO: copy the other part if shorter?
				copy(num[dot:], num[dot+1:end])
				end--
			}
		}
		if normExp >= n+3 {
			num[end] = 'e'
			end++
			for i := end + lenIntExp - 1; i >= end; i-- {
				num[i] = byte(intExp%10) + '0'
				intExp /= 10
			}
			end += lenIntExp
		} else if normExp == n+2 {
			num[end] = '0'
			num[end+1] = '0'
			end += 2
		} else if normExp == n+1 {
			num[end] = '0'
			end++
		}
	} else if normExp >= -lenIntExp-1 {
		// case 2
		zeroes := -normExp
		newDot := 0
		if zeroes > 0 {
			// dot placed at the front and add zeroes
			newDot = end - n - zeroes - 1
			if newDot != dot {
				d := start - newDot
				if d > 0 {
					if dot < end {
						// copy original digits behind the dot backwards
						copy(num[dot+1+d:], num[dot+1:end])
						if dot > start {
							// copy original digits before the dot backwards
							copy(num[start+d+1:], num[start:dot])
						}
					} else if dot > start {
						// copy original digits before the dot backwards
						copy(num[start+d:], num[start:dot])
					}
					newDot = start
					end += d
				} else {
					start += -d
				}
				num[newDot] = '.'
				for i := 0; i < zeroes; i++ {
					num[newDot+1+i] = '0'
				}
			}
		} else {
			// placed in the middle
			if dot == start {
				// TODO: try if placing at the end reduces copying
				// when there are zeroes after the dot
				dot = end - n - 1
				start = dot
			} else if dot >= end {
				// TODO: try if placing at the start reduces copying
				// when input has no dot in it
				dot = end
				end++
			}
			newDot = start + normExp
			if newDot > dot {
				// copy digits forwards
				copy(num[dot:], num[dot+1:newDot+1])
			} else if newDot < dot {
				// copy digits backwards
				copy(num[newDot+1:], num[newDot:dot])
			}
			num[newDot] = '.'
		}

		// apply precision
		dot = newDot
		if prec > -1 && dot+1+prec < end {
			end = dot + 1 + prec
			inc := num[end] >= '5'
			if inc || num[end-1] == '0' {
				for i := end - 1; i > start; i-- {
					if i == dot {
						end--
					} else if inc {
						if num[i] == '9' {
							if i > dot {
								end--
							} else {
								num[i] = '0'
								break
							}
						} else {
							num[i]++
							inc = false
							break
						}
					} else if i > dot && num[i] == '0' {
						end--
					} else {
						break
					}
				}
			}
			if dot == start && end == start+1 {
				if inc {
					num[start] = '1'
				} else {
					num[start] = '0'
				}
			} else {
				if dot+1 == end {
					end--
				}
				if inc {
					if num[start] == '9' {
						num[start] = '0'
						copy(num[start+1:], num[start:end])
						end++
						num[start] = '1'
					} else {
						num[start]++
					}
				}
			}
		}
	} else {
		// case 3

		// find new end, considering moving numbers to the front, removing the dot and increasing the length of the exponent
		newEnd := end
		if dot == start {
			newEnd = start + n
		} else {
			newEnd--
		}
		newEnd += 2 + lenIntExp

		exp := intExp
		lenExp := lenIntExp
		if newEnd < len(num) {
			// it saves space to convert the decimal to an integer and decrease the exponent
			if dot < end {
				if dot == start {
					copy(num[start:], num[end-n:end])
					end = start + n
				} else {
					copy(num[dot:], num[dot+1:end])
					end--
				}
			}
		} else {
			// it does not save space and will panic, so we revert to the original representation
			exp = origExp
			lenExp = 1
			if origExp <= -10 || origExp >= 10 {
				lenExp = strconv.LenInt(int64(origExp))
			}
		}
		num[end] = 'e'
		num[end+1] = '-'
		end += 2
		exp = -exp
		for i := end + lenExp - 1; i >= end; i-- {
			num[i] = byte(exp%10) + '0'
			exp /= 10
		}
		end += lenExp
	}

	if neg {
		start--
		num[start] = '-'
	}
	return num[start:end]
}
1325886
func (o *Buffer) dec_ref_int32(p *Properties, base structPointer) error {
	u, err := p.valDec(o)
	if err != nil {
		return err
	}
	refWord32_Set(structPointer_RefWord32(base, p.field), o, uint32(u))
	return nil
}