Add 'runk' calculator function

This commit is contained in:
Akemi Izuko 2022-10-10 20:09:07 -06:00
parent 8f06577f87
commit 206d940e79
No known key found for this signature in database
GPG key ID: 905D444F6A5E4BE4

View file

@ -57,6 +57,38 @@ viw() {
fi
}
# Ronald's Universal Number Kruncher, standardized syntax for unix calculators
# Syntax features:
# ^ and ** both work for exponentiation
# log() is base 10
# ln() is base e
# log2() is base 2
# Careful when changing order or sed might sed itself!
runk() {
local calc_ver="$(echo "$@" | sed \
-e 's#log2(\([^)]\+\))#(log(\1)/log(2))#g' \
)"
local py_ver="$(echo "$@" | sed \
-e 's#\^#**#g' \
-e 's#\(log([^)]\+)\)#(\1/log(10))#g' \
-e 's#ln(\([^)]\+\))#log(\1)#g' \
)"
local bc_ver="$(echo "$@" | sed \
-e 's#\*\*#^#g' \
-e 's#ln(\([^)]\+\))#l(\1)#g' \
-e 's#log(\([^)]\+\))#(l(\1)/l(10))#g' \
-e 's#log2(\([^)]\+\))#(l(\1)/l(2))#g' \
)"
if command -v calc &>/dev/null; then
calc "$calc_ver"
elif command -v python3 &>/dev/null; then
python3 -c "from math import *; print($py_ver)"
else
echo "$bc_ver" | bc --mathlib
fi
}
# Better exiting ====================================================
# Don't exit with background jobs still running. Particularly for Qemu
function exit () {