bc 3.7 KB

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