add scalar multiplication and division

This commit is contained in:
NunoSempere 2024-05-11 10:27:05 +01:00
parent 355ad371a7
commit 8142a19c4b

39
f.go
View File

@ -64,51 +64,61 @@ func main() {
error_msg_cont := "Please enter two floats separated by a space, like: 1 10, or two floats preceded by a division, like: / 2 20. Or enter i to get the logmean and logstd" error_msg_cont := "Please enter two floats separated by a space, like: 1 10, or two floats preceded by a division, like: / 2 20. Or enter i to get the logmean and logstd"
EventForLoop: EventForLoop:
for { for {
// fmt.Println("Enter another two floats separated by a space:") var new_low, new_high float64
var err1, err2 error
input, _ = reader.ReadString('\n') input, _ = reader.ReadString('\n')
if strings.TrimSpace(input) == "" { if strings.TrimSpace(input) == "" {
continue EventForLoop continue EventForLoop
} }
tokens := strings.Split(strings.TrimSpace(input), " ") tokens := strings.Split(strings.TrimSpace(input), " ")
var err1, err2 error
var new_low, new_high float64
switch len(tokens) { switch len(tokens) {
case 0: case 0:
continue EventForLoop continue EventForLoop
case 1: case 1:
if tokens[0] == "i" { single_float, err1 := strconv.ParseFloat(tokens[0], 64)
switch {
case tokens[0] == "i":
fmt.Printf("=> %.1f %.1f\n", old_low, old_high) fmt.Printf("=> %.1f %.1f\n", old_low, old_high)
logmean_old, logstd_old := boundsToLogParams(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) fmt.Printf("=> Lognormal, with logmean: %.1f, logstd: %.1f\n", logmean_old, logstd_old)
continue EventForLoop continue EventForLoop
} case err1 != nil:
new_low = single_float
new_high = single_float
default:
fmt.Println(error_msg_cont) fmt.Println(error_msg_cont)
continue EventForLoop continue EventForLoop
}
case 2: case 2:
new_low, err1 = strconv.ParseFloat(tokens[0], 64) new_low, err1 = strconv.ParseFloat(tokens[0], 64)
new_high, err2 = strconv.ParseFloat(tokens[1], 64) new_high, err2 = strconv.ParseFloat(tokens[1], 64)
if err1 != nil || err2 != nil { if err2 != nil {
fmt.Println(error_msg_cont) fmt.Println(error_msg_cont)
continue EventForLoop continue EventForLoop
} }
case 3:
switch tokens[0] { switch tokens[0] {
case "*": case "*":
new_low, err1 = strconv.ParseFloat(tokens[1], 64) new_low = new_high
new_high, err2 = strconv.ParseFloat(tokens[2], 64)
if err1 != nil || err2 != nil {
fmt.Println(error_msg_cont)
continue EventForLoop
}
case "/": case "/":
new_low = 1.0 / new_high
new_high = new_low
default:
if err1 != nil {
fmt.Println(error_msg_cont)
continue EventForLoop
}
}
case 3:
new_low, err1 = strconv.ParseFloat(tokens[1], 64) new_low, err1 = strconv.ParseFloat(tokens[1], 64)
new_high, err2 = strconv.ParseFloat(tokens[2], 64) new_high, err2 = strconv.ParseFloat(tokens[2], 64)
if err1 != nil || err2 != nil { if err1 != nil || err2 != nil {
fmt.Println(error_msg_cont) fmt.Println(error_msg_cont)
continue EventForLoop continue EventForLoop
} }
switch tokens[0] {
case "*":
case "/":
tmp_low := new_low tmp_low := new_low
new_low = 1.0 / new_high new_low = 1.0 / new_high
new_high = 1.0 / tmp_low new_high = 1.0 / tmp_low
@ -120,6 +130,7 @@ EventForLoop:
fmt.Println(error_msg_cont) fmt.Println(error_msg_cont)
continue EventForLoop continue EventForLoop
} }
// Use the abstracted function for combining floats // Use the abstracted function for combining floats
old_low, old_high = combineBounds(old_low, old_high, new_low, new_high) 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", old_low, old_high)