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 main() { reader := bufio.NewReader(os.Stdin) var old_low, old_high float64 input, _ := reader.ReadString('\n') input = strings.TrimSpace(input) tokens := strings.Split(input, " ") if len(tokens) != 2 { fmt.Println("Please enter two floats separated by a space") return } old_low, err1 := strconv.ParseFloat(tokens[0], 64) old_high, err2 := strconv.ParseFloat(tokens[1], 64) if err1 != nil || err2 != nil { fmt.Println("Invalid input. Please ensure you enter two floats.") return } fmt.Printf("=> %.1f %.1f\n", old_low, old_high) for { // fmt.Println("Enter another two floats separated by a space:") input, _ = reader.ReadString('\n') if strings.TrimSpace(input) == "" { break // Exit if no input is given } tokens := strings.Split(strings.TrimSpace(input), " ") var err1, err2 error var new_low, new_high float64 switch len(tokens) { case 0: continue case 1: continue case 2: new_low, err1 = strconv.ParseFloat(tokens[0], 64) new_high, err2 = strconv.ParseFloat(tokens[1], 64) if err1 != nil || err2 != nil { fmt.Println("Invalid input. Please ensure you enter two floats.") continue } case 3: switch tokens[0] { case "*": new_low, err1 = strconv.ParseFloat(tokens[1], 64) new_high, err2 = strconv.ParseFloat(tokens[2], 64) if err1 != nil || err2 != nil { fmt.Println("Invalid input. Please ensure you enter two floats.") continue } case "/": new_low, err1 = strconv.ParseFloat(tokens[1], 64) new_high, err2 = strconv.ParseFloat(tokens[2], 64) if err1 != nil || err2 != nil { fmt.Println("Invalid input. Please ensure you enter two floats.") continue } tmp_low := new_low new_low = 1.0 / new_high new_high = 1.0 / tmp_low default: continue } } // Use the abstracted function for combining floats old_low, old_high = combineBounds(old_low, old_high, new_low, new_high) fmt.Printf("=> %.1f %.1f\n", old_low, old_high) } }