-
Notifications
You must be signed in to change notification settings - Fork 31
Calculate with dc
If the current line contains a number, the dc
function sends the line and the commands to dc
, replacing the current line with the output from dc
.
If the current line does not contain a number, the dc
function sends the commands to dc
, appending the output from dc
after the current line.
In both cases, the output is the stack from dc
.
Suppose that the current line is:
1 2 3 4
The command <dc +*+
changes the line to:
15
# Send commands to dc.
# usage: <dc [commands]
# If the current line contains a number, dc reads the current line first.
function:dc {
db0
H-
ebvar-
# Check whether the current line contains a number.
s/\d/&/f
if(*) {
# Prepare negative numbers for dc.
s/^-\(\d+\(\.\d*\)?\)/_$1/f
s/\(\s+\)-\(\d+\(\.\d*\)?\)/$1_$2/gf
.W !echo $(dc -e "? ~0 f" | tac)
} else {
r !echo $(dc -e "~0 f" | tac)
}
}
If the current line contains 1 or more numbers, the ldc
function sends each number and the commands to dc
separately, replacing the number with the output from dc
.
If the current line does not contain a number, the ldc
function sends the commands to dc
, appending the output from dc
after the current line.
In all cases, the output is the stack from dc
.
Suppose that the current line is:
1 2 3 4
The command <ldc 1 + 2 ^
changes the line to:
4 9 16 25
# Send each number on a line separately to dc.
# usage: <ldc [commands]
# This function uses the labels y and z.
function+ldc {
db0
H-
ebvar-
# Check whether the current line contains a number.
s/\d/&/f
if(*) {
-X
if(*) {
ky
+X
s/-?\d+\(\.\d*\)?/\n&\n/gf
kz
'y+,'zg/^-?\d+\(\.\d*\)?$/f .W !echo $(dc -e "? ~0 f" | tac)
'y+,'zj
} else {
s/-?\d+\(\.\d*\)?/\n&\n/gf
kz
1,'zg/^-?\d+\(\.\d*\)?$/f .W !echo $(dc -e "? ~0 f" | tac)
1,'zj
}
} else {
r !echo $(dc -e "~0 f" | tac)
}
p
}
If the current line contains a number, the count
function sends the line and the commands to dc
, replacing the current line with the count of the dc
stack.
If the current line does not contain a number, the count
function sends the commands to dc
, appending the count of the dc
stack after the current line.
Suppose that the current line is:
1 2 3 4
The command <count
changes the line to:
4
# After executing any dc commands, count the numbers on the stack
# usage: <count [commands]
function+count {
<dc ~0 zSacLa
}
If the current line contains a number, the sum
function sends the line and the commands to dc
, replacing the current line with the sum of the dc
stack.
If the current line does not contain a number, the sum
function sends the commands to dc
, appending the sum of the dc
stack after the current line.
Suppose that the current line is:
1 2 3 4
The command <sum
changes the line to:
10
# After executing any dc commands, calculate the sum of the numbers on the stack
# usage: <sum [commands]
function+sum {
<dc ~0 [+2z>a]sa2z>a
}
If the current line contains a number, the mean
function sends the line and the commands to dc
, replacing the current line with the mean of the dc
stack.
If the current line does not contain a number, the mean
function sends the commands to dc
, appending the mean of the dc
stack after the current line.
Suppose that the current line is:
1 2 3 4
The command <mean 2k
changes the line to:
2.50
(The 2k
command changed the precision to 2.)
# After executing any dc commands, calculate the mean of the numbers on the stack
# usage: <mean [commands]
# Use a command like 3k to set the precision to 3.
function+mean {
<dc ~0 [+2z>b]sb[zSa2z>bLa/]sc1z>c
}
If the current line contains a number, the kproduct
function sends the line and the commands to dc
, replacing the current line with the precision-controlled product of the dc
stack.
If the current line does not contain a number, the kproduct
function sends the commands to dc
, appending the precision-controlled product of the dc
stack after the current line.
Suppose that the current line is:
1.1 2.2 3.3 4.4
The command <kproduct 2k
changes the line to:
35.13
# After executing any dc commands, calculate the precision-controlled product of the numbers on the stack
# usage: <kproduct [commands]
# Use a command like 3k to set the precision to 3.
function+kproduct {
<dc ~0 [*2z>a]sa2z>a
}
If the current line contains a number, the product
function sends the line and the commands to dc
, replacing the current line with the product of the dc
stack.
If the current line does not contain a number, the product
function sends the commands to dc
, appending the product of the dc
stack after the current line.
Suppose that the current line is:
1.1 2.2 3.3 4.4
The command <product
changes the line to:
35.1384
# After executing any dc commands, calculate the product of the numbers on the stack
# usage: <product [commands]
function+product {
<dc ~0 [SadXlaX+kLa*2z>b]sb2z>b }
If the current line contains a number, the max
function sends the line and the commands to dc
, replacing the current line with the maximum of the dc
stack.
If the current line does not contain a number, the max
function sends the commands to dc
, appending the maximum of the dc
stack after the current line.
Suppose that the current line is:
1 2 3 4
The command <max
changes the line to:
4
# After executing any dc commands, calculate the maximum of the numbers on the stack
# usage: <max [commands]
function+max {
<dc ~0 [sala]sc[dla<csb1z>d]sd[sa1z>dla]se1z>e
}
If the current line contains a number, the min
function sends the line and the commands to dc
, replacing the current line with the minimum of the dc
stack.
If the current line does not contain a number, the min
function sends the commands to dc
, appending the minimum of the dc
stack after the current line.
Suppose that the current line is:
1 2 3 4
The command <min
changes the line to:
1
# After executing any dc commands, calculate the minimum of the numbers on the stack
# usage: <min [commands]
function+min {
<dc ~0 [sala]sc[dla>csb1z>d]sd[sa1z>dla]se1z>e
}