| 1 | .TH DC 1 |
| 2 | .SH NAME |
| 3 | dc \- desk calculator |
| 4 | .SH SYNOPSIS |
| 5 | .B dc |
| 6 | [ file ] |
| 7 | .SH DESCRIPTION |
| 8 | .I Dc |
| 9 | is an arbitrary precision arithmetic package. |
| 10 | Ordinarily it operates on decimal integers, |
| 11 | but one may specify an input base, output base, |
| 12 | and a number of fractional digits to be maintained. |
| 13 | The overall structure of |
| 14 | .I dc |
| 15 | is |
| 16 | a stacking (reverse Polish) calculator. |
| 17 | If an argument is given, |
| 18 | input is taken from that file until its end, |
| 19 | then from the standard input. |
| 20 | The following constructions are recognized: |
| 21 | .HP 6 |
| 22 | number |
| 23 | .br |
| 24 | The value of the number is pushed on the stack. |
| 25 | A number is an unbroken string of the digits 0-9. |
| 26 | It may be preceded by an underscore _ to input a |
| 27 | negative number. |
| 28 | Numbers may contain decimal points. |
| 29 | .HP 6 |
| 30 | + \- / * % ^ |
| 31 | .br |
| 32 | The |
| 33 | top two values on the stack are added |
| 34 | (+), |
| 35 | subtracted |
| 36 | (\-), |
| 37 | multiplied (*), |
| 38 | divided (/), |
| 39 | remaindered (%), |
| 40 | or exponentiated (^). |
| 41 | The two entries are popped off the stack; |
| 42 | the result is pushed on the stack in their place. |
| 43 | Any fractional part of an exponent is ignored. |
| 44 | .TP |
| 45 | .BI s x |
| 46 | The |
| 47 | top of the stack is popped and stored into |
| 48 | a register named |
| 49 | .I x, |
| 50 | where |
| 51 | .I x |
| 52 | may be any character. |
| 53 | If |
| 54 | the |
| 55 | .B s |
| 56 | is capitalized, |
| 57 | .I x |
| 58 | is treated as a stack and the value is pushed on it. |
| 59 | .TP |
| 60 | .BI l x |
| 61 | The |
| 62 | value in register |
| 63 | .I x |
| 64 | is pushed on the stack. |
| 65 | The register |
| 66 | .I x |
| 67 | is not altered. |
| 68 | All registers start with zero value. |
| 69 | If the |
| 70 | .B l |
| 71 | is capitalized, |
| 72 | register |
| 73 | .I x |
| 74 | is treated as a stack and its top value is popped onto the main stack. |
| 75 | .TP |
| 76 | .B d |
| 77 | The |
| 78 | top value on the stack is duplicated. |
| 79 | .TP |
| 80 | .B p |
| 81 | The top value on the stack is printed. |
| 82 | The top value remains unchanged. |
| 83 | .B P |
| 84 | interprets the top of the stack as an ascii string, |
| 85 | removes it, and prints it. |
| 86 | .TP |
| 87 | .B f |
| 88 | All values on the stack and in registers are printed. |
| 89 | .TP |
| 90 | .B q |
| 91 | exits the program. |
| 92 | If executing a string, the recursion level is |
| 93 | popped by two. |
| 94 | If |
| 95 | .B q |
| 96 | is capitalized, |
| 97 | the top value on the stack is popped and the string execution level is popped |
| 98 | by that value. |
| 99 | .TP |
| 100 | .B x |
| 101 | treats the top element of the stack as a character string |
| 102 | and executes it as a string of dc commands. |
| 103 | .TP |
| 104 | .B X |
| 105 | replaces the number on the top of the stack with its scale factor. |
| 106 | .TP |
| 107 | .B "[ ... ]" |
| 108 | puts the bracketed ascii string onto the top of the stack. |
| 109 | .HP 6 |
| 110 | .I "<x >x =x" |
| 111 | .br |
| 112 | The |
| 113 | top two elements of the stack are popped and compared. |
| 114 | Register |
| 115 | .I x |
| 116 | is executed if they obey the stated |
| 117 | relation. |
| 118 | .TP |
| 119 | .B v |
| 120 | replaces the top element on the stack by its square root. |
| 121 | Any existing fractional part of the argument is taken |
| 122 | into account, but otherwise the scale factor is ignored. |
| 123 | .TP |
| 124 | .B ! |
| 125 | interprets the rest of the line as a UNIX command. |
| 126 | .TP |
| 127 | .B c |
| 128 | All values on the stack are popped. |
| 129 | .TP |
| 130 | .B i |
| 131 | The top value on the stack is popped and used as the |
| 132 | number radix for further input. |
| 133 | .B I |
| 134 | pushes the input base on the top of the stack. |
| 135 | .TP |
| 136 | .B o |
| 137 | The top value on the stack is popped and used as the |
| 138 | number radix for further output. |
| 139 | .TP |
| 140 | .SM |
| 141 | .B O |
| 142 | pushes the output base on the top of the stack. |
| 143 | .TP |
| 144 | .B k |
| 145 | the top of the stack is popped, and that value is used as |
| 146 | a non-negative scale factor: |
| 147 | the appropriate number of places |
| 148 | are printed on output, |
| 149 | and maintained during multiplication, division, and exponentiation. |
| 150 | The interaction of scale factor, |
| 151 | input base, and output base will be reasonable if all are changed |
| 152 | together. |
| 153 | .TP |
| 154 | .B z |
| 155 | The stack level is pushed onto the stack. |
| 156 | .TP |
| 157 | .SM |
| 158 | .B Z |
| 159 | replaces the number on the top of the stack with its length. |
| 160 | .TP |
| 161 | .B ? |
| 162 | A line of input is taken from the input source (usually the terminal) |
| 163 | and executed. |
| 164 | .TP |
| 165 | .B "; :" |
| 166 | are used by |
| 167 | .I bc |
| 168 | for array operations. |
| 169 | .PP |
| 170 | An example which prints the first ten values of n! is |
| 171 | .nf |
| 172 | .PP |
| 173 | .in +3 |
| 174 | [la1+dsa*pla10>y]sy |
| 175 | .br |
| 176 | 0sa1 |
| 177 | .br |
| 178 | lyx |
| 179 | .fi |
| 180 | .SH "SEE ALSO" |
| 181 | bc(1), |
| 182 | which is a preprocessor for |
| 183 | .I dc |
| 184 | providing infix notation and a C-like syntax |
| 185 | which implements functions and reasonable control |
| 186 | structures for programs. |
| 187 | .SH DIAGNOSTICS |
| 188 | `x is unimplemented' where x is an octal number. |
| 189 | .br |
| 190 | `stack empty' for not enough elements on the stack to do what was asked. |
| 191 | .br |
| 192 | `Out of space' when the free list is exhausted (too many digits). |
| 193 | .br |
| 194 | `Out of headers' for too many numbers being kept around. |
| 195 | .br |
| 196 | `Out of pushdown' for too many items on the stack. |
| 197 | .br |
| 198 | `Nesting Depth' for too many levels of nested execution. |