dc (Unix)

from Wikipedia, the free encyclopedia

dc (an acronym for desk calculator ) is a computer program for Unix or Unix derivatives . It uses the reverse Polish notation principle and is one of the oldest Unix tools - even older than the C programming language . In practice, bc is mostly preferred to dc because users have problems with the reverse Polish notation. The bc program is based on dc in the POSIX variant (the GNU variant was newly developed).

Examples

Note: the one pat the end of the input stands for print and outputs the result, more precisely the last element of the stack. In the examples, the result is also listed in the last line. All spaces and lines, with the exception of those between digits, are optional. The syntax may seem a bit idiosyncratic and is fundamentally different from what you would type into a calculator, for example. This is because dc is stack- based. The first example would be verbally translated as "push (push) the two elements six and eight on the stack, remove (pop) them with the multiplication operator, multiply them and write the result back to the stack, then give the contents of the stack on the screen ".

Multiplication: Calculates 6 * 8

6 8 * p
48

Several types of calculation combined: Calculated (12 + 3 ^ 4) / 11-22:

12 3 4 ^ + 11 / 22 – p
-14

register

In addition to the stack, registers can also be used. With s< name of the register > (e.g. scfor the register with the name c) values ​​are saved in it (s = English store 'save' ), with l < name of the register > fetched from it (l = English load 'load') ). The first example with register:

8 sc 6 lc * p
48

Strictly speaking, the single value in a register is the top element of an entire stack: Scpushes a value onto the register's stack c, and Lcremoves a value from there (and puts it on the anonymous stack):

8 Sc 6 Lc * p
48

Square brackets are strings formed. They can also be saved in registers. The following example calculates (3 + 1) * 2 using a register (m):

[1 + 2 *] sm
3 lm x p
8

That xperforms the calculation that is in the top stack value.

Macros can also be executed with dc:

dc -e '[[Gib einen Wert in Meter an oder drücke 0 um zu beenden]psj]
sh[q]sz[lhx?d0=z10k39.370079*.5+0k12~1/rn[ Fuß ]Pn[ Zoll]P10Pdx]dx'

Here is no longer used the dc own shell, but the macro with the switch -e (for English execute , 'Run ) to dc passed. The script converts the unit of measurement from meters to feet and inches.

Web links