Compare commits

...

5 Commits

Author SHA1 Message Date
Yargo iMac f46e1af0a9 add use of TMPDIR for temporary dir, if defined 2017-08-31 15:20:14 +02:00
Yargo iMac 044085f70e change name of output file 2017-03-10 16:16:17 +01:00
yargo-moni 532fb8c3ec add output file for last top of stack 2017-03-10 16:09:48 +01:00
Yargo iMac 5812333583 replace all temporary registers by '.' and ':', simplify inv macro 2017-02-13 14:48:27 +01:00
Yargo 12836f1077 add %f "percent future": value to be added to Y resulting in sum of
which the added value is X%
2017-01-25 18:56:27 +00:00
2 changed files with 28 additions and 19 deletions

View File

@ -13,6 +13,8 @@ This wrapper renders `dc` more usable as a command line calculator by:
- defining commonly used macros for more complex operations - defining commonly used macros for more complex operations
- saving precision, top of stack, and registers 0 to 9 ("memories") - saving precision, top of stack, and registers 0 to 9 ("memories")
from one invocation to the next in the status file `$HOME/.wdcrc` 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 # PREREQUISITES
@ -160,8 +162,10 @@ only at the very end. Otherwise, e.g the `rem` command would first have its
# FILES # FILES
The status between invocations is stored in the file `$HOME/.wdcrc` as a - The status between invocations is stored in the file `$HOME/.wdcrc` as a list of `dc` commands.
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 # BUGS

View File

@ -1,8 +1,10 @@
#!/bin/sh #!/bin/sh
# wrapper for dc # 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 # global status file
statf=$HOME/.wdcrc statf=$HOME/.wdcrc
# output file (last stack top)
outf=${TMPDIR:-/tmp}/wdc-$USER
# clear state: precision 2, clear stack and reg.0..9 # clear state: precision 2, clear stack and reg.0..9
clstat='2k 0 0 0 0 0 0s00s10s20s30s40s50s60s70s80s9' clstat='2k 0 0 0 0 0 0s00s10s20s30s40s50s60s70s80s9'
if test ! -r $statf if test ! -r $statf
@ -17,22 +19,23 @@ fi
# macros defined as sed patterns # macros defined as sed patterns
# (kill all '!' not part of comparison commands, and also comments) # (kill all '!' not part of comparison commands, and also comments)
mcrs=' mcrs='
s,%t,SAdLA100*SASBLALB/,g;# percent part: X:=100*X/Y, Y kept s,%t,S.dL.100*S.S:L.L:/,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,%d,S.ddL.-_100*S.S:L.L:/,g;# percent delta: X:=100*(X-Y)/Y, Y kept
s,%,SAdLA*100/,g;# percentage: X:=X*Y/100, 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,hm,r60*+,g;# hoursminutes: X:=X+Y*60
s,rem,\%,g;# remainder: X:=Y%X (instead of normal '%' command) 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,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,neg,_1*,g;# negate: X:=-X
s,inv,1r/,g;# inverse: X:=1/X s,inv,S.1L./,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,\$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,\$-,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,\$+*,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, , ,;# 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,[eE]\(_*[0-9][0-9]*\), 10 \1^*,g;# infix exponential: X:=X*10^N
s,drop,0*+,g;# stack drop 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,![ ]*[^<=>].*,,
s,#.*,, s,#.*,,
' '
@ -96,21 +99,23 @@ l0n[s0
]nl8n[s8 ]nl8n[s8
]nl9n[s9 ]nl9n[s9
]n ]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, # 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 # prints 0 (from reg.'.'), else former top of stack, and clears stack
c 0sZ [1z=Znc]sY c 0s. [1z=.nc]s:
# apply macro in reg.Y to all saved stack values, and separate with NL # apply macro in reg.':' to all saved stack values, and separate with NL
LElYx[ LEl:x[
]nLDlYx[ ]nLDl:x[
]nLClYx[ ]nLCl:x[
]nLBlYx[ ]nLBl:x[
]nlAlYx[ # ]n ]nlAl:x[ # ]n
# print exponentialized version of stack top as comment: # print exponentialized version of stack top as comment:
# A=(number of digits - number of fraction digits - 1) of stack top, # A=(number of digits - number of fraction digits - 1) of stack top,
# divide stack top by 10^A and print, print A # divide stack top by 10^A and print, print A
lA10lAZLAX-1-sAlA^/n[E]nLAp lA10lAZLAX-1-sAlA^/n[E]nLAp
ENDOFDCINPUT ENDOFDCINPUT
# save stack top in output file for further direct processing
tail -n 1 $statf | sed -e 's/ *#.*//' >$outf
# print stack top # print stack top
tail -n 1 $statf tail -n 1 $statf
} }