| p:abstract
| - dc is a reverse-polish desk calculator which supports unlimited precision arithmetics. It is one of the oldest Unix utilities, predating even the invention of the C programming language; like other utilities of that vintage, it has a powerful set of features but an extremely terse syntax. Traditionally, the more user-friendly (with its infix notation) bc calculator program was implemented on top of dc, although more modern implementations are no longer related.
This article provides some examples in an attempt to give a flavour of the language; for a complete list of commands and syntax you should consult a manual page for your implementation.
To multiply four and five in dc (note that most of the whitespace is optional):
4 5 *
p
This translates into "push four and five onto the stack, then, with the multiplication operator, pop two elements from the stack, multiply them and push the result back on the stack." Then the 'p' command is used to examine (print out to the screen) the top element on the stack.
To change the precision, the command is k. Since the default precision is zero, this sequence of commands produces '0' as a result:
2 3 / p
By adjusting the precision with k, arbitrary number of decimal places can be produced. This command sequence outputs '.66666'.
5 k
2 3 / p
To evaluate (12 + 3^4)/11-22:
12 3 4 ^ + 11 / 22 -
p
In addition to these basic arithmetic and stack operations, dc includes support for macros, conditionals and storing of results for later retrieval; unfortunately the syntax is terse and complex programs in dc tend to be very hard to read.
The mechanism underlying macros and conditionals is the register, which in dc is a storage location with a single character name which can be stored to and retrieved from: 'sc' pops the top of the stack and stores it in register c, and 'lc' pushes the value of register c onto the stack. For example:
3 sc 4 lc * p
Registers can also be treated as secondary stacks, so values can be pushed and popped between them and the main stack.
Macros are then implemented by allowing registers and stack entries to be strings as well as numbers. A string can be printed, but it can also be executed (ie processed as a sequence of dc commands). So for instance we can store a macro to add one and then multiply by 2 into register m:
[1 + 2 *] sm
and then (using the 'x' command which executes the top of the stack) we can use it like this:
3 lm x p
Finally, we can use this macro mechanism to provide conditionals. The command '=r' will pop two values from the stack, and execute the macro stored in register r only if they are equal. So this will print the string 'equal' only if the top of the stack is equal to 5:
equal]p] sm 5 =m
Looping is then possible by defining a macro which (conditionally) reinvokes itself. (en)
- dc (ein Akronym für desk calculator) ist ein Rechenprogramm für Unix respektive Unix-Derivate. Es nutzt das Prinzip der umgekehrten polnischen Notation und ist eines der ältesten Unix-Tools - sogar älter als die Programmiersprache C. In der Praxis wird bc meistens dc vorgezogen, nicht zuletzt auch weil dc seit 1993 nicht mehr aktualisiert worden ist und weil bc auf dc aufsetzt. (de)
- D'après la page de manuel du projet GNU concernant dc, dc est une calculatrice en notation polonaise inverse qui permet une précision arithmétique illimitée.
C'est l'un des plus anciens outils Unix, antérieur au langage de programmation C. Comme d'autres utilitaires datant de cette époque, il dispose d'un jeu de fonctionnalités très puissantes, mais d'une syntaxe relativement cryptique. Le calculateur en notation infixée bc, plus simple d'utilisation, a été originellement créé comme une interface de programmation de plus haut niveau à dc. Les implémentations actuelles ne sont plus liées.
Catégorie:Unix (fr)
- dc to uniksowe narzędzie do prostych obliczeń używające języka stosowego typu RPN.
Współcześnie nie jest szerzej używane.
Działanie:
* każda liczba jest odkładana na stos
* operatory pobierają ze stosu określoną ilość elementów (0 lub więcej), wykonują nad nimi jakąś operację i odkładają określoną (0 lub więcej) ilość wartości na stos, ewentualnie mają jakieś efekty uboczne
* Wszystko między "[" a "]" (z uwzględnieniem zagnieżdżania), jest traktowane jako tekst i odkładane na stos w całości.
Programy można uruchamiać komendą: dc -e "program".
Przykłady programów:
* 2 2 2 * + p
** odkłada na stosie wartość 2 - stos po operacji: 2
** odkłada na stosie wartość 2 - stos po operacji: 2, 2
** odkłada na stosie wartość 2 - stos po operacji: 2, 2, 2
** pobiera dwie wartości w góry stosu i wrzuca na stos wynik ich mnożenia - stos po operacji: 2, 4
** pobiera dwie wartości w góry stosu i wrzuca na stos wynik ich dodawania - stos po operacji: 6
** drukuje najwyższą wartość ze stosu, po czym dodaje znak nowej linii - stos po operacji nadal zawiera wartość 6
* [Hello, world !] p
** odkłada na stosie tekst "Hello, world!"
** drukuje ten tekst
* [[Hello, world !] p] x
** umieszcza na stosie tekst [Hello, world !] p
** pobiera najwyższą wartość ze stosu i ją ewaluuje
* 2 sa 3 sb la la * sa lb lb * sb la lb + p
** uwaga: ten program jest napisany w wyjątkowo mało efektywny sposób
** zapisuje wartość 2 na stosie
** przenosi wartość z góry stosu do rejestru a
** zapisuje wartość 3 na stosie
** przenosi wartość z góry stosu do rejestru b
** kopiuje dwukrotnie wartość z rejestru a na stos
** zdejmuje i mnoży dwie najwyższe wartości na stosie, wynik umieszczając na stosie
** umieszcza wynik w rejestrze a
** to samo robi z b
** wczytuje wartości rejestrów a i b ze stosu
** dodaje je
** drukuje wynik operacji 2*2 + 3*3
* 2 3 d * r d * + p
** Program robi to samo co powyżej z wykorzystaniem dwóch nowych operacji:
** d - umieść wartość z góry stosu jeszcze raz (czyli jeśli stos to zawierał dane 2, 3 to po tej operacji będzie zawierał 2,3,3)
** r - zamień dwie wartości na górze stosu (czyli jeśli stos zawierał dane 2,9, to po tej operacji będzie zawierał 9,2) (pl)
|
| rdfs:comment
| - dc is a reverse-polish desk calculator which supports unlimited precision arithmetics. It is one of the oldest Unix utilities, predating even the invention of the C programming language; like other utilities of that vintage, it has a powerful set of features but an extremely terse syntax. Traditionally, the more user-friendly (with its infix notation) bc calculator program was implemented on top of dc, although more modern implementations are no longer related. (en)
- dc (ein Akronym für desk calculator) ist ein Rechenprogramm für Unix respektive Unix-Derivate. Es nutzt das Prinzip der umgekehrten polnischen Notation und ist eines der ältesten Unix-Tools - sogar älter als die Programmiersprache C. In der Praxis wird bc meistens dc vorgezogen, nicht zuletzt auch weil dc seit 1993 nicht mehr aktualisiert worden ist und weil bc auf dc aufsetzt. (de)
- D'après la page de manuel du projet GNU concernant dc, dc est une calculatrice en notation polonaise inverse qui permet une précision arithmétique illimitée. C'est l'un des plus anciens outils Unix, antérieur au langage de programmation C. Comme d'autres utilitaires datant de cette époque, il dispose d'un jeu de fonctionnalités très puissantes, mais d'une syntaxe relativement cryptique. (fr)
- dc to uniksowe narzędzie do prostych obliczeń używające języka stosowego typu RPN. (pl)
|