update examples &c

This commit is contained in:
NunoSempere 2024-05-10 22:38:20 +01:00
parent 72be1167bd
commit 290be77204
4 changed files with 108 additions and 32 deletions

View File

@ -1,33 +1,83 @@
# A minimalist calculator for fermi estimation
This project contains a minimalist calculator for Fermi estimation. Right now, it just multiplies lognormals.
This project contains a minimalist command-line calculator for Fermi estimation. For now, it just multiplies lognormals.
## Motivation
Sometimes, [Squiggle](https://github.com/quantified-uncertainty/squiggle), [simple squiggle](https://git.nunosempere.com/quantified.uncertainty/simple-squiggle) or [squiggle.c](https://git.nunosempere.com/personal/squiggle.c) are still too complicated and un-unix-like.
## An example
## Usage
Here is an example
```
$ go run f.go
5000000 12000000
=> 5000000.0 12000000.0
0.002 0.01
=> 13859.5 86583.4
30 180
=> 706832.8 9167656.0
/ 48 52
=> 14139.1 183614.7
/ 5 6
=> 2573.1 33632.0
/ 6 8
=> 368.4 4893.5
/ 60 60
=> 6.1 81.6
```
Perhaps this example might be clearer with comment
Perhaps this example is more understandable with comments and better units:
```
$ sed -u "s|#.*||" | sed -u 's|M|000000|g' | go run f.go
5M 12M # number of people living in Chicago
=> 5000000.0 12000000.0
0.002 0.01 # fraction of people that have a piano
=> 13859.5 86583.4
30 180 # minutes it takes to tune a piano, including travel time
=> 706832.8 9167656.0
/ 48 52 # weeks a year that piano tuners work for
=> 14139.1 183614.7
/ 5 6 # days a week in which piano tuners work
=> 2573.1 33632.0
/ 6 8 # hours a day in which piano tuners work
=> 368.4 4893.5
/ 60 60 # minutes to an hour
=> 6.1 81.6
# ^ piano tuners in Chicago
```
##
## Installation
To do:
```
make build
sudo make install
f # rather than the previous go run f.go
```
- [ ] Write README
- [ ] Add show more info version
- [ ] Add division?
- [ ] Read from file?
- [ ] Save to file?
Why use make instead of the built-in go commands? Because the point of make is to be able to share command-line recipes.
## Usage together with standard Linux utilities
```
f
ed -u "s|#.*||" | sed -u 's|M|000000|g' | f
cat more/piano-tuners.f | f
cat more/piano-tuners-commented.f | sed -u "s|#.*||" | sed -u 's|M|000000|g' | f
tee -a input.log | go run f.go | tee -a output.log
tee -a io.log | go run f.go | tee -a io.log
```
## Roadmap
- [x] Write README
- [x] Add division?
- [x] Read from file?
- [x] Save to file?
- [x] Allow comments?
- [x] Use a sed filter?
- [x] Add show more info version
- [ ] Program into a small device, like a calculator?
- [ ] Allow comments?
- Use a sed filter?
- [ ] Think of some way of calling bc
go run main.go < model.f
sed 's|//||' model.f | go run main.go

40
f.go
View File

@ -41,31 +41,33 @@ func combineBounds(old_low, old_high, new_low, new_high float64) (float64, float
}
func main() {
reader := bufio.NewReader(os.Stdin)
var old_low, old_high float64
reader := bufio.NewReader(os.Stdin)
input, _ := reader.ReadString('\n')
input = strings.TrimSpace(input)
tokens := strings.Split(input, " ")
error_msg_start := "Please enter two floats separated by a space, like: 1 10"
if len(tokens) != 2 {
fmt.Println("Please enter two floats separated by a space")
fmt.Println(error_msg_start)
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.")
fmt.Println(error_msg_start)
return
}
fmt.Printf("=> %.1f %.1f\n", old_low, old_high)
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:
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
continue EventForLoop
}
tokens := strings.Split(strings.TrimSpace(input), " ")
@ -74,15 +76,20 @@ func main() {
var new_low, new_high float64
switch len(tokens) {
case 0:
continue
continue EventForLoop
case 1:
continue
if tokens[0] == "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 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
fmt.Println(error_msg_cont)
continue EventForLoop
}
case 3:
switch tokens[0] {
@ -90,23 +97,26 @@ func main() {
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
fmt.Println(error_msg_cont)
continue EventForLoop
}
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
fmt.Println(error_msg_cont)
continue EventForLoop
}
tmp_low := new_low
new_low = 1.0 / new_high
new_high = 1.0 / tmp_low
default:
continue
fmt.Println(error_msg_cont)
continue EventForLoop
}
default:
fmt.Println(error_msg_cont)
continue EventForLoop
}
// Use the abstracted function for combining floats
old_low, old_high = combineBounds(old_low, old_high, new_low, new_high)

View File

@ -0,0 +1,9 @@
5M 12M # number of people living in Chicago
0.002 0.01 # fraction of people that have a piano
30 180 # minutes it takes to tune a piano, including travel time
/ 48 52 # weeks a year that piano tuners work for
/ 5 6 # days a week in which piano tuners work
/ 6 8 # hours a day in which piano tuners work
/ 60 60 # minutes to an hour
# ^ piano tuners in Chicago

7
more/piano-tuners.f Normal file
View File

@ -0,0 +1,7 @@
5000000 12000000
0.002 0.01
30 180
/ 48 52
/ 5 6
/ 6 8
/ 60 60