reach initial mvp

This commit is contained in:
NunoSempere 2024-05-10 19:25:23 +01:00
parent becd5cd741
commit e0c570107b

56
main.go
View File

@ -3,22 +3,49 @@ package main
import ( import (
"bufio" "bufio"
"fmt" "fmt"
"math"
"os" "os"
"strconv" "strconv"
"strings" "strings"
) )
// combineFloats takes previous stored floats and new floats, applies an operation, and returns the combined results. const NORMAL90CONFIDENCE = 1.6448536269514727
func combineFloats(stored1, stored2, new1, new2 float64) (float64, float64) {
// Example operation: simply adding the new floats to the stored ones. func boundsToLogParams(low float64, high float64) (float64, float64) {
return stored1 + new1, stored2 + new2
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() { func main() {
reader := bufio.NewReader(os.Stdin) reader := bufio.NewReader(os.Stdin)
var stored1, stored2 float64 // Variables to store the intermediate results var old_low, old_high float64 // Variables to store the intermediate results
fmt.Println("Enter two floats separated by a space:") // fmt.Println("Enter two floats separated by a space:")
input, _ := reader.ReadString('\n') input, _ := reader.ReadString('\n')
input = strings.TrimSpace(input) input = strings.TrimSpace(input)
@ -27,17 +54,17 @@ func main() {
fmt.Println("Please enter exactly two floats.") fmt.Println("Please enter exactly two floats.")
return return
} }
stored1, err1 := strconv.ParseFloat(tokens[0], 64) old_low, err1 := strconv.ParseFloat(tokens[0], 64)
stored2, err2 := strconv.ParseFloat(tokens[1], 64) old_high, err2 := strconv.ParseFloat(tokens[1], 64)
if err1 != nil || err2 != nil { if err1 != nil || err2 != nil {
fmt.Println("Invalid input. Please ensure you enter two floats.") fmt.Println("Invalid input. Please ensure you enter two floats.")
return return
} }
fmt.Printf("=> %.1f %.1f\n", stored1, stored2) fmt.Printf("=> %.1f %.1f\n", old_low, old_high)
for { for {
fmt.Println("Enter another two floats separated by a space:") // fmt.Println("Enter another two floats separated by a space:")
input, _ = reader.ReadString('\n') input, _ = reader.ReadString('\n')
if strings.TrimSpace(input) == "" { if strings.TrimSpace(input) == "" {
break // Exit if no input is given break // Exit if no input is given
@ -49,16 +76,15 @@ func main() {
continue continue
} }
new1, err1 := strconv.ParseFloat(tokens[0], 64) new_low, err1 := strconv.ParseFloat(tokens[0], 64)
new2, err2 := strconv.ParseFloat(tokens[1], 64) new_high, err2 := strconv.ParseFloat(tokens[1], 64)
if err1 != nil || err2 != nil { if err1 != nil || err2 != nil {
fmt.Println("Invalid input. Please ensure you enter two floats.") fmt.Println("Invalid input. Please ensure you enter two floats.")
continue continue
} }
// Use the abstracted function for combining floats // Use the abstracted function for combining floats
stored1, stored2 = combineFloats(stored1, stored2, new1, new2) old_low, old_high = combineBounds(old_low, old_high, new_low, new_high)
fmt.Printf("=> %.1f %.1f\n", old_low, old_high)
fmt.Printf("=> %.1f %.1f\n", stored1, stored2)
} }
} }