Away:Module-math: Difference between revisions
From The Commonwealth of Acropolis
More actions
No edit summary |
No edit summary |
||
| Line 1: | Line 1: | ||
#!/usr/bin/env bash | #!/usr/bin/env bash | ||
code=("$@") | code=("$@") | ||
bc_mathlib() { | |||
printf "%s (%s)\n" $1 "${code[2]}" | bc -l | printf "%s (%s)\n" "${1}" "${code[2]}" | bc -l | ||
} | |||
bc_biterm() { | |||
printf "%s %s %s\n" "${code[2]}" "${1}" "${code[3]}" | bc | |||
} | } | ||
case $1 in | case $1 in | ||
| Line 9: | Line 12: | ||
;; | ;; | ||
sin) | sin) | ||
bc_mathlib s | |||
;; | ;; | ||
cos) | cos) | ||
bc_mathlib c | |||
;; | ;; | ||
atan) | atan) | ||
bc_mathlib a | |||
;; | ;; | ||
ln) | ln) | ||
bc_mathlib l | |||
;; | ;; | ||
epow) | epow) | ||
bc_mathlib e | |||
;; | ;; | ||
pi) | pi) | ||
printf "4*a(1)\n" | bc -l | printf "4*a(1)\n" | bc -l | ||
;; | |||
neg) | |||
printf "- %s\n" "${code[2]}" | bc | |||
;; | |||
add) | |||
bc_biterm "+" | |||
;; | |||
sub) | |||
bc_biterm "-" | |||
;; | |||
mul) | |||
bc_biterm "*" | |||
;; | |||
div) | |||
bc_biterm "/" | |||
;; | |||
mod) | |||
bc_biterm "%" | |||
;; | |||
pow) | |||
bc_biterm "^" | |||
;; | |||
lt) | |||
bc_biterm "<" | |||
;; | |||
lte) | |||
bc_biterm "<=" | |||
;; | |||
gt) | |||
bc_biterm ">" | |||
;; | |||
gte) | |||
bc_biterm ">=" | |||
;; | |||
eq) | |||
bc_biterm "==" | |||
;; | |||
neq) | |||
bc_biterm "!=" | |||
;; | |||
not) | |||
printf "!%s\n" "${code[2]}" | bc | |||
;; | |||
and) | |||
bc_biterm "&&" | |||
;; | |||
or) | |||
bc_biterm "||" | |||
;; | |||
sigfig) | |||
printf "length ( %s )\n" "${code[2]}" | bc | |||
;; | |||
sqrt) | |||
printf "sqrt ( %s )\n" "${code[2]}" | bc | |||
;; | ;; | ||
esac | esac | ||
Revision as of 01:57, 17 September 2026
#!/usr/bin/env bash
code=("$@")
bc_mathlib() {
printf "%s (%s)\n" "${1}" "${code[2]}" | bc -l
}
bc_biterm() {
printf "%s %s %s\n" "${code[2]}" "${1}" "${code[3]}" | bc
}
case $1 in
sum)
echo "${code[@]:1}" | awk -F" " '{for(i=1; i<=NF; i++) s+=$i} END {print s}'
;;
sin)
bc_mathlib s
;;
cos)
bc_mathlib c
;;
atan)
bc_mathlib a
;;
ln)
bc_mathlib l
;;
epow)
bc_mathlib e
;;
pi)
printf "4*a(1)\n" | bc -l
;;
neg)
printf "- %s\n" "${code[2]}" | bc
;;
add)
bc_biterm "+"
;;
sub)
bc_biterm "-"
;;
mul)
bc_biterm "*"
;;
div)
bc_biterm "/"
;;
mod)
bc_biterm "%"
;;
pow)
bc_biterm "^"
;;
lt)
bc_biterm "<"
;;
lte)
bc_biterm "<="
;;
gt)
bc_biterm ">"
;;
gte)
bc_biterm ">="
;;
eq)
bc_biterm "=="
;;
neq)
bc_biterm "!="
;;
not)
printf "!%s\n" "${code[2]}" | bc
;;
and)
bc_biterm "&&"
;;
or)
bc_biterm "||"
;;
sigfig)
printf "length ( %s )\n" "${code[2]}" | bc
;;
sqrt)
printf "sqrt ( %s )\n" "${code[2]}" | bc
;;
esac