bc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. .TH BC 1
  2. .SH NAME
  3. bc \- arbitrary-precision arithmetic language
  4. .SH SYNOPSIS
  5. .B bc
  6. [
  7. .B -cdls
  8. ]
  9. [
  10. .I file ...
  11. ]
  12. .SH DESCRIPTION
  13. .I Bc
  14. is an interactive processor for a language that resembles
  15. C but provides arithmetic on numbers of arbitrary length with up
  16. to 100 digits right of the decimal point.
  17. It takes input from any files given, then reads
  18. the standard input.
  19. .PP
  20. The
  21. .B -d
  22. option enables debugging output.
  23. The
  24. .B -l
  25. option stands for the name
  26. of an arbitrary precision math library.
  27. The
  28. .B -s
  29. option suppresses the automatic display
  30. of calculation results; all output is via the
  31. .B print
  32. command.
  33. .PP
  34. The following syntax for
  35. .I bc
  36. programs is like that of C;
  37. .I L
  38. means letter
  39. .BR a - z ,
  40. .I E
  41. means expression,
  42. .I S
  43. means statement.
  44. .TF length(E)
  45. .TP
  46. Lexical
  47. .RS
  48. .HP
  49. comments are enclosed in
  50. .B /* */
  51. .HP
  52. newlines end statements
  53. .RE
  54. .TP
  55. Names
  56. .IP
  57. simple variables:
  58. .I L
  59. .br
  60. array elements:
  61. .IB L [ E ]
  62. .br
  63. The words
  64. .BR ibase ,
  65. .BR obase ,
  66. and
  67. .B scale
  68. .TP
  69. Other operands
  70. .IP
  71. arbitrarily long numbers with optional sign and decimal point.
  72. .RS
  73. .TP
  74. .BI ( E )
  75. .TP
  76. .BI sqrt( E )
  77. .TP
  78. .BI length( E )
  79. number of significant decimal digits
  80. .TP
  81. .BI scale( E )
  82. number of digits right of decimal point
  83. .TP
  84. .IB L ( E , ... ,\fIE\fP)
  85. function call
  86. .RE
  87. .TP
  88. Operators
  89. .RS
  90. .HP
  91. .B "+ - * / % ^\ "
  92. .RB ( %
  93. is remainder;
  94. .B ^
  95. is power)
  96. .HP
  97. .B "++ --\ "
  98. .TP
  99. .B "== <= >= != < >"
  100. .TP
  101. .B "= += -= *= /= %= ^="
  102. .RE
  103. .TP
  104. Statements
  105. .RS
  106. .br
  107. .I E
  108. .br
  109. .B {
  110. .I S
  111. .B ;
  112. \&...
  113. .B ;
  114. .I S
  115. .B }
  116. .br
  117. .B "print"
  118. .I E
  119. .br
  120. .B "if ("
  121. .I E
  122. .B )
  123. .I S
  124. .br
  125. .B "while ("
  126. .I E
  127. .B )
  128. .I S
  129. .br
  130. .B "for ("
  131. .I E
  132. .B ;
  133. .I E
  134. .B ;
  135. .I E
  136. .B ")"
  137. .I S
  138. .br
  139. null statement
  140. .br
  141. .B break
  142. .br
  143. .B quit
  144. .br
  145. \fL"\fRtext\fL"\fR
  146. .RE
  147. .TP
  148. Function definitions
  149. .RS
  150. .br
  151. .B define
  152. .I L
  153. .B (
  154. .I L
  155. .B ,
  156. \&...
  157. .B ,
  158. .I L
  159. .B ){
  160. .PD0
  161. .br
  162. .B auto
  163. .I L
  164. .B ,
  165. \&...
  166. .B ,
  167. .I L
  168. .br
  169. .I S
  170. .B ;
  171. \&...
  172. .B ;
  173. .I S
  174. .br
  175. .B return
  176. .I E
  177. .LP
  178. .B }
  179. .RE
  180. .TP
  181. Functions in
  182. .B -l
  183. math library
  184. .RS
  185. .TP
  186. .BI s( x )
  187. sine
  188. .TP
  189. .BI c( x )
  190. cosine
  191. .TP
  192. .BI e( x )
  193. exponential
  194. .TP
  195. .BI l( x )
  196. log
  197. .TP
  198. .BI a( x )
  199. arctangent
  200. .TP
  201. .BI j( "n, x" )
  202. Bessel function
  203. .RE
  204. .PP
  205. .DT
  206. All function arguments are passed by value.
  207. .PD
  208. .PP
  209. The value of an expression at the top level is printed
  210. unless the main operator is an assignment or the
  211. .B -s
  212. command line argument is given.
  213. Text in quotes, which may include newlines, is always printed.
  214. Either semicolons or newlines may separate statements.
  215. Assignment to
  216. .B scale
  217. influences the number of digits to be retained on arithmetic
  218. operations in the manner of
  219. .IR dc (1).
  220. Assignments to
  221. .B ibase
  222. or
  223. .B obase
  224. set the input and output number radix respectively.
  225. .PP
  226. The same letter may be used as an array, a function,
  227. and a simple variable simultaneously.
  228. All variables are global to the program.
  229. Automatic variables are pushed down during function calls.
  230. In a declaration of an array as a function argument
  231. or automatic variable
  232. empty square brackets must follow the array name.
  233. .PP
  234. .I Bc
  235. is actually a preprocessor for
  236. .IR dc (1),
  237. which it invokes automatically, unless the
  238. .B -c
  239. (compile only)
  240. option is present.
  241. In this case the
  242. .I dc
  243. input is sent to the standard output instead.
  244. .SH EXAMPLE
  245. Define a function to compute an approximate value of
  246. the exponential.
  247. Use it to print 10 values.
  248. (The exponential function in the library gives better answers.)
  249. .PP
  250. .EX
  251. scale = 20
  252. define e(x) {
  253. auto a, b, c, i, s
  254. a = 1
  255. b = 1
  256. s = 1
  257. for(i=1; 1; i++) {
  258. a *= x
  259. b *= i
  260. c = a/b
  261. if(c == 0) return s
  262. s += c
  263. }
  264. }
  265. for(i=1; i<=10; i++) print e(i)
  266. .EE
  267. .SH FILES
  268. .B /sys/lib/bclib
  269. mathematical library
  270. .SH SOURCE
  271. .B /sys/src/cmd/bc.y
  272. .SH "SEE ALSO"
  273. .IR dc (1),
  274. .IR hoc (1)
  275. .SH BUGS
  276. No
  277. .LR && ,
  278. .LR || ,
  279. or
  280. .L !
  281. operators.
  282. .PP
  283. A
  284. .L for
  285. statement must have all three
  286. .LR E s.
  287. .PP
  288. A
  289. .L quit
  290. is interpreted when read, not when executed.