]> git.lizzy.rs Git - plan9front.git/blob - sys/man/1/dc
merge
[plan9front.git] / sys / man / 1 / dc
1 .TH DC 1
2 .SH NAME
3 dc \- desk calculator
4 .SH SYNOPSIS
5 .B dc
6 [
7 .I file
8 ]
9 .SH DESCRIPTION
10 .I Dc
11 is an arbitrary precision desk calculator.
12 Ordinarily it operates on decimal integers,
13 but one may specify an input base, output base,
14 and a number of fractional digits to be maintained.
15 The overall structure of
16 .I dc
17 is
18 a stacking (reverse Polish) calculator.
19 If an argument is given,
20 input is taken from that file until its end,
21 then from the standard input.
22 The following constructions are recognized:
23 .TP
24 number
25 The value of the number is pushed on the stack.
26 A number is an unbroken string of the digits 
27 .B 0-9A-F 
28 or
29 .BR 0-9a-f .
30 A hexadecimal number beginning with a lower case
31 letter must be preceded by a zero to distinguish it
32 from the command associated with the letter.
33 It may be preceded by an underscore
34 .B _
35 to input a
36 negative number.
37 Numbers may contain decimal points.
38 .TP
39 .L
40 +  - /  *  %  ^
41 Add
42 .LR + ,
43 subtract
44 .LR - ,
45 multiply
46 .LR * ,
47 divide
48 .LR / ,
49 remainder
50 .LR % ,
51 or exponentiate
52 .L ^
53 the top two values on the stack.
54 The two entries are popped off the stack;
55 the result is pushed on the stack in their place.
56 Any fractional part of an exponent is ignored.
57 .TP
58 .BI s x
59 .br
60 .ns
61 .TP
62 .BI S x
63 Pop the top of the stack and store into
64 a register named
65 .IR x ,
66 where
67 .I x
68 may be any character.
69 Under operation
70 .B S
71 register
72 .I x
73 is treated as a stack and the value is pushed on it.
74 .TP
75 .BI l x
76 .br
77 .ns
78 .TP
79 .BI L x
80 Push the value in register
81 .I x
82 onto the stack.
83 The register
84 .I x
85 is not altered.
86 All registers start with zero value.
87 Under operation
88 .B L
89 register
90 .I x
91 is treated as a stack and its top value is popped onto the main stack.
92 .TP
93 .B  d
94 Duplicate the
95 top value on the stack.
96 .TP
97 .B  p
98 Print the top value on the stack.
99 The top value remains unchanged.
100 .B P
101 interprets the top of the stack as a text
102 string,
103 removes it, and prints it.
104 .TP
105 .B  f
106 Print the values on the stack.
107 .TP
108 .B  q
109 .br
110 .ns
111 .TP
112 .B Q
113 Exit the program.
114 If executing a string, the recursion level is
115 popped by two.
116 Under operation
117 .B Q
118 the top value on the stack is popped and the string execution level is popped
119 by that value.
120 .TP
121 .B  x
122 Treat the top element of the stack as a character string
123 and execute it as a string of
124 .I dc
125 commands.
126 .TP
127 .B  X
128 Replace the number on the top of the stack with its scale factor.
129 .TP
130 .B "[ ... ]"
131 Put the bracketed
132 text
133 string on the top of the stack.
134 .TP
135 .PD0
136 .BI < x
137 .TP
138 .BI > x
139 .TP
140 .BI = x
141 .PD
142 Pop and compare the
143 top two elements of the stack.
144 Register
145 .I x
146 is executed if they obey the stated
147 relation.
148 .TP
149 .B  v
150 Replace the top element on the stack by its square root.
151 Any existing fractional part of the argument is taken
152 into account, but otherwise the scale factor is ignored.
153 .TP
154 .B  !
155 Interpret the rest of the line as a shell command.
156 .TP
157 .B  c
158 Clear the stack.
159 .TP
160 .B  i
161 The top value on the stack is popped and used as the
162 number base for further input.
163 .TP
164 .B I
165 Push the input base on the top of the stack.
166 .TP
167 .B  o
168 The top value on the stack is popped and used as the
169 number base for further output.
170 In bases larger than 10, each `digit' prints as a group of decimal digits.
171 .TP
172 .B O
173 Push the output base on the top of the stack.
174 .TP
175 .B  k
176 Pop the top of the stack, and use that value as
177 a non-negative scale factor:
178 the appropriate number of places
179 are printed on output,
180 and maintained during multiplication, division, and exponentiation.
181 The interaction of scale factor,
182 input base, and output base will be reasonable if all are changed
183 together.
184 .TP
185 .B  z
186 Push the stack level onto the stack.
187 .TP
188 .B  Z
189 Replace the number on the top of the stack with its length.
190 .TP
191 .B  ?
192 A line of input is taken from the input source (usually the terminal)
193 and executed.
194 .TP
195 .B "; :"
196 Used by 
197 .I bc
198 for array operations.
199 .PP
200 The scale factor set by
201 .B k
202 determines how many digits are kept to the right of
203 the decimal point.
204 If
205 .I s
206 is the current scale factor,
207 .I sa
208 is the scale of the first operand,
209 .I sb
210 is the scale of the second,
211 and
212 .I b
213 is the (integer) second operand,
214 results are truncated to the following scales.
215 .IP
216 .nf
217 \fL+\fR,\fL-\fR max(\fIsa,sb\fR)
218 \fL*\fR min(\fIsa\fR+\fIsb \fR, max\fR(\fIs,sa,sb\fR))
219 \fL/\fI s
220 \fL%\fR so that dividend = divisor*quotient + remainder; remainder has sign of dividend
221 \fL^\fR min(\fIsa\fR\(mu|\fIb\fR|, max(\fIs,sa\fR))
222 \fLv\fR max(\fIs,sa\fR)
223 .fi
224 .SH EXAMPLES
225 .LP
226 Print the first ten values of
227 .IR n !
228 .IP
229 .EX
230 [la1+dsa*pla10>y]sy
231 0sa1
232 lyx
233 .EE
234 .SH SOURCE
235 .B /sys/src/cmd/dc.c
236 .SH "SEE ALSO"
237 .IR bc (1),
238 .IR hoc (1)
239 .SH DIAGNOSTICS
240 .I x
241 .LR "is unimplemented" ,
242 where
243 .I x
244 is an octal number: an internal error.
245 .br
246 `Out of headers'
247 for too many numbers being kept around.
248 .br
249 `Nesting depth'
250 for too many levels of nested execution.
251 .SH BUGS
252 When the input base exceeds 16,
253 there is no notation for digits greater than
254 .BR F .
255 .PP
256 Past its time.