package main import ( "bufio" "fmt" "math" "os" "strconv" "strings" ) const NORMAL90CONFIDENCE = 1.6448536269514727 func boundsToLogParams(low float64, high float64) (float64, float64) { loglow := math.Log(low) loghigh := math.Log(high) logmean := (loghigh + loglow) / 2.0 logstd := (loghigh - loglow) / (2.0 * NORMAL90CONFIDENCE) return logmean, logstd } func multiplyLognormals(logmean1 float64, logstd1 float64, logmean2 float64, logstd2 float64) (float64, float64) { return logmean1 + logmean2, math.Sqrt(logstd1*logstd1 + logstd2*logstd2) } func logParamsToBounds(logmean float64, logstd float64) (float64, float64) { h := logstd * NORMAL90CONFIDENCE loglow := logmean - h loghigh := logmean + h return math.Exp(loglow), math.Exp(loghigh) } func combineBounds(old_low, old_high, new_low, new_high float64) (float64, float64) { logmean_old, logstd_old := boundsToLogParams(old_low, old_high) logmean_new, logstd_new := boundsToLogParams(new_low, new_high) logmean_product, logstd_product := multiplyLognormals(logmean_old, logstd_old, logmean_new, logstd_new) return logParamsToBounds(logmean_product, logstd_product) } func prettyPrintDist(low float64, high float64) { // fmt.Printf("=> %.1f %.1f\n", low, high) fmt.Printf("=> ") switch { case math.Abs(low) >= 1_000_000_000_000: fmt.Printf("%.1fT", low/1_000_000_000_000) case math.Abs(low) >= 1_000_000_000: fmt.Printf("%.1fB", low/1_000_000_000) case math.Abs(low) >= 1_000_000: fmt.Printf("%.1fM", low/1_000_000) case math.Abs(low) >= 1_000: fmt.Printf("%.1fK", low/1_000) case math.Abs(low) >= 1_000: fmt.Printf("%.1fK", low/1_000) default: fmt.Printf("%.1f", low) } fmt.Printf(" ") switch { case math.Abs(high) >= 1_000_000_000_000: fmt.Printf("%.1fT", high/1_000_000_000_000) case math.Abs(high) >= 1_000_000_000: fmt.Printf("%.1fB", high/1_000_000_000) case math.Abs(high) >= 1_000_000: fmt.Printf("%.1fM", high/1_000_000) case math.Abs(high) >= 1_000: fmt.Printf("%.1fK", high/1_000) case math.Abs(high) >= 1_000: fmt.Printf("%.1fK", high/1_000) default: fmt.Printf("%.1f", high) } fmt.Printf("\n") // fmt.Printf("=> %.1f %.1f\n", low, high) } func main() { reader := bufio.NewReader(os.Stdin) var old_low, old_high float64 var input string var err1, err2 error InitialForLoop: for { input, _ = reader.ReadString('\n') input = strings.TrimSpace(input) words := strings.Split(input, " ") switch len(words) { case 1: single_float, err1 := strconv.ParseFloat(words[0], 64) if err1 != nil { fmt.Println("Trying to initialize with a scalar, but scalar is not a float") continue InitialForLoop } old_low = single_float old_high = single_float case 2: old_low, err1 = strconv.ParseFloat(words[0], 64) old_high, err2 = strconv.ParseFloat(words[1], 64) if err1 != nil || err2 != nil { fmt.Println("Trying to initialize with a distribution, but distribution is not specified as two floats") continue InitialForLoop } default: fmt.Println("Please enter two floats separated by a space, like: 1 10") continue InitialForLoop } if err1 != nil || err2 != nil { fmt.Println("Please enter two floats separated by a space, like: 1 10") continue } break } prettyPrintDist(old_low, old_high) error_msg_cont := "Valid inputs: 2 || * 2 || / 2 || 2 20 || * 2 20 || / 2 20 || i || e" EventForLoop: for { input, _ = reader.ReadString('\n') if strings.TrimSpace(input) == "" { continue EventForLoop } words := strings.Split(strings.TrimSpace(input), " ") var new_low, new_high float64 switch words[0] { case "*": switch len(words) { case 1: fmt.Println("Can't multiply by nothing") fmt.Println(error_msg_cont) continue EventForLoop case 2: single_float, err1 := strconv.ParseFloat(words[1], 64) if err1 != nil { fmt.Println("Trying to multiply by a scalar, but scalar is not a float") fmt.Println(error_msg_cont) continue EventForLoop } new_low = single_float new_high = single_float case 3: new_low, err1 = strconv.ParseFloat(words[1], 64) new_high, err2 = strconv.ParseFloat(words[2], 64) if err1 != nil || err2 != nil { fmt.Println(error_msg_cont) fmt.Println("Trying to multiply by a distribution, but distribution is not specified as two floats") continue EventForLoop } default: fmt.Println("Trying to multiply by something, but this something is neither a scalar nor a distribution") fmt.Println(error_msg_cont) continue EventForLoop } case "/": switch len(words) { case 1: fmt.Println("Can't divide by nothing") fmt.Println(error_msg_cont) continue EventForLoop case 2: single_float, err1 := strconv.ParseFloat(words[1], 64) if err1 != nil { fmt.Println("Trying to divide by a scalar, but scalar is not a float") fmt.Println(error_msg_cont) continue EventForLoop } new_low = 1.0 / single_float new_high = 1.0 / single_float case 3: new_low, err1 = strconv.ParseFloat(words[1], 64) new_high, err2 = strconv.ParseFloat(words[2], 64) if err1 != nil || err2 != nil { fmt.Println("Trying to divide by a distribution, but distribution is not specified as two floats") fmt.Println(error_msg_cont) continue EventForLoop } tmp := new_low new_low = 1.0 / new_high new_high = 1.0 / tmp default: fmt.Println("Trying to divide by something, but this something is neither a scalar nor a distribution") } default: switch len(words) { case 0: continue EventForLoop case 1: switch words[0] { case "i": fmt.Printf("=> %.1f %.1f\n", old_low, old_high) logmean_old, logstd_old := boundsToLogParams(old_low, old_high) fmt.Printf("=> Lognormal, with logmean: %.1f, logstd: %.1f\n", logmean_old, logstd_old) continue EventForLoop case "e": break EventForLoop default: single_float, err1 := strconv.ParseFloat(words[0], 64) if err1 != nil { fmt.Println("Unrecognized command") fmt.Println(error_msg_cont) continue EventForLoop } new_low = single_float new_high = single_float } case 2: new_low, err1 = strconv.ParseFloat(words[0], 64) new_high, err2 = strconv.ParseFloat(words[1], 64) if err1 != nil || err2 != nil { fmt.Println("Trying to multiply by a distribution, but distribution is not specified as two floats") fmt.Println(error_msg_cont) continue EventForLoop } default: fmt.Println("No operation takes more than 3 words") fmt.Println(error_msg_cont) continue EventForLoop } } old_low, old_high = combineBounds(old_low, old_high, new_low, new_high) prettyPrintDist(old_low, old_high) } }