Skip to content
David-Apps edited this page May 27, 2023 · 14 revisions

Send commands to dc

The dc function reads the current line into dc, sends the commands to dc, and replaces the current line with the stack from dc. If the current line does not exist, the dc function sends the commands to dc and reads the stack from dc into the buffer.

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]
function:dc {
db0
H-
ebvar-
X
if(*) {
# Prepare negative numbers for dc.
s/\(^\|\s+\)-\(\d*\.?\d*\)/$1_$2/gf
.W !echo $(dc -e "? ~0 f" | tac)
} else {
r !echo $(dc -e "~0 f" | tac)
}
}

Send each number on a line to dc separately

If the current line contains 1 or more numbers, for each number, the ldc function reads the number into dc, sends the commands to dc, and replaces the number with the stack from dc. If the current line does not contain a number, the ldc function sends the commands to dc and reads the stack from dc into the buffer.

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/f s/^-/_/f
'y+,'zg/\d/f .W !echo $(dc -e "? ~0 f" | tac)
'y+,'zj
} else {
s/-?\d*\.?\d+/\n&\n/gf
kz
1,'zg/\d/f s/^-/_/f
1,'zg/\d/f .W !echo $(dc -e "? ~0 f" | tac)
1,'zj
}
} else {
r !echo $(dc -e "~0 f" | tac)
}
p
}

Send commands to dc and count the numbers on the stack

If the current line exists, the count function reads the line into dc, sends the commands to dc, and replaces the current line with the count of the dc stack. If the current line does not exist, the count function sends the commands to dc and reads the count of the dc stack into the buffer.

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
}

Send commands to dc and calculate the sum of the numbers on the stack

If the current line exists, the sum function reads the line into dc, sends the commands to dc, and replaces the current line with the sum of the dc stack. If the current line does not exist, the sum function sends the commands to dc and reads the sum of the dc stack into the buffer.

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
}

Send commands to dc and calculate the mean of the numbers on the stack

If the current line exists, the mean function reads the line into dc, sends the commands to dc, and replaces the current line with the mean of the dc stack. If the current line does not exist, the mean function sends the commands to dc and reads the mean of the dc stack into the buffer.

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
}

Send commands to dc and calculate the precision-controlled product of the numbers on the stack

If the current line exists, the kproduct function reads the line into dc, sends the commands to dc, and replaces the current line with the precision-controlled product of the dc stack. If the current line does not exist, the kproduct function sends the commands to dc and reads the precision-controlled product of the dc stack into the buffer.

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
}

Send commands to dc and calculate the product of the numbers on the stack

If the current line exists, the product function reads the line into dc, sends the commands to dc, and replaces the current line with the product of the dc stack. If the current line does not exist, the product function sends the commands to dc and reads the product of the dc stack into the buffer.

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 }

Send commands to dc and calculate the maximum of the numbers on the stack

If the current line exists, the max function reads the line into dc, sends the commands to dc, and replaces the current line with the maximum of the dc stack. If the current line does not exist, the max function sends the commands to dc and reads the maximum of the dc stack into the buffer.

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
}

Send commands to dc and calculate the minimum of the numbers on the stack

If the current line exists, the min function reads the line into dc, sends the commands to dc, and replaces the current line with the minimum of the dc stack. If the current line does not exist, the min function sends the commands to dc and reads the minimum of the dc stack into the buffer.

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
}
Clone this wiki locally