Jump to content
Toggle menu
  • 6 users
  • 271 edits
The Commonwealth of Acropolis
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

The Constitution is blocked from ratification due to the standing appointment of the Recruiter-General

Away:Module-math: Difference between revisions

From The Commonwealth of Acropolis
No edit summary
No edit summary
 
(7 intermediate revisions by the same user not shown)
Line 1: Line 1:
#!/usr/bin/env bash
#!/usr/bin/env bash
export BC_LINE_LENGTH=0
code=("$@")
code=("$@")
glob_scale=100
var_conf_math_scale=${var_conf_math_scale:-20}
bc_mathlib() {
bc_mathlib() {
     echo "scale=${glob_scale}; ${1} (${code[1]})" | bc -l
     echo "scale=${var_conf_math_scale}; ${1} (${code[1]})" | bc -l
}
}
bc_biterm() {
bc_biterm() {
     echo "scale=${glob_scale}; ${code[1]} ${1} ${code[2]}" | bc
     echo "scale=${var_conf_math_scale}; ${code[1]} ${1} ${code[2]}" | bc
}
}
case $1 in
case $1 in
Line 31: Line 32:
         ;;
         ;;
     neg)
     neg)
         printf "- %s\n" "${code[1]}" | bc
         printf "scale=%s; - %s\n" "${glob_scale}" "${code[1]}" | bc
         ;;
         ;;
     add)
     add)

Latest revision as of 00:12, 19 September 2026

#!/usr/bin/env bash
export BC_LINE_LENGTH=0
code=("$@")
var_conf_math_scale=${var_conf_math_scale:-20}
bc_mathlib() {
    echo "scale=${var_conf_math_scale}; ${1} (${code[1]})" | bc -l
}
bc_biterm() {
    echo "scale=${var_conf_math_scale}; ${code[1]} ${1} ${code[2]}" | 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 "scale=%s; - %s\n" "${glob_scale}" "${code[1]}" | 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[1]}" | bc
        ;;
    and)
        bc_biterm "&&"
        ;;
    or)
        bc_biterm "||"
        ;;
    sigfig)
        printf "length ( %s )\n" "${code[1]}" | bc
        ;;
    sqrt)
        printf "sqrt ( %s )\n" "${code[1]}" | bc
        ;;
esac