_ _ ___ | |__ __ _ _______ | |_ / _ \| '_ \ / _` |_ / _ \| __| | (_) | | | | (_| |/ / (_) | |_ \___/|_| |_|\__,_/___\___/ \__|
| ohazot | docs | links | dev | conf | txt |
| es | en |
| mdoc file |
| search |
dc —
desk calculator - rpn
DESCRIPTION
It's a stack calculator using Reverse Polish Notation. Values are pushed into a stack and operations pop values from the stack in reverse order.
Example
To get the result of 1 + 2:
1 2 + f 3
- 1 is pushed into the stack.
- 2 is pushed into the stack.
- + pops the top two values from the stack, adds them and the result is pushed into the stack.
- f prints the complete stack.
Another example
To get the result of 2 * 3 + 4:
- one way to do it:
2 3 * 4 + f 10
+ pops 4 and *, therefore * pops 2 and 3, multiplies them and 6 is sent into the stack resulting in 6 4 +. - another way to do it:
4 2 3 * + f 10
+ pops * which pops 2 and 3 to get 6 which is pushed into the stack resulting in 4 6 +.
Scripting
dc can be called with file: dc
filename
OPERATIONS
The common operation are supported: + - * / ^.
| % | | pops the top two values, divides the second popped value by the first one and pushes the remainder into the stack. Eg.: 8 4 % fc 0, 8 5 % fc 3. |
| ~ | | similar but returns the quotient and remainder: 8 4 ~ fc 0 2, 8 5 ~ fc 3 1 |
| v | | pops the top value off the stack and calculates the square root. |
PRINTING
| f | | prints stack. |
| p | | prints value on top of the stack with newline. |
| n | | prints value on top of the stack without newline. |
STACK
| c | | clears the stack. |
| r | | swap the top two values of the stack. |
| d | | duplicates value on top of the stack. |
REGISTERS
| sr | | pops the top value off the stack and stores it into the register r. (r can be any single character) |
| Sr | | same as s but overwrites the original value of the register. |
| lr | | loads a copy of the value of register r. |
| Lr | | loads the value of register r removing it from the register. |
PARAMETERS
| k | | pop the top value off the stack and use it as the precision parameter. |
STRINGS
Strings can be stored as [string], this can be used to store strings or operations as macros which can be saved as a macro.
Example
[the string]p the string [2 2 +]Sa laxp 4
Macros
Macros can be invoked based on conditions:
| >r | | compare the top two values of the stack and runs r if the top value of the stack is greater. |
| !>r | | similar but runs r if the top value is not greater. (less or equal) |
| <r | | runs r if the top value is lower than the seconf value.. |
| !<r | | runs r if the top value is not lower than the second value. (greater or equal) |
| =r | | runs r if the top two values of the stack are equal. |
| !=r | | runs r if the top two values of the stack are not equal. |
Considering the top two values 1 2, r could run with the following comparisons: >r, !<r, !=r. r will not run with the following comparisons: !>r, <r, =r.
[runs]Sa [lap]Sb 1 2 >b runs 1 2 !>b 1 2 <b 1 2 !<b runs 1 2 =b 1 2 !=b runs
SEE ALSO
- OpenBSD manpages: dc(1)