Compare commits
5 Commits
0b10c0b8a2
...
f46e1af0a9
Author | SHA1 | Date |
---|---|---|
Yargo iMac | f46e1af0a9 | |
Yargo iMac | 044085f70e | |
yargo-moni | 532fb8c3ec | |
Yargo iMac | 5812333583 | |
Yargo | 12836f1077 |
|
@ -13,6 +13,8 @@ This wrapper renders `dc` more usable as a command line calculator by:
|
|||
- defining commonly used macros for more complex operations
|
||||
- saving precision, top of stack, and registers 0 to 9 ("memories")
|
||||
from one invocation to the next in the status file `$HOME/.wdcrc`
|
||||
- saving last top of stack value in `${TMPDIR:-/tmp}/wdc-$USER` for further direct use
|
||||
(change file name in the source, if desired)
|
||||
|
||||
# PREREQUISITES
|
||||
|
||||
|
@ -160,8 +162,10 @@ only at the very end. Otherwise, e.g the `rem` command would first have its
|
|||
|
||||
# FILES
|
||||
|
||||
The status between invocations is stored in the file `$HOME/.wdcrc` as a
|
||||
list of `dc` commands.
|
||||
- The status between invocations is stored in the file `$HOME/.wdcrc` as a list of `dc` commands.
|
||||
- The last top of stack value is stored in `/tmp/wdc-$USER`.
|
||||
|
||||
_These file names can be easily changed at the beginning of the script._
|
||||
|
||||
# BUGS
|
||||
|
||||
|
|
39
wrapdc.sh
39
wrapdc.sh
|
@ -1,8 +1,10 @@
|
|||
#!/bin/sh
|
||||
# wrapper for dc
|
||||
info="wrapper for dc // 2016-11-22 Y.Bonetti // see https://gitlab.com/yargo/wrapdc"
|
||||
info="wrapper for dc // 2017-08-31 Y.Bonetti // see https://gitlab.com/yargo/wrapdc"
|
||||
# global status file
|
||||
statf=$HOME/.wdcrc
|
||||
# output file (last stack top)
|
||||
outf=${TMPDIR:-/tmp}/wdc-$USER
|
||||
# clear state: precision 2, clear stack and reg.0..9
|
||||
clstat='2k 0 0 0 0 0 0s00s10s20s30s40s50s60s70s80s9'
|
||||
if test ! -r $statf
|
||||
|
@ -17,22 +19,23 @@ fi
|
|||
# macros defined as sed patterns
|
||||
# (kill all '!' not part of comparison commands, and also comments)
|
||||
mcrs='
|
||||
s,%t,SAdLA100*SASBLALB/,g;# percent part: X:=100*X/Y, Y kept
|
||||
s,%d,SAddLA-_100*SASBLALB/,g;# percent delta: X:=100*(X-Y)/Y, Y kept
|
||||
s,%,SAdLA*100/,g;# percentage: X:=X*Y/100, Y kept
|
||||
s,%t,S.dL.100*S.S:L.L:/,g;# percent part: X:=100*X/Y, Y kept
|
||||
s,%d,S.ddL.-_100*S.S:L.L:/,g;# percent delta: X:=100*(X-Y)/Y, Y kept
|
||||
s,%f,S.dl.*100L.-/,g;# percent future: X:=Y*X/(100-X), Y kept
|
||||
s,%,S.dL.*100/,g;# percentage: X:=X*Y/100, Y kept
|
||||
s,hm,r60*+,g;# hoursminutes: X:=X+Y*60
|
||||
s,rem,\%,g;# remainder: X:=Y%X (instead of normal '%' command)
|
||||
s,sto\(.\),s\1l\1,g;# sto.: store with copying (i.e keep value on stack)
|
||||
s,fact,[SAlA*LA1-d0<B]SBSA1LAlBx0*sBLB+,g;# factorial: X:=X!
|
||||
s,fact,[S.l.*L.1-d0<:]S:S.1L.l:x0*s:L:+,g;# factorial: X:=X!
|
||||
s,neg,_1*,g;# negate: X:=-X
|
||||
s,inv,1r/,g;# inverse: X:=1/X
|
||||
s,\$m,[l4l1/l2l1/]SSl10!=SsSLS,g;# mean value: X:=reg.2/reg.1, Y:=reg.4/reg.1
|
||||
s,inv,S.1L./,g;# inverse: X:=1/X
|
||||
s,\$m,[l4l1/l2l1/]S.l10!=.s.L.,g;# mean value: X:=reg.2/reg.1, Y:=reg.4/reg.1
|
||||
s,\$-,dL2r-s2dd*L3r-s3rdL4r-s4dd*L5r-s5*L6r-s6L11-s1,g;# remove statistic entry
|
||||
s,\$+*,dL2+s2dd*L3+s3rdL4+s4dd*L5+s5*L6+s6L11+s1,g;# add statistic entry
|
||||
s, , ,;# statistic registers:1=n 2=sumX 3=sumX^2 4=sumY 5=sumY^2 6=sumXY
|
||||
s,[eE]\(_*[0-9][0-9]*\), 10 \1^*,g;# infix exponential: X:=X*10^N
|
||||
s,drop,0*+,g;# stack drop
|
||||
s,r,SRSSLRLS,g;# revert: X:=Y, Y:=X ('r' is a GNU extension)
|
||||
s,r,S.S:L.L:,g;# revert: X:=Y, Y:=X ('r' is a GNU extension)
|
||||
s,![ ]*[^<=>].*,,
|
||||
s,#.*,,
|
||||
'
|
||||
|
@ -96,21 +99,23 @@ l0n[s0
|
|||
]nl8n[s8
|
||||
]nl9n[s9
|
||||
]n
|
||||
# clear stack, store 0 in reg.Z and macro in reg.Y, which compares stack
|
||||
# clear stack, store 0 in reg.'.' and macro in reg.':', which compares stack
|
||||
# depth with 1 (in case of empty stack generated by this '1') and if equal,
|
||||
# prints 0 (from reg.Z), else former top of stack, and clears stack
|
||||
c 0sZ [1z=Znc]sY
|
||||
# apply macro in reg.Y to all saved stack values, and separate with NL
|
||||
LElYx[
|
||||
]nLDlYx[
|
||||
]nLClYx[
|
||||
]nLBlYx[
|
||||
]nlAlYx[ # ]n
|
||||
# prints 0 (from reg.'.'), else former top of stack, and clears stack
|
||||
c 0s. [1z=.nc]s:
|
||||
# apply macro in reg.':' to all saved stack values, and separate with NL
|
||||
LEl:x[
|
||||
]nLDl:x[
|
||||
]nLCl:x[
|
||||
]nLBl:x[
|
||||
]nlAl:x[ # ]n
|
||||
# print exponentialized version of stack top as comment:
|
||||
# A=(number of digits - number of fraction digits - 1) of stack top,
|
||||
# divide stack top by 10^A and print, print A
|
||||
lA10lAZLAX-1-sAlA^/n[E]nLAp
|
||||
ENDOFDCINPUT
|
||||
# save stack top in output file for further direct processing
|
||||
tail -n 1 $statf | sed -e 's/ *#.*//' >$outf
|
||||
# print stack top
|
||||
tail -n 1 $statf
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue