Compare commits

..

10 Commits

Author SHA1 Message Date
69b3681697 fix distributional division 2024-05-12 18:10:25 +01:00
bc40428f8a tweak error msg 2024-05-11 12:24:45 +01:00
4204db0c32 propagate scalar ops, tweaks 2024-05-11 12:22:29 +01:00
b1a4f2bd3c tokens => words 2024-05-11 11:08:24 +01:00
073728e750 rationalize parser 2024-05-11 11:08:03 +01:00
8142a19c4b add scalar multiplication and division 2024-05-11 10:27:05 +01:00
355ad371a7 savepoint 2024-05-10 22:47:34 +01:00
290be77204 update examples &c 2024-05-10 22:38:20 +01:00
72be1167bd tweak error msgs 2024-05-10 20:53:30 +01:00
1f6b61cee0 add division 2024-05-10 20:50:55 +01:00
7 changed files with 230 additions and 64 deletions

102
README.md
View File

@ -1,33 +1,101 @@
# 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
=> 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 # 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
```bash
f
sed -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
function f(){
sed -u "s|#.*||" |
sed -u "s|//.*||" |
sed -u 's|K|000|g' |
sed -u 's|M|000000|g' |
sed -u 's|B|000000000|g' |
/usr/bin/f
}
```
## Tips & tricks
Conceptually clearer to have all the multiplications first and then all the divisions
## 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
- [x] Scalar multiplication and division
- [ ] 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
- [ ] Think how to integrate with squiggle.c to draw samples
- [ ] Think about how to draw a histogram from samples

150
f.go
View File

@ -42,46 +42,136 @@ 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
var input string
var err1, err2 error
for {
input, _ = reader.ReadString('\n')
input = strings.TrimSpace(input)
words := strings.Split(input, " ")
if len(words) != 2 {
fmt.Println("Please enter two floats separated by a space, like: 1 10")
continue
}
old_low, err1 = strconv.ParseFloat(words[0], 64)
old_high, err2 = strconv.ParseFloat(words[1], 64)
if err1 != nil || err2 != nil {
fmt.Println("Please enter two floats separated by a space, like: 1 10")
continue
}
break
input, _ := reader.ReadString('\n')
input = strings.TrimSpace(input)
tokens := strings.Split(input, " ")
if len(tokens) != 2 {
fmt.Println("Please enter exactly two floats.")
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)
error_msg_cont := "Valid inputs: 2 || * 2 || / 2 || 2 20 || * 2 20 || / 2 20 || i || e"
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
}
words := strings.Split(strings.TrimSpace(input), " ")
var new_low, new_high float64
switch words[0] {
case "*":
switch len(words) {
case 1:
fmt.Println("Can't multiply by nothing")
fmt.Println(error_msg_cont)
continue EventForLoop
case 2:
single_float, err1 := strconv.ParseFloat(words[1], 64)
if err1 != nil {
fmt.Println("Trying to multiply by a scalar, but scalar is not a float")
fmt.Println(error_msg_cont)
continue EventForLoop
}
new_low = single_float
new_high = single_float
case 3:
new_low, err1 = strconv.ParseFloat(words[1], 64)
new_high, err2 = strconv.ParseFloat(words[2], 64)
if err1 != nil || err2 != nil {
fmt.Println(error_msg_cont)
fmt.Println("Trying to multiply by a distribution, but distribution is not specified as two floats")
continue EventForLoop
}
default:
fmt.Println("Trying to multiply by something, but this something is neither a scalar nor a distribution")
fmt.Println(error_msg_cont)
continue EventForLoop
}
case "/":
switch len(words) {
case 1:
fmt.Println("Can't divide by nothing")
fmt.Println(error_msg_cont)
continue EventForLoop
case 2:
single_float, err1 := strconv.ParseFloat(words[1], 64)
if err1 != nil {
fmt.Println("Trying to divide by a scalar, but scalar is not a float")
fmt.Println(error_msg_cont)
continue EventForLoop
}
new_low = 1.0 / single_float
new_high = 1.0 / single_float
case 3:
new_low, err1 = strconv.ParseFloat(words[1], 64)
new_high, err2 = strconv.ParseFloat(words[2], 64)
if err1 != nil || err2 != nil {
fmt.Println("Trying to divide by a distribution, but distribution is not specified as two floats")
fmt.Println(error_msg_cont)
continue EventForLoop
}
tmp := new_low
new_low = 1.0 / new_high
new_high = 1.0 / tmp
default:
fmt.Println("Trying to divide by something, but this something is neither a scalar nor a distribution")
}
default:
switch len(words) {
case 0:
continue EventForLoop
case 1:
switch words[0] {
case "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 "e":
break EventForLoop
default:
single_float, err1 := strconv.ParseFloat(words[0], 64)
if err1 != nil {
fmt.Println("Unrecognized command")
fmt.Println(error_msg_cont)
continue EventForLoop
}
new_low = single_float
new_high = single_float
}
case 2:
new_low, err1 = strconv.ParseFloat(words[0], 64)
new_high, err2 = strconv.ParseFloat(words[1], 64)
if err1 != nil || err2 != nil {
fmt.Println("Trying to multiply by a distribution, but distribution is not specified as two floats")
fmt.Println(error_msg_cont)
continue EventForLoop
}
default:
fmt.Println("No operation takes more than 3 words")
fmt.Println(error_msg_cont)
continue EventForLoop
}
}
tokens := strings.Split(strings.TrimSpace(input), " ")
if len(tokens) != 2 {
fmt.Println("Please enter exactly two floats.")
continue
}
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
}
// 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)
}

View File

@ -1,9 +1,12 @@
#!/bin/bash
#while IFS= read -r line; do echo "$line" | sed 's| //.*||' | go run main.go
# while IFS= read -r line; do echo "$line" | sed 's|//.*||'; done | go run main.go
function f(){
sed -u "s|#.*||" |
sed -u "s|//.*||" |
sed -u 's|K|000|g' |
sed -u 's|M|000000|g' |
sed -u 's|B|000000000|g' |
/usr/bin/f
}
sed -u 's|//.*||' | go run main.go
# ^ sed unbuffered.
# cat | tee -a log.f | sed -u 's|//.*||' | go run main.go | tee -a log.f
# f

View File

@ -1,2 +0,0 @@
1 10
2 20

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 # 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

View File

@ -1,9 +0,0 @@
Please enter exactly two floats.
4000000 12000000
=> 4000000.0 12000000.0
0.005 0.01
=> 25587.7 93795.1
1/365 1/365
Invalid input. Please ensure you enter two floats.
1 1
=> 25587.7 93795.1