fermi/main.go

91 lines
2.5 KiB
Go
Raw Normal View History

2024-05-10 18:05:03 +00:00
package main
import (
"bufio"
"fmt"
2024-05-10 18:25:23 +00:00
"math"
2024-05-10 18:05:03 +00:00
"os"
"strconv"
"strings"
)
2024-05-10 18:25:23 +00:00
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)
2024-05-10 18:07:58 +00:00
}
2024-05-10 18:05:03 +00:00
func main() {
reader := bufio.NewReader(os.Stdin)
2024-05-10 18:25:23 +00:00
var old_low, old_high float64 // Variables to store the intermediate results
2024-05-10 18:05:03 +00:00
2024-05-10 18:25:23 +00:00
// fmt.Println("Enter two floats separated by a space:")
2024-05-10 18:05:03 +00:00
input, _ := reader.ReadString('\n')
input = strings.TrimSpace(input)
2024-05-10 18:07:36 +00:00
tokens := strings.Split(input, " ")
if len(tokens) != 2 {
fmt.Println("Please enter exactly two floats.")
return
}
2024-05-10 18:25:23 +00:00
old_low, err1 := strconv.ParseFloat(tokens[0], 64)
old_high, err2 := strconv.ParseFloat(tokens[1], 64)
2024-05-10 18:07:36 +00:00
if err1 != nil || err2 != nil {
fmt.Println("Invalid input. Please ensure you enter two floats.")
return
}
2024-05-10 18:25:23 +00:00
fmt.Printf("=> %.1f %.1f\n", old_low, old_high)
2024-05-10 18:05:03 +00:00
for {
2024-05-10 18:25:23 +00:00
// fmt.Println("Enter another two floats separated by a space:")
2024-05-10 18:05:03 +00:00
input, _ = reader.ReadString('\n')
2024-05-10 18:07:58 +00:00
if strings.TrimSpace(input) == "" {
2024-05-10 18:05:03 +00:00
break // Exit if no input is given
}
2024-05-10 18:07:58 +00:00
tokens := strings.Split(strings.TrimSpace(input), " ")
2024-05-10 18:05:03 +00:00
if len(tokens) != 2 {
fmt.Println("Please enter exactly two floats.")
continue
}
2024-05-10 18:25:23 +00:00
new_low, err1 := strconv.ParseFloat(tokens[0], 64)
new_high, err2 := strconv.ParseFloat(tokens[1], 64)
2024-05-10 18:05:03 +00:00
if err1 != nil || err2 != nil {
fmt.Println("Invalid input. Please ensure you enter two floats.")
continue
}
2024-05-10 18:07:58 +00:00
// Use the abstracted function for combining floats
2024-05-10 18:25:23 +00:00
old_low, old_high = combineBounds(old_low, old_high, new_low, new_high)
fmt.Printf("=> %.1f %.1f\n", old_low, old_high)
2024-05-10 18:05:03 +00:00
}
}