tweak parsing code.

This commit is contained in:
NunoSempere 2024-11-20 11:19:10 +00:00
parent 4b21612d55
commit 6c98249e57
2 changed files with 13 additions and 15 deletions

View File

@ -186,6 +186,8 @@ func prettyPrintStats(dist Dist) {
}) })
print_ci := func(ci float64, prefix string) { print_ci := func(ci float64, prefix string) {
x := sorted_xs[int(math.Round(float64(n)*ci))] x := sorted_xs[int(math.Round(float64(n)*ci))]
fmt.Printf("%s", prefix)
// pretty.PrettyPrintFloat(x)
fmt.Printf("%s%f\n", prefix, x) fmt.Printf("%s%f\n", prefix, x)
} }
print_ci(0.01, "ci 1%: ") print_ci(0.01, "ci 1%: ")

View File

@ -52,14 +52,6 @@ func PrettyPrint2Floats(low float64, high float64) {
PrettyPrintFloat(high) PrettyPrintFloat(high)
} }
func multiplyOrPassThroughError(a float64, b float64, err error) (float64, error) {
if err != nil {
return b, err
} else {
return a * b, nil
}
}
func ParseFloat(word string) (float64, error) { func ParseFloat(word string) (float64, error) {
// l = len(word) // assuming no UTF stuff // l = len(word) // assuming no UTF stuff
switch len(word) { switch len(word) {
@ -71,19 +63,23 @@ func ParseFloat(word string) (float64, error) {
n := len(word) - 1 n := len(word) - 1
f, err := strconv.ParseFloat(word[:n], 64) f, err := strconv.ParseFloat(word[:n], 64)
if err != nil {
return 0, err
}
multiplier := 1.0
switch word[n] { switch word[n] {
case '%': case '%':
return multiplyOrPassThroughError(0.01, f, err) multiplier = 0.01
case 'K': case 'K':
return multiplyOrPassThroughError(1_000, f, err) multiplier = 1_000
case 'M': case 'M':
return multiplyOrPassThroughError(1_000_000, f, err) multiplier = 1_000_000
case 'B': case 'B':
return multiplyOrPassThroughError(1_000_000_000, f, err) multiplier = 1_000_000
case 'T': case 'T':
return multiplyOrPassThroughError(1_000_000_000_000, f, err) multiplier = 1_000_000
default: default:
return strconv.ParseFloat(word, 64) multiplier = 1.0
} }
return f * multiplier, nil
} }