1
0

dc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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 an
  102. text
  103. string,
  104. removes it, and prints it.
  105. .TP
  106. .B f
  107. Print the values on the stack.
  108. .TP
  109. .B q
  110. .br
  111. .ns
  112. .TP
  113. .B Q
  114. Exit the program.
  115. If executing a string, the recursion level is
  116. popped by two.
  117. Under operation
  118. .B Q
  119. the top value on the stack is popped and the string execution level is popped
  120. by that value.
  121. .TP
  122. .B x
  123. Treat the top element of the stack as a character string
  124. and execute it as a string of
  125. .I dc
  126. commands.
  127. .TP
  128. .B X
  129. Replace the number on the top of the stack with its scale factor.
  130. .TP
  131. .B "[ ... ]"
  132. Put the bracketed
  133. text
  134. string on the top of the stack.
  135. .TP
  136. .PD0
  137. .BI < x
  138. .TP
  139. .BI > x
  140. .TP
  141. .BI = x
  142. .PD
  143. Pop and compare the
  144. top two elements of the stack.
  145. Register
  146. .I x
  147. is executed if they obey the stated
  148. relation.
  149. .TP
  150. .B v
  151. Replace the top element on the stack by its square root.
  152. Any existing fractional part of the argument is taken
  153. into account, but otherwise the scale factor is ignored.
  154. .TP
  155. .B !
  156. Interpret the rest of the line as a shell command.
  157. .TP
  158. .B c
  159. Clear the stack.
  160. .TP
  161. .B i
  162. The top value on the stack is popped and used as the
  163. number base for further input.
  164. .TP
  165. .B I
  166. Push the input base on the top of the stack.
  167. .TP
  168. .B o
  169. The top value on the stack is popped and used as the
  170. number base for further output.
  171. In bases larger than 10, each `digit' prints as a group of decimal digits.
  172. .TP
  173. .B O
  174. Push the output base on the top of the stack.
  175. .TP
  176. .B k
  177. Pop the top of the stack, and use that value as
  178. a non-negative scale factor:
  179. the appropriate number of places
  180. are printed on output,
  181. and maintained during multiplication, division, and exponentiation.
  182. The interaction of scale factor,
  183. input base, and output base will be reasonable if all are changed
  184. together.
  185. .TP
  186. .B z
  187. Push the stack level onto the stack.
  188. .TP
  189. .B Z
  190. Replace the number on the top of the stack with its length.
  191. .TP
  192. .B ?
  193. A line of input is taken from the input source (usually the terminal)
  194. and executed.
  195. .TP
  196. .B "; :"
  197. Used by
  198. .I bc
  199. for array operations.
  200. .PP
  201. The scale factor set by
  202. .B k
  203. determines how many digits are kept to the right of
  204. the decimal point.
  205. If
  206. .I s
  207. is the current scale factor,
  208. .I sa
  209. is the scale of the first operand,
  210. .I sb
  211. is the scale of the second,
  212. and
  213. .I b
  214. is the (integer) second operand,
  215. results are truncated to the following scales.
  216. .IP
  217. .nf
  218. \fL+\fR,\fL-\fR max(\fIsa,sb\fR)
  219. \fL*\fR min(\fIsa\fR+\fIsb \fR, max\fR(\fIs,sa,sb\fR))
  220. \fL/\fI s
  221. \fL%\fR so that dividend = divisor*quotient + remainder; remainder has sign of dividend
  222. \fL^\fR min(\fIsa\fR\(mu|\fIb\fR|, max(\fIs,sa\fR))
  223. \fLv\fR max(\fIs,sa\fR)
  224. .fi
  225. .SH EXAMPLES
  226. .EX
  227. [la1+dsa*pla10>y]sy
  228. 0sa1
  229. lyx
  230. .EE
  231. .ns
  232. .IP
  233. Print the first ten values of
  234. .IR n !
  235. .SH SOURCE
  236. .B /sys/src/cmd/dc.c
  237. .SH "SEE ALSO"
  238. .IR bc (1),
  239. .IR hoc (1)
  240. .SH DIAGNOSTICS
  241. .I x
  242. .LR "is unimplemented" ,
  243. where
  244. .I x
  245. is an octal number: an internal error.
  246. .br
  247. `Out of headers'
  248. for too many numbers being kept around.
  249. .br
  250. `Nesting depth'
  251. for too many levels of nested execution.
  252. .SH BUGS
  253. When the input base exceeds 16,
  254. there is no notation for digits greater than
  255. .BR F .
  256. .PP
  257. Past its time.