From 6c98249e57b852663591ada4efa5a78ba6b5f230 Mon Sep 17 00:00:00 2001 From: NunoSempere Date: Wed, 20 Nov 2024 11:19:10 +0000 Subject: [PATCH] tweak parsing code. --- fermi.go | 2 ++ pretty/pretty.go | 26 +++++++++++--------------- 2 files changed, 13 insertions(+), 15 deletions(-) diff --git a/fermi.go b/fermi.go index 8a4e4fc..64f98ab 100644 --- a/fermi.go +++ b/fermi.go @@ -186,6 +186,8 @@ func prettyPrintStats(dist Dist) { }) print_ci := func(ci float64, prefix string) { x := sorted_xs[int(math.Round(float64(n)*ci))] + fmt.Printf("%s", prefix) + // pretty.PrettyPrintFloat(x) fmt.Printf("%s%f\n", prefix, x) } print_ci(0.01, "ci 1%: ") diff --git a/pretty/pretty.go b/pretty/pretty.go index 3e6eb79..d092ad7 100644 --- a/pretty/pretty.go +++ b/pretty/pretty.go @@ -52,14 +52,6 @@ func PrettyPrint2Floats(low float64, high float64) { 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) { // l = len(word) // assuming no UTF stuff switch len(word) { @@ -71,19 +63,23 @@ func ParseFloat(word string) (float64, error) { n := len(word) - 1 f, err := strconv.ParseFloat(word[:n], 64) + if err != nil { + return 0, err + } + multiplier := 1.0 switch word[n] { case '%': - return multiplyOrPassThroughError(0.01, f, err) + multiplier = 0.01 case 'K': - return multiplyOrPassThroughError(1_000, f, err) + multiplier = 1_000 case 'M': - return multiplyOrPassThroughError(1_000_000, f, err) + multiplier = 1_000_000 case 'B': - return multiplyOrPassThroughError(1_000_000_000, f, err) + multiplier = 1_000_000 case 'T': - return multiplyOrPassThroughError(1_000_000_000_000, f, err) + multiplier = 1_000_000 default: - return strconv.ParseFloat(word, 64) + multiplier = 1.0 } - + return f * multiplier, nil }