logo

oasis-root

Compiled tree of Oasis Linux based on own branch at <https://hacktivis.me/git/oasis/> git clone https://anongit.hacktivis.me/git/oasis-root.git

bc.1 (35704B)


  1. .\"
  2. .\" bc.1 - the *roff document processor source for the bc manual
  3. .\"
  4. .\" This file is part of GNU bc.
  5. .\" Copyright (C) 1991-1994, 1997, 2000, 2003, 2006, 2017 Free Software Foundation, Inc.
  6. .\"
  7. .\" This program is free software; you can redistribute it and/or modify
  8. .\" it under the terms of the GNU General Public License as published by
  9. .\" the Free Software Foundation; either version 2 of the License , or
  10. .\" (at your option) any later version.
  11. .\"
  12. .\" This program is distributed in the hope that it will be useful,
  13. .\" but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. .\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. .\" GNU General Public License for more details.
  16. .\"
  17. .\" You should have received a copy of the GNU General Public License
  18. .\" along with this program; see the file COPYING. If not, write to:
  19. .\" The Free Software Foundation, Inc.
  20. .\" 51 Franklin Street, Fifth Floor
  21. .\" Boston, MA 02110-1301 USA
  22. .\"
  23. .\" You may contact the author by:
  24. .\" e-mail: philnelson@acm.org
  25. .\" us-mail: Philip A. Nelson
  26. .\" Computer Science Department, 9062
  27. .\" Western Washington University
  28. .\" Bellingham, WA 98226-9062
  29. .\"
  30. .\"
  31. .TH bc 1 "2006-06-11" "GNU Project"
  32. .SH NAME
  33. bc - An arbitrary precision calculator language
  34. .SH SYNTAX
  35. \fBbc\fR [ \fB-hlwsqv\fR ] [long-options] [ \fI file ...\fR ]
  36. .SH DESCRIPTION
  37. \fBbc\fR is a language that supports arbitrary precision numbers
  38. with interactive execution of statements. There are some similarities
  39. in the syntax to the C programming language.
  40. A standard math library is available by command line option.
  41. If requested, the math library is defined before processing any files.
  42. \fBbc\fR starts by processing code from all the files listed
  43. on the command line in the order listed. After all files have been
  44. processed, \fBbc\fR reads from the standard input. All code is
  45. executed as it is read. (If a file contains a command to halt the
  46. processor, \fBbc\fR will never read from the standard input.)
  47. .PP
  48. This version of \fBbc\fR contains several extensions beyond
  49. traditional \fBbc\fR implementations and the POSIX draft standard.
  50. Command line options can cause these extensions to print a warning
  51. or to be rejected. This
  52. document describes the language accepted by this processor.
  53. Extensions will be identified as such.
  54. .SS OPTIONS
  55. .IP "-h, --help"
  56. Print the usage and exit.
  57. .IP "-i, --interactive"
  58. Force interactive mode.
  59. .IP "-l, --mathlib"
  60. Define the standard math library.
  61. .IP "-w, --warn"
  62. Give warnings for extensions to POSIX \fBbc\fR.
  63. .IP "-s, --standard"
  64. Process exactly the POSIX \fBbc\fR language.
  65. .IP "-q, --quiet"
  66. Do not print the normal GNU bc welcome.
  67. .IP "-v, --version"
  68. Print the version number and copyright and quit.
  69. .SS NUMBERS
  70. The most basic element in \fBbc\fR is the number. Numbers are
  71. arbitrary precision numbers. This precision is both in the integer
  72. part and the fractional part. All numbers are represented internally
  73. in decimal and all computation is done in decimal. (This version
  74. truncates results from divide and multiply operations.) There are two
  75. attributes of numbers, the length and the scale. The length is the
  76. total number of decimal digits used by \fBbc\fR to represent a number
  77. and the scale is the total number of decimal digits after the decimal
  78. point. For example:
  79. .nf
  80. .RS
  81. .000001 has a length of 6 and scale of 6.
  82. 1935.000 has a length of 7 and a scale of 3.
  83. .RE
  84. .fi
  85. .SS VARIABLES
  86. Numbers are stored in two types of variables, simple variables and
  87. arrays. Both simple variables and array variables are named. Names
  88. begin with a letter followed by any number of letters, digits and
  89. underscores. All letters must be lower case. (Full alpha-numeric
  90. names are an extension. In POSIX \fBbc\fR all names are a single
  91. lower case letter.) The type of variable is clear by the context
  92. because all array variable names will be followed by brackets ([]).
  93. .PP
  94. There are four special variables, \fBscale, ibase, obase,\fR and
  95. \fBlast\fR. \fBscale\fR defines how some operations use digits after the
  96. decimal point. The default value of \fBscale\fR is 0. \fBibase\fR
  97. and \fBobase\fR define the conversion base for input and output
  98. numbers. The default for both input and output is base 10.
  99. \fBlast\fR (an extension) is a variable that has the value of the last
  100. printed number. These will be discussed in further detail where
  101. appropriate. All of these variables may have values assigned to them
  102. as well as used in expressions.
  103. .SS COMMENTS
  104. Comments in \fBbc\fR start with the characters \fB/*\fR and end with
  105. the characters \fB*/\fR. Comments may start anywhere and appear as a
  106. single space in the input. (This causes comments to delimit other
  107. input items. For example, a comment can not be found in the middle of
  108. a variable name.) Comments include any newlines (end of line) between
  109. the start and the end of the comment.
  110. .PP
  111. To support the use of scripts for \fBbc\fR, a single line comment has been
  112. added as an extension. A single line comment starts at a \fB#\fR
  113. character and continues to the next end of the line. The end of line
  114. character is not part of the comment and is processed normally.
  115. .SS EXPRESSIONS
  116. The numbers are manipulated by expressions and statements. Since
  117. the language was designed to be interactive, statements and expressions
  118. are executed as soon as possible. There is no "main" program. Instead,
  119. code is executed as it is encountered. (Functions, discussed in
  120. detail later, are defined when encountered.)
  121. .PP
  122. A simple expression is just a constant. \fBbc\fR converts constants
  123. into internal decimal numbers using the current input base, specified
  124. by the variable \fBibase\fR. (There is an exception in functions.)
  125. The legal values of \fBibase\fR are 2 through 36. (Bases greater than
  126. 16 are an extension.) Assigning a value outside this range to
  127. \fBibase\fR will result in a value of 2 or 36. Input numbers may
  128. contain the characters 0-9 and A-Z. (Note: They must be capitals.
  129. Lower case letters are variable names.) Single digit numbers always
  130. have the value of the digit regardless of the value of
  131. \fBibase\fR. (i.e. A = 10.) For multi-digit numbers, \fBbc\fR changes
  132. all input digits greater or equal to ibase to the value of
  133. \fBibase\fR-1. This makes the number \fBZZZ\fR always be the largest
  134. 3 digit number of the input base.
  135. .PP
  136. Full expressions are similar to many other high level languages.
  137. Since there is only one kind of number, there are no rules for mixing
  138. types. Instead, there are rules on the scale of expressions. Every
  139. expression has a scale. This is derived from the scale of original
  140. numbers, the operation performed and in many cases, the value of the
  141. variable \fBscale\fR. Legal values of the variable \fBscale\fR are
  142. 0 to the maximum number representable by a C integer.
  143. .PP
  144. In the following descriptions of legal expressions, "expr" refers to a
  145. complete expression and "var" refers to a simple or an array variable.
  146. A simple variable is just a
  147. .RS
  148. \fIname\fR
  149. .RE
  150. and an array variable is specified as
  151. .RS
  152. \fIname\fR[\fIexpr\fR]
  153. .RE
  154. Unless specifically
  155. mentioned the scale of the result is the maximum scale of the
  156. expressions involved.
  157. .IP "- expr"
  158. The result is the negation of the expression.
  159. .IP "++ var"
  160. The variable is incremented by one and the new value is the result of
  161. the expression.
  162. .IP "-- var"
  163. The variable
  164. is decremented by one and the new value is the result of the
  165. expression.
  166. .IP "var ++"
  167. The result of the expression is the value of
  168. the variable and then the variable is incremented by one.
  169. .IP "var --"
  170. The result of the expression is the value of the variable and then
  171. the variable is decremented by one.
  172. .IP "expr + expr"
  173. The result of the expression is the sum of the two expressions.
  174. .IP "expr - expr"
  175. The result of the expression is the difference of the two expressions.
  176. .IP "expr * expr"
  177. The result of the expression is the product of the two expressions.
  178. .IP "expr / expr"
  179. The result of the expression is the quotient of the two expressions.
  180. The scale of the result is the value of the variable \fBscale\fR.
  181. .IP "expr % expr"
  182. The result of the expression is the "remainder" and it is computed in the
  183. following way. To compute a%b, first a/b is computed to \fBscale\fR
  184. digits. That result is used to compute a-(a/b)*b to the scale of the
  185. maximum of \fBscale\fR+scale(b) and scale(a). If \fBscale\fR is set
  186. to zero and both expressions are integers this expression is the
  187. integer remainder function.
  188. .IP "expr ^ expr"
  189. The result of the expression is the value of the first raised to the
  190. second. The second expression must be an integer. (If the second
  191. expression is not an integer, a warning is generated and the
  192. expression is truncated to get an integer value.) The scale of the
  193. result is \fBscale\fR if the exponent is negative. If the exponent
  194. is positive the scale of the result is the minimum of the scale of the
  195. first expression times the value of the exponent and the maximum of
  196. \fBscale\fR and the scale of the first expression. (e.g. scale(a^b)
  197. = min(scale(a)*b, max( \fBscale,\fR scale(a))).) It should be noted
  198. that expr^0 will always return the value of 1.
  199. .IP "( expr )"
  200. This alters the standard precedence to force the evaluation of the
  201. expression.
  202. .IP "var = expr"
  203. The variable is assigned the value of the expression.
  204. .IP "var <op>= expr"
  205. This is equivalent to "var = var <op> expr" with the exception that
  206. the "var" part is evaluated only once. This can make a difference if
  207. "var" is an array.
  208. .PP
  209. Relational expressions are a special kind of expression
  210. that always evaluate to 0 or 1, 0 if the relation is false and 1 if
  211. the relation is true. These may appear in any legal expression.
  212. (POSIX bc requires that relational expressions are used only in if,
  213. while, and for statements and that only one relational test may be
  214. done in them.) The relational operators are
  215. .IP "expr1 < expr2"
  216. The result is 1 if expr1 is strictly less than expr2.
  217. .IP "expr1 <= expr2"
  218. The result is 1 if expr1 is less than or equal to expr2.
  219. .IP "expr1 > expr2"
  220. The result is 1 if expr1 is strictly greater than expr2.
  221. .IP "expr1 >= expr2"
  222. The result is 1 if expr1 is greater than or equal to expr2.
  223. .IP "expr1 == expr2"
  224. The result is 1 if expr1 is equal to expr2.
  225. .IP "expr1 != expr2"
  226. The result is 1 if expr1 is not equal to expr2.
  227. .PP
  228. Boolean operations are also legal. (POSIX \fBbc\fR does NOT have
  229. boolean operations). The result of all boolean operations are 0 and 1
  230. (for false and true) as in relational expressions. The boolean
  231. operators are:
  232. .IP "!expr"
  233. The result is 1 if expr is 0.
  234. .IP "expr && expr"
  235. The result is 1 if both expressions are non-zero.
  236. .IP "expr || expr"
  237. The result is 1 if either expression is non-zero.
  238. .PP
  239. The expression precedence is as follows: (lowest to highest)
  240. .nf
  241. .RS
  242. || operator, left associative
  243. && operator, left associative
  244. ! operator, nonassociative
  245. Relational operators, left associative
  246. Assignment operator, right associative
  247. + and - operators, left associative
  248. *, / and % operators, left associative
  249. ^ operator, right associative
  250. unary - operator, nonassociative
  251. ++ and -- operators, nonassociative
  252. .RE
  253. .fi
  254. .PP
  255. This precedence was chosen so that POSIX compliant \fBbc\fR programs
  256. will run correctly. This will cause the use of the relational and
  257. logical operators to have some unusual behavior when used with
  258. assignment expressions. Consider the expression:
  259. .RS
  260. a = 3 < 5
  261. .RE
  262. .PP
  263. Most C programmers would assume this would assign the result of "3 <
  264. 5" (the value 1) to the variable "a". What this does in \fBbc\fR is
  265. assign the value 3 to the variable "a" and then compare 3 to 5. It is
  266. best to use parenthesis when using relational and logical operators
  267. with the assignment operators.
  268. .PP
  269. There are a few more special expressions that are provided in \fBbc\fR.
  270. These have to do with user defined functions and standard
  271. functions. They all appear as "\fIname\fB(\fIparameters\fB)\fR".
  272. See the section on functions for user defined functions. The standard
  273. functions are:
  274. .IP "length ( expression )"
  275. The value of the length function is the number of significant digits in the
  276. expression.
  277. .IP "read ( )"
  278. The read function (an extension) will read a number from the standard
  279. input, regardless of where the function occurs. Beware, this can
  280. cause problems with the mixing of data and program in the standard input.
  281. The best use for this function is in a previously written program that
  282. needs input from the user, but never allows program code to be input
  283. from the user. The value of the read function is the number read from
  284. the standard input using the current value of the variable
  285. \fBibase\fR for the conversion base.
  286. .IP "scale ( expression )"
  287. The value of the scale function is the number of digits after the decimal
  288. point in the expression.
  289. .IP "sqrt ( expression )"
  290. The value of the sqrt function is the square root of the expression. If
  291. the expression is negative, a run time error is generated.
  292. .SS STATEMENTS
  293. Statements (as in most algebraic languages) provide the sequencing of
  294. expression evaluation. In \fBbc\fR statements are executed "as soon
  295. as possible." Execution happens when a newline in encountered and
  296. there is one or more complete statements. Due to this immediate
  297. execution, newlines are very important in \fBbc\fR. In fact, both a
  298. semicolon and a newline are used as statement separators. An
  299. improperly placed newline will cause a syntax error. Because newlines
  300. are statement separators, it is possible to hide a newline by using
  301. the backslash character. The sequence "\e<nl>", where <nl> is the
  302. newline appears to \fBbc\fR as whitespace instead of a newline. A
  303. statement list is a series of statements separated by semicolons and
  304. newlines. The following is a list of \fBbc\fR statements and what
  305. they do: (Things enclosed in brackets ([]) are optional parts of the
  306. statement.)
  307. .IP "expression"
  308. This statement does one of two things. If the expression starts with
  309. "<variable> <assignment> ...", it is considered to be an assignment
  310. statement. If the expression is not an assignment statement, the
  311. expression is evaluated and printed to the output. After the number
  312. is printed, a newline is printed. For example, "a=1" is an assignment
  313. statement and "(a=1)" is an expression that has an embedded
  314. assignment. All numbers that are printed are printed in the base
  315. specified by the variable \fBobase\fR. The legal values for \fB
  316. obase\fR are 2 through BC_BASE_MAX. (See the section LIMITS.) For
  317. bases 2 through 16, the usual method of writing numbers is used. For
  318. bases greater than 16, \fBbc\fR uses a multi-character digit method
  319. of printing the numbers where each higher base digit is printed as a
  320. base 10 number. The multi-character digits are separated by spaces.
  321. Each digit contains the number of characters required to represent the
  322. base ten value of "obase-1". Since numbers are of arbitrary
  323. precision, some numbers may not be printable on a single output line.
  324. These long numbers will be split across lines using the "\e" as the
  325. last character on a line. The maximum number of characters printed
  326. per line is 70. Due to the interactive nature of \fBbc\fR, printing
  327. a number causes the side effect of assigning the printed value to the
  328. special variable \fBlast\fR. This allows the user to recover the
  329. last value printed without having to retype the expression that
  330. printed the number. Assigning to \fBlast\fR is legal and will
  331. overwrite the last printed value with the assigned value. The newly
  332. assigned value will remain until the next number is printed or another
  333. value is assigned to \fBlast\fR. (Some installations may allow the
  334. use of a single period (.) which is not part of a number as a short
  335. hand notation for for \fBlast\fR.)
  336. .IP "string"
  337. The string is printed to the output. Strings start with a double quote
  338. character and contain all characters until the next double quote character.
  339. All characters are take literally, including any newline. No newline
  340. character is printed after the string.
  341. .IP "\fBprint\fR list"
  342. The print statement (an extension) provides another method of output.
  343. The "list" is a list of strings and expressions separated by commas.
  344. Each string or expression is printed in the order of the list. No
  345. terminating newline is printed. Expressions are evaluated and their
  346. value is printed and assigned to the variable \fBlast\fR. Strings
  347. in the print statement are printed to the output and may contain
  348. special characters. Special characters start with the backslash
  349. character (\e). The special characters recognized by \fBbc\fR are
  350. "a" (alert or bell), "b" (backspace), "f" (form feed), "n" (newline),
  351. "r" (carriage return), "q" (double quote), "t" (tab), and "\e" (backslash).
  352. Any other character following the backslash will be ignored.
  353. .IP "{ statement_list }"
  354. This is the compound statement. It allows multiple statements to be
  355. grouped together for execution.
  356. .IP "\fBif\fR ( expression ) statement1 [\fBelse\fR statement2]"
  357. The if statement evaluates the expression and executes statement1 or
  358. statement2 depending on the value of the expression. If the expression
  359. is non-zero, statement1 is executed. If statement2 is present and
  360. the value of the expression is 0, then statement2 is executed. (The
  361. else clause is an extension.)
  362. .IP "\fBwhile\fR ( expression ) statement"
  363. The while statement will execute the statement while the expression
  364. is non-zero. It evaluates the expression before each execution of
  365. the statement. Termination of the loop is caused by a zero
  366. expression value or the execution of a break statement.
  367. .IP "\fBfor\fR ( [expression1] ; [expression2] ; [expression3] ) statement"
  368. The for statement controls repeated execution of the statement.
  369. Expression1 is evaluated before the loop. Expression2 is evaluated
  370. before each execution of the statement. If it is non-zero, the statement
  371. is evaluated. If it is zero, the loop is terminated. After each
  372. execution of the statement, expression3 is evaluated before the reevaluation
  373. of expression2. If expression1 or expression3 are missing, nothing is
  374. evaluated at the point they would be evaluated.
  375. If expression2 is missing, it is the same as substituting
  376. the value 1 for expression2. (The optional expressions are an
  377. extension. POSIX \fBbc\fR requires all three expressions.)
  378. The following is equivalent code for the for statement:
  379. .nf
  380. .RS
  381. expression1;
  382. while (expression2) {
  383. statement;
  384. expression3;
  385. }
  386. .RE
  387. .fi
  388. .IP "\fBbreak\fR"
  389. This statement causes a forced exit of the most recent enclosing while
  390. statement or for statement.
  391. .IP "\fBcontinue\fR"
  392. The continue statement (an extension) causes the most recent enclosing
  393. for statement to start the next iteration.
  394. .IP "\fBhalt\fR"
  395. The halt statement (an extension) is an executed statement that causes
  396. the \fBbc\fR processor to quit only when it is executed. For example,
  397. "if (0 == 1) halt" will not cause \fBbc\fR to terminate because the halt is
  398. not executed.
  399. .IP "\fBreturn\fR"
  400. Return the value 0 from a function. (See the section on functions.)
  401. .IP "\fBreturn\fR ( expression )"
  402. Return the value of the expression from a function. (See the section on
  403. functions.) As an extension, the parenthesis are not required.
  404. .SS PSEUDO STATEMENTS
  405. These statements are not statements in the traditional sense. They are
  406. not executed statements. Their function is performed at "compile" time.
  407. .IP "\fBlimits\fR"
  408. Print the local limits enforced by the local version of \fBbc\fR. This
  409. is an extension.
  410. .IP "\fBquit\fR"
  411. When the quit statement is read, the \fBbc\fR processor
  412. is terminated, regardless of where the quit statement is found. For
  413. example, "if (0 == 1) quit" will cause \fBbc\fR to terminate.
  414. .IP "\fBwarranty\fR"
  415. Print a longer warranty notice. This is an extension.
  416. .SS FUNCTIONS
  417. Functions provide a method of defining a computation that can be executed
  418. later. Functions in
  419. .B bc
  420. always compute a value and return it to the caller. Function definitions
  421. are "dynamic" in the sense that a function is undefined until a definition
  422. is encountered in the input. That definition is then used until another
  423. definition function for the same name is encountered. The new definition
  424. then replaces the older definition. A function is defined as follows:
  425. .nf
  426. .RS
  427. \fBdefine \fIname \fB( \fIparameters \fB) { \fInewline
  428. \fI auto_list statement_list \fB}\fR
  429. .RE
  430. .fi
  431. A function call is just an expression of the form
  432. "\fIname\fB(\fIparameters\fB)\fR".
  433. .PP
  434. Parameters are numbers or arrays (an extension). In the function definition,
  435. zero or more parameters are defined by listing their names separated by
  436. commas. All parameters are call by value parameters.
  437. Arrays are specified in the parameter definition by
  438. the notation "\fIname\fB[]\fR". In the function call, actual parameters
  439. are full expressions for number parameters. The same notation is used
  440. for passing arrays as for defining array parameters. The named array is
  441. passed by value to the function. Since function definitions are dynamic,
  442. parameter numbers and types are checked when a function is called. Any
  443. mismatch in number or types of parameters will cause a runtime error.
  444. A runtime error will also occur for the call to an undefined function.
  445. .PP
  446. The \fIauto_list\fR is an optional list of variables that are for
  447. "local" use. The syntax of the auto list (if present) is "\fBauto
  448. \fIname\fR, ... ;". (The semicolon is optional.) Each \fIname\fR is
  449. the name of an auto variable. Arrays may be specified by using the
  450. same notation as used in parameters. These variables have their
  451. values pushed onto a stack at the start of the function. The
  452. variables are then initialized to zero and used throughout the
  453. execution of the function. At function exit, these variables are
  454. popped so that the original value (at the time of the function call)
  455. of these variables are restored. The parameters are really auto
  456. variables that are initialized to a value provided in the function
  457. call. Auto variables are different than traditional local variables
  458. because if function A calls function B, B may access function
  459. A's auto variables by just using the same name, unless function B has
  460. called them auto variables. Due to the fact that auto variables and
  461. parameters are pushed onto a stack, \fBbc\fR supports recursive functions.
  462. .PP
  463. The function body is a list of \fBbc\fR statements. Again, statements
  464. are separated by semicolons or newlines. Return statements cause the
  465. termination of a function and the return of a value. There are two
  466. versions of the return statement. The first form, "\fBreturn\fR", returns
  467. the value 0 to the calling expression. The second form,
  468. "\fBreturn ( \fIexpression \fB)\fR", computes the value of the expression
  469. and returns that value to the calling expression. There is an implied
  470. "\fBreturn (0)\fR" at the end of every function. This allows a function
  471. to terminate and return 0 without an explicit return statement.
  472. .PP
  473. Functions also change the usage of the variable \fBibase\fR. All
  474. constants in the function body will be converted using the value of
  475. \fBibase\fR at the time of the function call. Changes of \fBibase\fR
  476. will be ignored during the execution of the function except for the
  477. standard function \fBread\fR, which will always use the current value
  478. of \fBibase\fR for conversion of numbers.
  479. .PP
  480. Several extensions have been added to functions. First, the format of
  481. the definition has been slightly relaxed. The standard requires the
  482. opening brace be on the same line as the \fBdefine\fR keyword and all
  483. other parts must be on following lines. This version of \fBbc\fR will
  484. allow any number of newlines before and after the opening brace of the
  485. function. For example, the following definitions are legal.
  486. .nf
  487. .RS
  488. \f(CW
  489. define d (n) { return (2*n); }
  490. define d (n)
  491. { return (2*n); }
  492. \fR
  493. .RE
  494. .fi
  495. .PP
  496. Functions may be defined as \fBvoid\fR. A void
  497. funtion returns no value and thus may not be used in any place that needs
  498. a value. A void function does not produce any output when called by itself
  499. on an input line. The key word \fBvoid\fR is placed between the key word
  500. \fBdefine\fR and the function name. For example, consider the following
  501. session.
  502. .nf
  503. .RS
  504. \f(CW
  505. define py (y) { print "--->", y, "<---", "\en"; }
  506. define void px (x) { print "--->", x, "<---", "\en"; }
  507. py(1)
  508. --->1<---
  509. 0
  510. px(1)
  511. --->1<---
  512. \fR
  513. .RE
  514. .fi
  515. Since \fBpy\fR is not a void function, the call of \fBpy(1)\fR prints
  516. the desired output and then prints a second line that is the value of
  517. the function. Since the value of a function that is not given an
  518. explicit return statement is zero, the zero is printed. For \fBpx(1)\fR,
  519. no zero is printed because the function is a void function.
  520. .PP
  521. Also, call by variable for arrays was added. To declare
  522. a call by variable array, the declaration of the array parameter in the
  523. function definition looks like "\fI*name\fB[]\fR". The call to the
  524. function remains the same as call by value arrays.
  525. .SS MATH LIBRARY
  526. If \fBbc\fR is invoked with the \fB-l\fR option, a math library is preloaded
  527. and the default scale is set to 20. The math functions will calculate their
  528. results to the scale set at the time of their call.
  529. The math library defines the following functions:
  530. .IP "s (\fIx\fR)"
  531. The sine of x, x is in radians.
  532. .IP "c (\fIx\fR)"
  533. The cosine of x, x is in radians.
  534. .IP "a (\fIx\fR)"
  535. The arctangent of x, arctangent returns radians.
  536. .IP "l (\fIx\fR)"
  537. The natural logarithm of x.
  538. .IP "e (\fIx\fR)"
  539. The exponential function of raising e to the value x.
  540. .IP "j (\fIn,x\fR)"
  541. The Bessel function of integer order n of x.
  542. .SS EXAMPLES
  543. In /bin/sh, the following will assign the value of "pi" to the shell
  544. variable \fBpi\fR.
  545. .RS
  546. \f(CW
  547. pi=$(echo "scale=10; 4*a(1)" | bc -l)
  548. \fR
  549. .RE
  550. .PP
  551. The following is the definition of the exponential function used in the
  552. math library. This function is written in POSIX \fBbc\fR.
  553. .nf
  554. .RS
  555. \f(CW
  556. scale = 20
  557. /* Uses the fact that e^x = (e^(x/2))^2
  558. When x is small enough, we use the series:
  559. e^x = 1 + x + x^2/2! + x^3/3! + ...
  560. */
  561. define e(x) {
  562. auto a, d, e, f, i, m, v, z
  563. /* Check the sign of x. */
  564. if (x<0) {
  565. m = 1
  566. x = -x
  567. }
  568. /* Precondition x. */
  569. z = scale;
  570. scale = 4 + z + .44*x;
  571. while (x > 1) {
  572. f += 1;
  573. x /= 2;
  574. }
  575. /* Initialize the variables. */
  576. v = 1+x
  577. a = x
  578. d = 1
  579. for (i=2; 1; i++) {
  580. e = (a *= x) / (d *= i)
  581. if (e == 0) {
  582. if (f>0) while (f--) v = v*v;
  583. scale = z
  584. if (m) return (1/v);
  585. return (v/1);
  586. }
  587. v += e
  588. }
  589. }
  590. \fR
  591. .RE
  592. .fi
  593. .PP
  594. The following is code that uses the extended features of \fBbc\fR to
  595. implement a simple program for calculating checkbook balances. This
  596. program is best kept in a file so that it can be used many times
  597. without having to retype it at every use.
  598. .nf
  599. .RS
  600. \f(CW
  601. scale=2
  602. print "\enCheck book program!\en"
  603. print " Remember, deposits are negative transactions.\en"
  604. print " Exit by a 0 transaction.\en\en"
  605. print "Initial balance? "; bal = read()
  606. bal /= 1
  607. print "\en"
  608. while (1) {
  609. "current balance = "; bal
  610. "transaction? "; trans = read()
  611. if (trans == 0) break;
  612. bal -= trans
  613. bal /= 1
  614. }
  615. quit
  616. \fR
  617. .RE
  618. .fi
  619. .PP
  620. The following is the definition of the recursive factorial function.
  621. .nf
  622. .RS
  623. \f(CW
  624. define f (x) {
  625. if (x <= 1) return (1);
  626. return (f(x-1) * x);
  627. }
  628. \fR
  629. .RE
  630. .fi
  631. .SS READLINE AND LIBEDIT OPTIONS
  632. GNU \fBbc\fR can be compiled (via a configure option) to use the GNU
  633. \fBreadline\fR input editor library or the BSD \fBlibedit\fR library.
  634. This allows the user to do editing of lines before sending them
  635. to \fBbc\fR. It also allows for a history of previous lines typed.
  636. When this option is selected, \fBbc\fR has one more special variable.
  637. This special variable, \fBhistory\fR is the number of lines of history
  638. retained. For \fBreadline\fR, a value of -1 means that an unlimited
  639. number of history lines are retained. Setting the value of
  640. \fBhistory\fR to a positive number restricts the number of history
  641. lines to the number given. The value of 0 disables the history
  642. feature. The default value is 100. For more information, read the
  643. user manuals for the GNU \fBreadline\fR, \fBhistory\fR and BSD \fBlibedit\fR
  644. libraries. One can not enable both \fBreadline\fR and \fBlibedit\fR
  645. at the same time.
  646. .SS DIFFERENCES
  647. This version of
  648. .B bc
  649. was implemented from the POSIX P1003.2/D11 draft and contains
  650. several differences and extensions relative to the draft and
  651. traditional implementations.
  652. It is not implemented in the traditional way using
  653. .I dc(1).
  654. This version is a single process which parses and runs a byte code
  655. translation of the program. There is an "undocumented" option (-c)
  656. that causes the program to output the byte code to
  657. the standard output instead of running it. It was mainly used for
  658. debugging the parser and preparing the math library.
  659. .PP
  660. A major source of differences is
  661. extensions, where a feature is extended to add more functionality and
  662. additions, where new features are added.
  663. The following is the list of differences and extensions.
  664. .IP "LANG environment"
  665. This version does not conform to the POSIX standard in the processing
  666. of the LANG environment variable and all environment variables starting
  667. with LC_.
  668. .IP "names"
  669. Traditional and POSIX
  670. .B bc
  671. have single letter names for functions, variables and arrays. They have
  672. been extended to be multi-character names that start with a letter and
  673. may contain letters, numbers and the underscore character.
  674. .IP "Strings"
  675. Strings are not allowed to contain NUL characters. POSIX says all characters
  676. must be included in strings.
  677. .IP "last"
  678. POSIX \fBbc\fR does not have a \fBlast\fR variable. Some implementations
  679. of \fBbc\fR use the period (.) in a similar way.
  680. .IP "comparisons"
  681. POSIX \fBbc\fR allows comparisons only in the if statement, the while
  682. statement, and the second expression of the for statement. Also, only
  683. one relational operation is allowed in each of those statements.
  684. .IP "if statement, else clause"
  685. POSIX \fBbc\fR does not have an else clause.
  686. .IP "for statement"
  687. POSIX \fBbc\fR requires all expressions to be present in the for statement.
  688. .IP "&&, ||, !"
  689. POSIX \fBbc\fR does not have the logical operators.
  690. .IP "read function"
  691. POSIX \fBbc\fR does not have a read function.
  692. .IP "print statement"
  693. POSIX \fBbc\fR does not have a print statement .
  694. .IP "continue statement"
  695. POSIX \fBbc\fR does not have a continue statement.
  696. .IP "return statement"
  697. POSIX \fBbc\fR requires parentheses around the return expression.
  698. .IP "array parameters"
  699. POSIX \fBbc\fR does not (currently) support array parameters in full.
  700. The POSIX grammar allows for arrays in function definitions, but does
  701. not provide a method to specify an array as an actual parameter. (This
  702. is most likely an oversight in the grammar.) Traditional implementations
  703. of \fBbc\fR have only call by value array parameters.
  704. .IP "function format"
  705. POSIX \fBbc\fR requires the opening brace on the same line as the
  706. \fBdefine\fR key word and the \fBauto\fR statement on the next line.
  707. .IP "=+, =-, =*, =/, =%, =^"
  708. POSIX \fBbc\fR does not require these "old style" assignment operators to
  709. be defined. This version may allow these "old style" assignments. Use
  710. the limits statement to see if the installed version supports them. If
  711. it does support the "old style" assignment operators, the statement
  712. "a =- 1" will decrement \fBa\fR by 1 instead of setting \fBa\fR to the
  713. value -1.
  714. .IP "spaces in numbers"
  715. Other implementations of \fBbc\fR allow spaces in numbers. For example,
  716. "x=1 3" would assign the value 13 to the variable x. The same statement
  717. would cause a syntax error in this version of \fBbc\fR.
  718. .IP "errors and execution"
  719. This implementation varies from other implementations in terms of what
  720. code will be executed when syntax and other errors are found in the
  721. program. If a syntax error is found in a function definition, error
  722. recovery tries to find the beginning of a statement and continue to
  723. parse the function. Once a syntax error is found in the function, the
  724. function will not be callable and becomes undefined.
  725. Syntax errors in the interactive execution code will invalidate the
  726. current execution block. The execution block is terminated by an
  727. end of line that appears after a complete sequence of statements.
  728. For example,
  729. .nf
  730. .RS
  731. a = 1
  732. b = 2
  733. .RE
  734. .fi
  735. has two execution blocks and
  736. .nf
  737. .RS
  738. { a = 1
  739. b = 2 }
  740. .RE
  741. .fi
  742. has one execution block. Any runtime error will terminate the execution
  743. of the current execution block. A runtime warning will not terminate the
  744. current execution block.
  745. .IP "Interrupts"
  746. During an interactive session, the SIGINT signal (usually generated by
  747. the control-C character from the terminal) will cause execution of the
  748. current execution block to be interrupted. It will display a "runtime"
  749. error indicating which function was interrupted. After all runtime
  750. structures have been cleaned up, a message will be printed to notify the
  751. user that \fBbc\fR is ready for more input. All previously defined functions
  752. remain defined and the value of all non-auto variables are the value at
  753. the point of interruption. All auto variables and function parameters
  754. are removed during the
  755. clean up process. During a non-interactive
  756. session, the SIGINT signal will terminate the entire run of \fBbc\fR.
  757. .SS LIMITS
  758. The following are the limits currently in place for this
  759. .B bc
  760. processor. Some of them may have been changed by an installation.
  761. Use the limits statement to see the actual values.
  762. .IP "BC_BASE_MAX"
  763. The maximum output base is currently set at 999. The maximum input base
  764. is 16.
  765. .IP "BC_DIM_MAX"
  766. This is currently an arbitrary limit of 65535 as distributed. Your
  767. installation may be different.
  768. .IP "BC_SCALE_MAX"
  769. The number of digits after the decimal point is limited to INT_MAX digits.
  770. Also, the number of digits before the decimal point is limited to INT_MAX
  771. digits.
  772. .IP "BC_STRING_MAX"
  773. The limit on the number of characters in a string is INT_MAX characters.
  774. .IP "exponent"
  775. The value of the exponent in the raise operation (^) is limited to LONG_MAX.
  776. .IP "variable names"
  777. The current limit on the number of unique names is 32767 for each of
  778. simple variables, arrays and functions.
  779. .SH ENVIRONMENT VARIABLES
  780. The following environment variables are processed by \fBbc\fR:
  781. .IP "POSIXLY_CORRECT"
  782. This is the same as the \fB-s\fR option.
  783. .IP "BC_ENV_ARGS"
  784. This is another mechanism to get arguments to \fBbc\fR. The
  785. format is the same as the command line arguments. These arguments
  786. are processed first, so any files listed in the environment arguments
  787. are processed before any command line argument files. This allows
  788. the user to set up "standard" options and files to be processed
  789. at every invocation of \fBbc\fR. The files in the environment
  790. variables would typically contain function definitions for functions
  791. the user wants defined every time \fBbc\fR is run.
  792. .IP "BC_LINE_LENGTH"
  793. This should be an integer specifying the number of characters in an
  794. output line for numbers. This includes the backslash and newline characters
  795. for long numbers. As an extension, the value of zero disables the
  796. multi-line feature. Any other value of this variable that is less than
  797. 3 sets the line length to 70.
  798. .SH DIAGNOSTICS
  799. If any file on the command line can not be opened, \fBbc\fR will report
  800. that the file is unavailable and terminate. Also, there are compile
  801. and run time diagnostics that should be self-explanatory.
  802. .SH BUGS
  803. Error recovery is not very good yet.
  804. .PP
  805. Email bug reports to
  806. .BR bug-bc@gnu.org .
  807. Be sure to include the word ``bc'' somewhere in the ``Subject:'' field.
  808. .SH AUTHOR
  809. .nf
  810. Philip A. Nelson
  811. philnelson@acm.org
  812. .fi
  813. .SH ACKNOWLEDGEMENTS
  814. The author would like to thank Steve Sommars (Steve.Sommars@att.com) for
  815. his extensive help in testing the implementation. Many great suggestions
  816. were given. This is a much better product due to his involvement.