]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/bc/manuals/bc/HN.1
Merge bmake-20210206
[FreeBSD/FreeBSD.git] / contrib / bc / manuals / bc / HN.1
1 .\"
2 .\" SPDX-License-Identifier: BSD-2-Clause
3 .\"
4 .\" Copyright (c) 2018-2021 Gavin D. Howard and contributors.
5 .\"
6 .\" Redistribution and use in source and binary forms, with or without
7 .\" modification, are permitted provided that the following conditions are met:
8 .\"
9 .\" * Redistributions of source code must retain the above copyright notice,
10 .\"   this list of conditions and the following disclaimer.
11 .\"
12 .\" * Redistributions in binary form must reproduce the above copyright notice,
13 .\"   this list of conditions and the following disclaimer in the documentation
14 .\"   and/or other materials provided with the distribution.
15 .\"
16 .\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 .\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 .\" LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 .\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 .\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 .\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 .\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 .\" POSSIBILITY OF SUCH DAMAGE.
27 .\"
28 .TH "BC" "1" "January 2021" "Gavin D. Howard" "General Commands Manual"
29 .SH NAME
30 .PP
31 bc - arbitrary-precision decimal arithmetic language and calculator
32 .SH SYNOPSIS
33 .PP
34 \f[B]bc\f[R] [\f[B]-ghilPqsvVw\f[R]] [\f[B]\[en]global-stacks\f[R]]
35 [\f[B]\[en]help\f[R]] [\f[B]\[en]interactive\f[R]]
36 [\f[B]\[en]mathlib\f[R]] [\f[B]\[en]no-prompt\f[R]]
37 [\f[B]\[en]quiet\f[R]] [\f[B]\[en]standard\f[R]] [\f[B]\[en]warn\f[R]]
38 [\f[B]\[en]version\f[R]] [\f[B]-e\f[R] \f[I]expr\f[R]]
39 [\f[B]\[en]expression\f[R]=\f[I]expr\f[R]\&...] [\f[B]-f\f[R]
40 \f[I]file\f[R]\&...] [\f[B]-file\f[R]=\f[I]file\f[R]\&...]
41 [\f[I]file\f[R]\&...]
42 .SH DESCRIPTION
43 .PP
44 bc(1) is an interactive processor for a language first standardized in
45 1991 by POSIX.
46 (The current standard is
47 here (https://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html).)
48 The language provides unlimited precision decimal arithmetic and is
49 somewhat C-like, but there are differences.
50 Such differences will be noted in this document.
51 .PP
52 After parsing and handling options, this bc(1) reads any files given on
53 the command line and executes them before reading from \f[B]stdin\f[R].
54 .SH OPTIONS
55 .PP
56 The following are the options that bc(1) accepts.
57 .TP
58 \f[B]-g\f[R], \f[B]\[en]global-stacks\f[R]
59 Turns the globals \f[B]ibase\f[R], \f[B]obase\f[R], \f[B]scale\f[R], and
60 \f[B]seed\f[R] into stacks.
61 .RS
62 .PP
63 This has the effect that a copy of the current value of all four are
64 pushed onto a stack for every function call, as well as popped when
65 every function returns.
66 This means that functions can assign to any and all of those globals
67 without worrying that the change will affect other functions.
68 Thus, a hypothetical function named \f[B]output(x,b)\f[R] that simply
69 printed \f[B]x\f[R] in base \f[B]b\f[R] could be written like this:
70 .IP
71 .nf
72 \f[C]
73 define void output(x, b) {
74     obase=b
75     x
76 }
77 \f[R]
78 .fi
79 .PP
80 instead of like this:
81 .IP
82 .nf
83 \f[C]
84 define void output(x, b) {
85     auto c
86     c=obase
87     obase=b
88     x
89     obase=c
90 }
91 \f[R]
92 .fi
93 .PP
94 This makes writing functions much easier.
95 .PP
96 (\f[B]Note\f[R]: the function \f[B]output(x,b)\f[R] exists in the
97 extended math library.
98 See the \f[B]LIBRARY\f[R] section.)
99 .PP
100 However, since using this flag means that functions cannot set
101 \f[B]ibase\f[R], \f[B]obase\f[R], \f[B]scale\f[R], or \f[B]seed\f[R]
102 globally, functions that are made to do so cannot work anymore.
103 There are two possible use cases for that, and each has a solution.
104 .PP
105 First, if a function is called on startup to turn bc(1) into a number
106 converter, it is possible to replace that capability with various shell
107 aliases.
108 Examples:
109 .IP
110 .nf
111 \f[C]
112 alias d2o=\[dq]bc -e ibase=A -e obase=8\[dq]
113 alias h2b=\[dq]bc -e ibase=G -e obase=2\[dq]
114 \f[R]
115 .fi
116 .PP
117 Second, if the purpose of a function is to set \f[B]ibase\f[R],
118 \f[B]obase\f[R], \f[B]scale\f[R], or \f[B]seed\f[R] globally for any
119 other purpose, it could be split into one to four functions (based on
120 how many globals it sets) and each of those functions could return the
121 desired value for a global.
122 .PP
123 For functions that set \f[B]seed\f[R], the value assigned to
124 \f[B]seed\f[R] is not propagated to parent functions.
125 This means that the sequence of pseudo-random numbers that they see will
126 not be the same sequence of pseudo-random numbers that any parent sees.
127 This is only the case once \f[B]seed\f[R] has been set.
128 .PP
129 If a function desires to not affect the sequence of pseudo-random
130 numbers of its parents, but wants to use the same \f[B]seed\f[R], it can
131 use the following line:
132 .IP
133 .nf
134 \f[C]
135 seed = seed
136 \f[R]
137 .fi
138 .PP
139 If the behavior of this option is desired for every run of bc(1), then
140 users could make sure to define \f[B]BC_ENV_ARGS\f[R] and include this
141 option (see the \f[B]ENVIRONMENT VARIABLES\f[R] section for more
142 details).
143 .PP
144 If \f[B]-s\f[R], \f[B]-w\f[R], or any equivalents are used, this option
145 is ignored.
146 .PP
147 This is a \f[B]non-portable extension\f[R].
148 .RE
149 .TP
150 \f[B]-h\f[R], \f[B]\[en]help\f[R]
151 Prints a usage message and quits.
152 .TP
153 \f[B]-i\f[R], \f[B]\[en]interactive\f[R]
154 Forces interactive mode.
155 (See the \f[B]INTERACTIVE MODE\f[R] section.)
156 .RS
157 .PP
158 This is a \f[B]non-portable extension\f[R].
159 .RE
160 .TP
161 \f[B]-l\f[R], \f[B]\[en]mathlib\f[R]
162 Sets \f[B]scale\f[R] (see the \f[B]SYNTAX\f[R] section) to \f[B]20\f[R]
163 and loads the included math library and the extended math library before
164 running any code, including any expressions or files specified on the
165 command line.
166 .RS
167 .PP
168 To learn what is in the libraries, see the \f[B]LIBRARY\f[R] section.
169 .RE
170 .TP
171 \f[B]-P\f[R], \f[B]\[en]no-prompt\f[R]
172 Disables the prompt in TTY mode.
173 (The prompt is only enabled in TTY mode.
174 See the \f[B]TTY MODE\f[R] section) This is mostly for those users that
175 do not want a prompt or are not used to having them in bc(1).
176 Most of those users would want to put this option in
177 \f[B]BC_ENV_ARGS\f[R] (see the \f[B]ENVIRONMENT VARIABLES\f[R] section).
178 .RS
179 .PP
180 This is a \f[B]non-portable extension\f[R].
181 .RE
182 .TP
183 \f[B]-q\f[R], \f[B]\[en]quiet\f[R]
184 This option is for compatibility with the GNU
185 bc(1) (https://www.gnu.org/software/bc/); it is a no-op.
186 Without this option, GNU bc(1) prints a copyright header.
187 This bc(1) only prints the copyright header if one or more of the
188 \f[B]-v\f[R], \f[B]-V\f[R], or \f[B]\[en]version\f[R] options are given.
189 .RS
190 .PP
191 This is a \f[B]non-portable extension\f[R].
192 .RE
193 .TP
194 \f[B]-s\f[R], \f[B]\[en]standard\f[R]
195 Process exactly the language defined by the
196 standard (https://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html)
197 and error if any extensions are used.
198 .RS
199 .PP
200 This is a \f[B]non-portable extension\f[R].
201 .RE
202 .TP
203 \f[B]-v\f[R], \f[B]-V\f[R], \f[B]\[en]version\f[R]
204 Print the version information (copyright header) and exit.
205 .RS
206 .PP
207 This is a \f[B]non-portable extension\f[R].
208 .RE
209 .TP
210 \f[B]-w\f[R], \f[B]\[en]warn\f[R]
211 Like \f[B]-s\f[R] and \f[B]\[en]standard\f[R], except that warnings (and
212 not errors) are printed for non-standard extensions and execution
213 continues normally.
214 .RS
215 .PP
216 This is a \f[B]non-portable extension\f[R].
217 .RE
218 .TP
219 \f[B]-e\f[R] \f[I]expr\f[R], \f[B]\[en]expression\f[R]=\f[I]expr\f[R]
220 Evaluates \f[I]expr\f[R].
221 If multiple expressions are given, they are evaluated in order.
222 If files are given as well (see below), the expressions and files are
223 evaluated in the order given.
224 This means that if a file is given before an expression, the file is
225 read in and evaluated first.
226 .RS
227 .PP
228 After processing all expressions and files, bc(1) will exit, unless
229 \f[B]-\f[R] (\f[B]stdin\f[R]) was given as an argument at least once to
230 \f[B]-f\f[R] or \f[B]\[en]file\f[R].
231 However, if any other \f[B]-e\f[R], \f[B]\[en]expression\f[R],
232 \f[B]-f\f[R], or \f[B]\[en]file\f[R] arguments are given after that,
233 bc(1) will give a fatal error and exit.
234 .PP
235 This is a \f[B]non-portable extension\f[R].
236 .RE
237 .TP
238 \f[B]-f\f[R] \f[I]file\f[R], \f[B]\[en]file\f[R]=\f[I]file\f[R]
239 Reads in \f[I]file\f[R] and evaluates it, line by line, as though it
240 were read through \f[B]stdin\f[R].
241 If expressions are also given (see above), the expressions are evaluated
242 in the order given.
243 .RS
244 .PP
245 After processing all expressions and files, bc(1) will exit, unless
246 \f[B]-\f[R] (\f[B]stdin\f[R]) was given as an argument at least once to
247 \f[B]-f\f[R] or \f[B]\[en]file\f[R].
248 .PP
249 This is a \f[B]non-portable extension\f[R].
250 .RE
251 .PP
252 All long options are \f[B]non-portable extensions\f[R].
253 .SH STDOUT
254 .PP
255 Any non-error output is written to \f[B]stdout\f[R].
256 .PP
257 \f[B]Note\f[R]: Unlike other bc(1) implementations, this bc(1) will
258 issue a fatal error (see the \f[B]EXIT STATUS\f[R] section) if it cannot
259 write to \f[B]stdout\f[R], so if \f[B]stdout\f[R] is closed, as in
260 \f[B]bc >&-\f[R], it will quit with an error.
261 This is done so that bc(1) can report problems when \f[B]stdout\f[R] is
262 redirected to a file.
263 .PP
264 If there are scripts that depend on the behavior of other bc(1)
265 implementations, it is recommended that those scripts be changed to
266 redirect \f[B]stdout\f[R] to \f[B]/dev/null\f[R].
267 .SH STDERR
268 .PP
269 Any error output is written to \f[B]stderr\f[R].
270 .PP
271 \f[B]Note\f[R]: Unlike other bc(1) implementations, this bc(1) will
272 issue a fatal error (see the \f[B]EXIT STATUS\f[R] section) if it cannot
273 write to \f[B]stderr\f[R], so if \f[B]stderr\f[R] is closed, as in
274 \f[B]bc 2>&-\f[R], it will quit with an error.
275 This is done so that bc(1) can exit with an error code when
276 \f[B]stderr\f[R] is redirected to a file.
277 .PP
278 If there are scripts that depend on the behavior of other bc(1)
279 implementations, it is recommended that those scripts be changed to
280 redirect \f[B]stderr\f[R] to \f[B]/dev/null\f[R].
281 .SH SYNTAX
282 .PP
283 The syntax for bc(1) programs is mostly C-like, with some differences.
284 This bc(1) follows the POSIX
285 standard (https://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html),
286 which is a much more thorough resource for the language this bc(1)
287 accepts.
288 This section is meant to be a summary and a listing of all the
289 extensions to the standard.
290 .PP
291 In the sections below, \f[B]E\f[R] means expression, \f[B]S\f[R] means
292 statement, and \f[B]I\f[R] means identifier.
293 .PP
294 Identifiers (\f[B]I\f[R]) start with a lowercase letter and can be
295 followed by any number (up to \f[B]BC_NAME_MAX-1\f[R]) of lowercase
296 letters (\f[B]a-z\f[R]), digits (\f[B]0-9\f[R]), and underscores
297 (\f[B]_\f[R]).
298 The regex is \f[B][a-z][a-z0-9_]*\f[R].
299 Identifiers with more than one character (letter) are a
300 \f[B]non-portable extension\f[R].
301 .PP
302 \f[B]ibase\f[R] is a global variable determining how to interpret
303 constant numbers.
304 It is the \[lq]input\[rq] base, or the number base used for interpreting
305 input numbers.
306 \f[B]ibase\f[R] is initially \f[B]10\f[R].
307 If the \f[B]-s\f[R] (\f[B]\[en]standard\f[R]) and \f[B]-w\f[R]
308 (\f[B]\[en]warn\f[R]) flags were not given on the command line, the max
309 allowable value for \f[B]ibase\f[R] is \f[B]36\f[R].
310 Otherwise, it is \f[B]16\f[R].
311 The min allowable value for \f[B]ibase\f[R] is \f[B]2\f[R].
312 The max allowable value for \f[B]ibase\f[R] can be queried in bc(1)
313 programs with the \f[B]maxibase()\f[R] built-in function.
314 .PP
315 \f[B]obase\f[R] is a global variable determining how to output results.
316 It is the \[lq]output\[rq] base, or the number base used for outputting
317 numbers.
318 \f[B]obase\f[R] is initially \f[B]10\f[R].
319 The max allowable value for \f[B]obase\f[R] is \f[B]BC_BASE_MAX\f[R] and
320 can be queried in bc(1) programs with the \f[B]maxobase()\f[R] built-in
321 function.
322 The min allowable value for \f[B]obase\f[R] is \f[B]0\f[R].
323 If \f[B]obase\f[R] is \f[B]0\f[R], values are output in scientific
324 notation, and if \f[B]obase\f[R] is \f[B]1\f[R], values are output in
325 engineering notation.
326 Otherwise, values are output in the specified base.
327 .PP
328 Outputting in scientific and engineering notations are \f[B]non-portable
329 extensions\f[R].
330 .PP
331 The \f[I]scale\f[R] of an expression is the number of digits in the
332 result of the expression right of the decimal point, and \f[B]scale\f[R]
333 is a global variable that sets the precision of any operations, with
334 exceptions.
335 \f[B]scale\f[R] is initially \f[B]0\f[R].
336 \f[B]scale\f[R] cannot be negative.
337 The max allowable value for \f[B]scale\f[R] is \f[B]BC_SCALE_MAX\f[R]
338 and can be queried in bc(1) programs with the \f[B]maxscale()\f[R]
339 built-in function.
340 .PP
341 bc(1) has both \f[I]global\f[R] variables and \f[I]local\f[R] variables.
342 All \f[I]local\f[R] variables are local to the function; they are
343 parameters or are introduced in the \f[B]auto\f[R] list of a function
344 (see the \f[B]FUNCTIONS\f[R] section).
345 If a variable is accessed which is not a parameter or in the
346 \f[B]auto\f[R] list, it is assumed to be \f[I]global\f[R].
347 If a parent function has a \f[I]local\f[R] variable version of a
348 variable that a child function considers \f[I]global\f[R], the value of
349 that \f[I]global\f[R] variable in the child function is the value of the
350 variable in the parent function, not the value of the actual
351 \f[I]global\f[R] variable.
352 .PP
353 All of the above applies to arrays as well.
354 .PP
355 The value of a statement that is an expression (i.e., any of the named
356 expressions or operands) is printed unless the lowest precedence
357 operator is an assignment operator \f[I]and\f[R] the expression is
358 notsurrounded by parentheses.
359 .PP
360 The value that is printed is also assigned to the special variable
361 \f[B]last\f[R].
362 A single dot (\f[B].\f[R]) may also be used as a synonym for
363 \f[B]last\f[R].
364 These are \f[B]non-portable extensions\f[R].
365 .PP
366 Either semicolons or newlines may separate statements.
367 .SS Comments
368 .PP
369 There are two kinds of comments:
370 .IP "1." 3
371 Block comments are enclosed in \f[B]/*\f[R] and \f[B]*/\f[R].
372 .IP "2." 3
373 Line comments go from \f[B]#\f[R] until, and not including, the next
374 newline.
375 This is a \f[B]non-portable extension\f[R].
376 .SS Named Expressions
377 .PP
378 The following are named expressions in bc(1):
379 .IP "1." 3
380 Variables: \f[B]I\f[R]
381 .IP "2." 3
382 Array Elements: \f[B]I[E]\f[R]
383 .IP "3." 3
384 \f[B]ibase\f[R]
385 .IP "4." 3
386 \f[B]obase\f[R]
387 .IP "5." 3
388 \f[B]scale\f[R]
389 .IP "6." 3
390 \f[B]seed\f[R]
391 .IP "7." 3
392 \f[B]last\f[R] or a single dot (\f[B].\f[R])
393 .PP
394 Numbers 6 and 7 are \f[B]non-portable extensions\f[R].
395 .PP
396 The meaning of \f[B]seed\f[R] is dependent on the current pseudo-random
397 number generator but is guaranteed to not change except for new major
398 versions.
399 .PP
400 The \f[I]scale\f[R] and sign of the value may be significant.
401 .PP
402 If a previously used \f[B]seed\f[R] value is assigned to \f[B]seed\f[R]
403 and used again, the pseudo-random number generator is guaranteed to
404 produce the same sequence of pseudo-random numbers as it did when the
405 \f[B]seed\f[R] value was previously used.
406 .PP
407 The exact value assigned to \f[B]seed\f[R] is not guaranteed to be
408 returned if \f[B]seed\f[R] is queried again immediately.
409 However, if \f[B]seed\f[R] \f[I]does\f[R] return a different value, both
410 values, when assigned to \f[B]seed\f[R], are guaranteed to produce the
411 same sequence of pseudo-random numbers.
412 This means that certain values assigned to \f[B]seed\f[R] will
413 \f[I]not\f[R] produce unique sequences of pseudo-random numbers.
414 The value of \f[B]seed\f[R] will change after any use of the
415 \f[B]rand()\f[R] and \f[B]irand(E)\f[R] operands (see the
416 \f[I]Operands\f[R] subsection below), except if the parameter passed to
417 \f[B]irand(E)\f[R] is \f[B]0\f[R], \f[B]1\f[R], or negative.
418 .PP
419 There is no limit to the length (number of significant decimal digits)
420 or \f[I]scale\f[R] of the value that can be assigned to \f[B]seed\f[R].
421 .PP
422 Variables and arrays do not interfere; users can have arrays named the
423 same as variables.
424 This also applies to functions (see the \f[B]FUNCTIONS\f[R] section), so
425 a user can have a variable, array, and function that all have the same
426 name, and they will not shadow each other, whether inside of functions
427 or not.
428 .PP
429 Named expressions are required as the operand of
430 \f[B]increment\f[R]/\f[B]decrement\f[R] operators and as the left side
431 of \f[B]assignment\f[R] operators (see the \f[I]Operators\f[R]
432 subsection).
433 .SS Operands
434 .PP
435 The following are valid operands in bc(1):
436 .IP " 1." 4
437 Numbers (see the \f[I]Numbers\f[R] subsection below).
438 .IP " 2." 4
439 Array indices (\f[B]I[E]\f[R]).
440 .IP " 3." 4
441 \f[B](E)\f[R]: The value of \f[B]E\f[R] (used to change precedence).
442 .IP " 4." 4
443 \f[B]sqrt(E)\f[R]: The square root of \f[B]E\f[R].
444 \f[B]E\f[R] must be non-negative.
445 .IP " 5." 4
446 \f[B]length(E)\f[R]: The number of significant decimal digits in
447 \f[B]E\f[R].
448 .IP " 6." 4
449 \f[B]length(I[])\f[R]: The number of elements in the array \f[B]I\f[R].
450 This is a \f[B]non-portable extension\f[R].
451 .IP " 7." 4
452 \f[B]scale(E)\f[R]: The \f[I]scale\f[R] of \f[B]E\f[R].
453 .IP " 8." 4
454 \f[B]abs(E)\f[R]: The absolute value of \f[B]E\f[R].
455 This is a \f[B]non-portable extension\f[R].
456 .IP " 9." 4
457 \f[B]I()\f[R], \f[B]I(E)\f[R], \f[B]I(E, E)\f[R], and so on, where
458 \f[B]I\f[R] is an identifier for a non-\f[B]void\f[R] function (see the
459 \f[I]Void Functions\f[R] subsection of the \f[B]FUNCTIONS\f[R] section).
460 The \f[B]E\f[R] argument(s) may also be arrays of the form
461 \f[B]I[]\f[R], which will automatically be turned into array references
462 (see the \f[I]Array References\f[R] subsection of the
463 \f[B]FUNCTIONS\f[R] section) if the corresponding parameter in the
464 function definition is an array reference.
465 .IP "10." 4
466 \f[B]read()\f[R]: Reads a line from \f[B]stdin\f[R] and uses that as an
467 expression.
468 The result of that expression is the result of the \f[B]read()\f[R]
469 operand.
470 This is a \f[B]non-portable extension\f[R].
471 .IP "11." 4
472 \f[B]maxibase()\f[R]: The max allowable \f[B]ibase\f[R].
473 This is a \f[B]non-portable extension\f[R].
474 .IP "12." 4
475 \f[B]maxobase()\f[R]: The max allowable \f[B]obase\f[R].
476 This is a \f[B]non-portable extension\f[R].
477 .IP "13." 4
478 \f[B]maxscale()\f[R]: The max allowable \f[B]scale\f[R].
479 This is a \f[B]non-portable extension\f[R].
480 .IP "14." 4
481 \f[B]rand()\f[R]: A pseudo-random integer between \f[B]0\f[R]
482 (inclusive) and \f[B]BC_RAND_MAX\f[R] (inclusive).
483 Using this operand will change the value of \f[B]seed\f[R].
484 This is a \f[B]non-portable extension\f[R].
485 .IP "15." 4
486 \f[B]irand(E)\f[R]: A pseudo-random integer between \f[B]0\f[R]
487 (inclusive) and the value of \f[B]E\f[R] (exclusive).
488 If \f[B]E\f[R] is negative or is a non-integer (\f[B]E\f[R]\[cq]s
489 \f[I]scale\f[R] is not \f[B]0\f[R]), an error is raised, and bc(1)
490 resets (see the \f[B]RESET\f[R] section) while \f[B]seed\f[R] remains
491 unchanged.
492 If \f[B]E\f[R] is larger than \f[B]BC_RAND_MAX\f[R], the higher bound is
493 honored by generating several pseudo-random integers, multiplying them
494 by appropriate powers of \f[B]BC_RAND_MAX+1\f[R], and adding them
495 together.
496 Thus, the size of integer that can be generated with this operand is
497 unbounded.
498 Using this operand will change the value of \f[B]seed\f[R], unless the
499 value of \f[B]E\f[R] is \f[B]0\f[R] or \f[B]1\f[R].
500 In that case, \f[B]0\f[R] is returned, and \f[B]seed\f[R] is
501 \f[I]not\f[R] changed.
502 This is a \f[B]non-portable extension\f[R].
503 .IP "16." 4
504 \f[B]maxrand()\f[R]: The max integer returned by \f[B]rand()\f[R].
505 This is a \f[B]non-portable extension\f[R].
506 .PP
507 The integers generated by \f[B]rand()\f[R] and \f[B]irand(E)\f[R] are
508 guaranteed to be as unbiased as possible, subject to the limitations of
509 the pseudo-random number generator.
510 .PP
511 \f[B]Note\f[R]: The values returned by the pseudo-random number
512 generator with \f[B]rand()\f[R] and \f[B]irand(E)\f[R] are guaranteed to
513 \f[I]NOT\f[R] be cryptographically secure.
514 This is a consequence of using a seeded pseudo-random number generator.
515 However, they \f[I]are\f[R] guaranteed to be reproducible with identical
516 \f[B]seed\f[R] values.
517 This means that the pseudo-random values from bc(1) should only be used
518 where a reproducible stream of pseudo-random numbers is
519 \f[I]ESSENTIAL\f[R].
520 In any other case, use a non-seeded pseudo-random number generator.
521 .SS Numbers
522 .PP
523 Numbers are strings made up of digits, uppercase letters, and at most
524 \f[B]1\f[R] period for a radix.
525 Numbers can have up to \f[B]BC_NUM_MAX\f[R] digits.
526 Uppercase letters are equal to \f[B]9\f[R] + their position in the
527 alphabet (i.e., \f[B]A\f[R] equals \f[B]10\f[R], or \f[B]9+1\f[R]).
528 If a digit or letter makes no sense with the current value of
529 \f[B]ibase\f[R], they are set to the value of the highest valid digit in
530 \f[B]ibase\f[R].
531 .PP
532 Single-character numbers (i.e., \f[B]A\f[R] alone) take the value that
533 they would have if they were valid digits, regardless of the value of
534 \f[B]ibase\f[R].
535 This means that \f[B]A\f[R] alone always equals decimal \f[B]10\f[R] and
536 \f[B]Z\f[R] alone always equals decimal \f[B]35\f[R].
537 .PP
538 In addition, bc(1) accepts numbers in scientific notation.
539 These have the form \f[B]<number>e<integer>\f[R].
540 The exponent (the portion after the \f[B]e\f[R]) must be an integer.
541 An example is \f[B]1.89237e9\f[R], which is equal to
542 \f[B]1892370000\f[R].
543 Negative exponents are also allowed, so \f[B]4.2890e-3\f[R] is equal to
544 \f[B]0.0042890\f[R].
545 .PP
546 Using scientific notation is an error or warning if the \f[B]-s\f[R] or
547 \f[B]-w\f[R], respectively, command-line options (or equivalents) are
548 given.
549 .PP
550 \f[B]WARNING\f[R]: Both the number and the exponent in scientific
551 notation are interpreted according to the current \f[B]ibase\f[R], but
552 the number is still multiplied by \f[B]10\[ha]exponent\f[R] regardless
553 of the current \f[B]ibase\f[R].
554 For example, if \f[B]ibase\f[R] is \f[B]16\f[R] and bc(1) is given the
555 number string \f[B]FFeA\f[R], the resulting decimal number will be
556 \f[B]2550000000000\f[R], and if bc(1) is given the number string
557 \f[B]10e-4\f[R], the resulting decimal number will be \f[B]0.0016\f[R].
558 .PP
559 Accepting input as scientific notation is a \f[B]non-portable
560 extension\f[R].
561 .SS Operators
562 .PP
563 The following arithmetic and logical operators can be used.
564 They are listed in order of decreasing precedence.
565 Operators in the same group have the same precedence.
566 .TP
567 \f[B]++\f[R] \f[B]\[en]\f[R]
568 Type: Prefix and Postfix
569 .RS
570 .PP
571 Associativity: None
572 .PP
573 Description: \f[B]increment\f[R], \f[B]decrement\f[R]
574 .RE
575 .TP
576 \f[B]-\f[R] \f[B]!\f[R]
577 Type: Prefix
578 .RS
579 .PP
580 Associativity: None
581 .PP
582 Description: \f[B]negation\f[R], \f[B]boolean not\f[R]
583 .RE
584 .TP
585 \f[B]$\f[R]
586 Type: Postfix
587 .RS
588 .PP
589 Associativity: None
590 .PP
591 Description: \f[B]truncation\f[R]
592 .RE
593 .TP
594 \f[B]\[at]\f[R]
595 Type: Binary
596 .RS
597 .PP
598 Associativity: Right
599 .PP
600 Description: \f[B]set precision\f[R]
601 .RE
602 .TP
603 \f[B]\[ha]\f[R]
604 Type: Binary
605 .RS
606 .PP
607 Associativity: Right
608 .PP
609 Description: \f[B]power\f[R]
610 .RE
611 .TP
612 \f[B]*\f[R] \f[B]/\f[R] \f[B]%\f[R]
613 Type: Binary
614 .RS
615 .PP
616 Associativity: Left
617 .PP
618 Description: \f[B]multiply\f[R], \f[B]divide\f[R], \f[B]modulus\f[R]
619 .RE
620 .TP
621 \f[B]+\f[R] \f[B]-\f[R]
622 Type: Binary
623 .RS
624 .PP
625 Associativity: Left
626 .PP
627 Description: \f[B]add\f[R], \f[B]subtract\f[R]
628 .RE
629 .TP
630 \f[B]<<\f[R] \f[B]>>\f[R]
631 Type: Binary
632 .RS
633 .PP
634 Associativity: Left
635 .PP
636 Description: \f[B]shift left\f[R], \f[B]shift right\f[R]
637 .RE
638 .TP
639 \f[B]=\f[R] \f[B]<<=\f[R] \f[B]>>=\f[R] \f[B]+=\f[R] \f[B]-=\f[R] \f[B]*=\f[R] \f[B]/=\f[R] \f[B]%=\f[R] \f[B]\[ha]=\f[R] \f[B]\[at]=\f[R]
640 Type: Binary
641 .RS
642 .PP
643 Associativity: Right
644 .PP
645 Description: \f[B]assignment\f[R]
646 .RE
647 .TP
648 \f[B]==\f[R] \f[B]<=\f[R] \f[B]>=\f[R] \f[B]!=\f[R] \f[B]<\f[R] \f[B]>\f[R]
649 Type: Binary
650 .RS
651 .PP
652 Associativity: Left
653 .PP
654 Description: \f[B]relational\f[R]
655 .RE
656 .TP
657 \f[B]&&\f[R]
658 Type: Binary
659 .RS
660 .PP
661 Associativity: Left
662 .PP
663 Description: \f[B]boolean and\f[R]
664 .RE
665 .TP
666 \f[B]||\f[R]
667 Type: Binary
668 .RS
669 .PP
670 Associativity: Left
671 .PP
672 Description: \f[B]boolean or\f[R]
673 .RE
674 .PP
675 The operators will be described in more detail below.
676 .TP
677 \f[B]++\f[R] \f[B]\[en]\f[R]
678 The prefix and postfix \f[B]increment\f[R] and \f[B]decrement\f[R]
679 operators behave exactly like they would in C.
680 They require a named expression (see the \f[I]Named Expressions\f[R]
681 subsection) as an operand.
682 .RS
683 .PP
684 The prefix versions of these operators are more efficient; use them
685 where possible.
686 .RE
687 .TP
688 \f[B]-\f[R]
689 The \f[B]negation\f[R] operator returns \f[B]0\f[R] if a user attempts
690 to negate any expression with the value \f[B]0\f[R].
691 Otherwise, a copy of the expression with its sign flipped is returned.
692 .TP
693 \f[B]!\f[R]
694 The \f[B]boolean not\f[R] operator returns \f[B]1\f[R] if the expression
695 is \f[B]0\f[R], or \f[B]0\f[R] otherwise.
696 .RS
697 .PP
698 This is a \f[B]non-portable extension\f[R].
699 .RE
700 .TP
701 \f[B]$\f[R]
702 The \f[B]truncation\f[R] operator returns a copy of the given expression
703 with all of its \f[I]scale\f[R] removed.
704 .RS
705 .PP
706 This is a \f[B]non-portable extension\f[R].
707 .RE
708 .TP
709 \f[B]\[at]\f[R]
710 The \f[B]set precision\f[R] operator takes two expressions and returns a
711 copy of the first with its \f[I]scale\f[R] equal to the value of the
712 second expression.
713 That could either mean that the number is returned without change (if
714 the \f[I]scale\f[R] of the first expression matches the value of the
715 second expression), extended (if it is less), or truncated (if it is
716 more).
717 .RS
718 .PP
719 The second expression must be an integer (no \f[I]scale\f[R]) and
720 non-negative.
721 .PP
722 This is a \f[B]non-portable extension\f[R].
723 .RE
724 .TP
725 \f[B]\[ha]\f[R]
726 The \f[B]power\f[R] operator (not the \f[B]exclusive or\f[R] operator,
727 as it would be in C) takes two expressions and raises the first to the
728 power of the value of the second.
729 The \f[I]scale\f[R] of the result is equal to \f[B]scale\f[R].
730 .RS
731 .PP
732 The second expression must be an integer (no \f[I]scale\f[R]), and if it
733 is negative, the first value must be non-zero.
734 .RE
735 .TP
736 \f[B]*\f[R]
737 The \f[B]multiply\f[R] operator takes two expressions, multiplies them,
738 and returns the product.
739 If \f[B]a\f[R] is the \f[I]scale\f[R] of the first expression and
740 \f[B]b\f[R] is the \f[I]scale\f[R] of the second expression, the
741 \f[I]scale\f[R] of the result is equal to
742 \f[B]min(a+b,max(scale,a,b))\f[R] where \f[B]min()\f[R] and
743 \f[B]max()\f[R] return the obvious values.
744 .TP
745 \f[B]/\f[R]
746 The \f[B]divide\f[R] operator takes two expressions, divides them, and
747 returns the quotient.
748 The \f[I]scale\f[R] of the result shall be the value of \f[B]scale\f[R].
749 .RS
750 .PP
751 The second expression must be non-zero.
752 .RE
753 .TP
754 \f[B]%\f[R]
755 The \f[B]modulus\f[R] operator takes two expressions, \f[B]a\f[R] and
756 \f[B]b\f[R], and evaluates them by 1) Computing \f[B]a/b\f[R] to current
757 \f[B]scale\f[R] and 2) Using the result of step 1 to calculate
758 \f[B]a-(a/b)*b\f[R] to \f[I]scale\f[R]
759 \f[B]max(scale+scale(b),scale(a))\f[R].
760 .RS
761 .PP
762 The second expression must be non-zero.
763 .RE
764 .TP
765 \f[B]+\f[R]
766 The \f[B]add\f[R] operator takes two expressions, \f[B]a\f[R] and
767 \f[B]b\f[R], and returns the sum, with a \f[I]scale\f[R] equal to the
768 max of the \f[I]scale\f[R]s of \f[B]a\f[R] and \f[B]b\f[R].
769 .TP
770 \f[B]-\f[R]
771 The \f[B]subtract\f[R] operator takes two expressions, \f[B]a\f[R] and
772 \f[B]b\f[R], and returns the difference, with a \f[I]scale\f[R] equal to
773 the max of the \f[I]scale\f[R]s of \f[B]a\f[R] and \f[B]b\f[R].
774 .TP
775 \f[B]<<\f[R]
776 The \f[B]left shift\f[R] operator takes two expressions, \f[B]a\f[R] and
777 \f[B]b\f[R], and returns a copy of the value of \f[B]a\f[R] with its
778 decimal point moved \f[B]b\f[R] places to the right.
779 .RS
780 .PP
781 The second expression must be an integer (no \f[I]scale\f[R]) and
782 non-negative.
783 .PP
784 This is a \f[B]non-portable extension\f[R].
785 .RE
786 .TP
787 \f[B]>>\f[R]
788 The \f[B]right shift\f[R] operator takes two expressions, \f[B]a\f[R]
789 and \f[B]b\f[R], and returns a copy of the value of \f[B]a\f[R] with its
790 decimal point moved \f[B]b\f[R] places to the left.
791 .RS
792 .PP
793 The second expression must be an integer (no \f[I]scale\f[R]) and
794 non-negative.
795 .PP
796 This is a \f[B]non-portable extension\f[R].
797 .RE
798 .TP
799 \f[B]=\f[R] \f[B]<<=\f[R] \f[B]>>=\f[R] \f[B]+=\f[R] \f[B]-=\f[R] \f[B]*=\f[R] \f[B]/=\f[R] \f[B]%=\f[R] \f[B]\[ha]=\f[R] \f[B]\[at]=\f[R]
800 The \f[B]assignment\f[R] operators take two expressions, \f[B]a\f[R] and
801 \f[B]b\f[R] where \f[B]a\f[R] is a named expression (see the \f[I]Named
802 Expressions\f[R] subsection).
803 .RS
804 .PP
805 For \f[B]=\f[R], \f[B]b\f[R] is copied and the result is assigned to
806 \f[B]a\f[R].
807 For all others, \f[B]a\f[R] and \f[B]b\f[R] are applied as operands to
808 the corresponding arithmetic operator and the result is assigned to
809 \f[B]a\f[R].
810 .PP
811 The \f[B]assignment\f[R] operators that correspond to operators that are
812 extensions are themselves \f[B]non-portable extensions\f[R].
813 .RE
814 .TP
815 \f[B]==\f[R] \f[B]<=\f[R] \f[B]>=\f[R] \f[B]!=\f[R] \f[B]<\f[R] \f[B]>\f[R]
816 The \f[B]relational\f[R] operators compare two expressions, \f[B]a\f[R]
817 and \f[B]b\f[R], and if the relation holds, according to C language
818 semantics, the result is \f[B]1\f[R].
819 Otherwise, it is \f[B]0\f[R].
820 .RS
821 .PP
822 Note that unlike in C, these operators have a lower precedence than the
823 \f[B]assignment\f[R] operators, which means that \f[B]a=b>c\f[R] is
824 interpreted as \f[B](a=b)>c\f[R].
825 .PP
826 Also, unlike the
827 standard (https://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html)
828 requires, these operators can appear anywhere any other expressions can
829 be used.
830 This allowance is a \f[B]non-portable extension\f[R].
831 .RE
832 .TP
833 \f[B]&&\f[R]
834 The \f[B]boolean and\f[R] operator takes two expressions and returns
835 \f[B]1\f[R] if both expressions are non-zero, \f[B]0\f[R] otherwise.
836 .RS
837 .PP
838 This is \f[I]not\f[R] a short-circuit operator.
839 .PP
840 This is a \f[B]non-portable extension\f[R].
841 .RE
842 .TP
843 \f[B]||\f[R]
844 The \f[B]boolean or\f[R] operator takes two expressions and returns
845 \f[B]1\f[R] if one of the expressions is non-zero, \f[B]0\f[R]
846 otherwise.
847 .RS
848 .PP
849 This is \f[I]not\f[R] a short-circuit operator.
850 .PP
851 This is a \f[B]non-portable extension\f[R].
852 .RE
853 .SS Statements
854 .PP
855 The following items are statements:
856 .IP " 1." 4
857 \f[B]E\f[R]
858 .IP " 2." 4
859 \f[B]{\f[R] \f[B]S\f[R] \f[B];\f[R] \&... \f[B];\f[R] \f[B]S\f[R]
860 \f[B]}\f[R]
861 .IP " 3." 4
862 \f[B]if\f[R] \f[B](\f[R] \f[B]E\f[R] \f[B])\f[R] \f[B]S\f[R]
863 .IP " 4." 4
864 \f[B]if\f[R] \f[B](\f[R] \f[B]E\f[R] \f[B])\f[R] \f[B]S\f[R]
865 \f[B]else\f[R] \f[B]S\f[R]
866 .IP " 5." 4
867 \f[B]while\f[R] \f[B](\f[R] \f[B]E\f[R] \f[B])\f[R] \f[B]S\f[R]
868 .IP " 6." 4
869 \f[B]for\f[R] \f[B](\f[R] \f[B]E\f[R] \f[B];\f[R] \f[B]E\f[R]
870 \f[B];\f[R] \f[B]E\f[R] \f[B])\f[R] \f[B]S\f[R]
871 .IP " 7." 4
872 An empty statement
873 .IP " 8." 4
874 \f[B]break\f[R]
875 .IP " 9." 4
876 \f[B]continue\f[R]
877 .IP "10." 4
878 \f[B]quit\f[R]
879 .IP "11." 4
880 \f[B]halt\f[R]
881 .IP "12." 4
882 \f[B]limits\f[R]
883 .IP "13." 4
884 A string of characters, enclosed in double quotes
885 .IP "14." 4
886 \f[B]print\f[R] \f[B]E\f[R] \f[B],\f[R] \&... \f[B],\f[R] \f[B]E\f[R]
887 .IP "15." 4
888 \f[B]I()\f[R], \f[B]I(E)\f[R], \f[B]I(E, E)\f[R], and so on, where
889 \f[B]I\f[R] is an identifier for a \f[B]void\f[R] function (see the
890 \f[I]Void Functions\f[R] subsection of the \f[B]FUNCTIONS\f[R] section).
891 The \f[B]E\f[R] argument(s) may also be arrays of the form
892 \f[B]I[]\f[R], which will automatically be turned into array references
893 (see the \f[I]Array References\f[R] subsection of the
894 \f[B]FUNCTIONS\f[R] section) if the corresponding parameter in the
895 function definition is an array reference.
896 .PP
897 Numbers 4, 9, 11, 12, 14, and 15 are \f[B]non-portable extensions\f[R].
898 .PP
899 Also, as a \f[B]non-portable extension\f[R], any or all of the
900 expressions in the header of a for loop may be omitted.
901 If the condition (second expression) is omitted, it is assumed to be a
902 constant \f[B]1\f[R].
903 .PP
904 The \f[B]break\f[R] statement causes a loop to stop iterating and resume
905 execution immediately following a loop.
906 This is only allowed in loops.
907 .PP
908 The \f[B]continue\f[R] statement causes a loop iteration to stop early
909 and returns to the start of the loop, including testing the loop
910 condition.
911 This is only allowed in loops.
912 .PP
913 The \f[B]if\f[R] \f[B]else\f[R] statement does the same thing as in C.
914 .PP
915 The \f[B]quit\f[R] statement causes bc(1) to quit, even if it is on a
916 branch that will not be executed (it is a compile-time command).
917 .PP
918 The \f[B]halt\f[R] statement causes bc(1) to quit, if it is executed.
919 (Unlike \f[B]quit\f[R] if it is on a branch of an \f[B]if\f[R] statement
920 that is not executed, bc(1) does not quit.)
921 .PP
922 The \f[B]limits\f[R] statement prints the limits that this bc(1) is
923 subject to.
924 This is like the \f[B]quit\f[R] statement in that it is a compile-time
925 command.
926 .PP
927 An expression by itself is evaluated and printed, followed by a newline.
928 .PP
929 Both scientific notation and engineering notation are available for
930 printing the results of expressions.
931 Scientific notation is activated by assigning \f[B]0\f[R] to
932 \f[B]obase\f[R], and engineering notation is activated by assigning
933 \f[B]1\f[R] to \f[B]obase\f[R].
934 To deactivate them, just assign a different value to \f[B]obase\f[R].
935 .PP
936 Scientific notation and engineering notation are disabled if bc(1) is
937 run with either the \f[B]-s\f[R] or \f[B]-w\f[R] command-line options
938 (or equivalents).
939 .PP
940 Printing numbers in scientific notation and/or engineering notation is a
941 \f[B]non-portable extension\f[R].
942 .SS Print Statement
943 .PP
944 The \[lq]expressions\[rq] in a \f[B]print\f[R] statement may also be
945 strings.
946 If they are, there are backslash escape sequences that are interpreted
947 specially.
948 What those sequences are, and what they cause to be printed, are shown
949 below:
950 .PP
951 .TS
952 tab(@);
953 l l.
954 T{
955 \f[B]\[rs]a\f[R]
956 T}@T{
957 \f[B]\[rs]a\f[R]
958 T}
959 T{
960 \f[B]\[rs]b\f[R]
961 T}@T{
962 \f[B]\[rs]b\f[R]
963 T}
964 T{
965 \f[B]\[rs]\[rs]\f[R]
966 T}@T{
967 \f[B]\[rs]\f[R]
968 T}
969 T{
970 \f[B]\[rs]e\f[R]
971 T}@T{
972 \f[B]\[rs]\f[R]
973 T}
974 T{
975 \f[B]\[rs]f\f[R]
976 T}@T{
977 \f[B]\[rs]f\f[R]
978 T}
979 T{
980 \f[B]\[rs]n\f[R]
981 T}@T{
982 \f[B]\[rs]n\f[R]
983 T}
984 T{
985 \f[B]\[rs]q\f[R]
986 T}@T{
987 \f[B]\[dq]\f[R]
988 T}
989 T{
990 \f[B]\[rs]r\f[R]
991 T}@T{
992 \f[B]\[rs]r\f[R]
993 T}
994 T{
995 \f[B]\[rs]t\f[R]
996 T}@T{
997 \f[B]\[rs]t\f[R]
998 T}
999 .TE
1000 .PP
1001 Any other character following a backslash causes the backslash and
1002 character to be printed as-is.
1003 .PP
1004 Any non-string expression in a print statement shall be assigned to
1005 \f[B]last\f[R], like any other expression that is printed.
1006 .SS Order of Evaluation
1007 .PP
1008 All expressions in a statment are evaluated left to right, except as
1009 necessary to maintain order of operations.
1010 This means, for example, assuming that \f[B]i\f[R] is equal to
1011 \f[B]0\f[R], in the expression
1012 .IP
1013 .nf
1014 \f[C]
1015 a[i++] = i++
1016 \f[R]
1017 .fi
1018 .PP
1019 the first (or 0th) element of \f[B]a\f[R] is set to \f[B]1\f[R], and
1020 \f[B]i\f[R] is equal to \f[B]2\f[R] at the end of the expression.
1021 .PP
1022 This includes function arguments.
1023 Thus, assuming \f[B]i\f[R] is equal to \f[B]0\f[R], this means that in
1024 the expression
1025 .IP
1026 .nf
1027 \f[C]
1028 x(i++, i++)
1029 \f[R]
1030 .fi
1031 .PP
1032 the first argument passed to \f[B]x()\f[R] is \f[B]0\f[R], and the
1033 second argument is \f[B]1\f[R], while \f[B]i\f[R] is equal to
1034 \f[B]2\f[R] before the function starts executing.
1035 .SH FUNCTIONS
1036 .PP
1037 Function definitions are as follows:
1038 .IP
1039 .nf
1040 \f[C]
1041 define I(I,...,I){
1042     auto I,...,I
1043     S;...;S
1044     return(E)
1045 }
1046 \f[R]
1047 .fi
1048 .PP
1049 Any \f[B]I\f[R] in the parameter list or \f[B]auto\f[R] list may be
1050 replaced with \f[B]I[]\f[R] to make a parameter or \f[B]auto\f[R] var an
1051 array, and any \f[B]I\f[R] in the parameter list may be replaced with
1052 \f[B]*I[]\f[R] to make a parameter an array reference.
1053 Callers of functions that take array references should not put an
1054 asterisk in the call; they must be called with just \f[B]I[]\f[R] like
1055 normal array parameters and will be automatically converted into
1056 references.
1057 .PP
1058 As a \f[B]non-portable extension\f[R], the opening brace of a
1059 \f[B]define\f[R] statement may appear on the next line.
1060 .PP
1061 As a \f[B]non-portable extension\f[R], the return statement may also be
1062 in one of the following forms:
1063 .IP "1." 3
1064 \f[B]return\f[R]
1065 .IP "2." 3
1066 \f[B]return\f[R] \f[B](\f[R] \f[B])\f[R]
1067 .IP "3." 3
1068 \f[B]return\f[R] \f[B]E\f[R]
1069 .PP
1070 The first two, or not specifying a \f[B]return\f[R] statement, is
1071 equivalent to \f[B]return (0)\f[R], unless the function is a
1072 \f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection
1073 below).
1074 .SS Void Functions
1075 .PP
1076 Functions can also be \f[B]void\f[R] functions, defined as follows:
1077 .IP
1078 .nf
1079 \f[C]
1080 define void I(I,...,I){
1081     auto I,...,I
1082     S;...;S
1083     return
1084 }
1085 \f[R]
1086 .fi
1087 .PP
1088 They can only be used as standalone expressions, where such an
1089 expression would be printed alone, except in a print statement.
1090 .PP
1091 Void functions can only use the first two \f[B]return\f[R] statements
1092 listed above.
1093 They can also omit the return statement entirely.
1094 .PP
1095 The word \[lq]void\[rq] is not treated as a keyword; it is still
1096 possible to have variables, arrays, and functions named \f[B]void\f[R].
1097 The word \[lq]void\[rq] is only treated specially right after the
1098 \f[B]define\f[R] keyword.
1099 .PP
1100 This is a \f[B]non-portable extension\f[R].
1101 .SS Array References
1102 .PP
1103 For any array in the parameter list, if the array is declared in the
1104 form
1105 .IP
1106 .nf
1107 \f[C]
1108 *I[]
1109 \f[R]
1110 .fi
1111 .PP
1112 it is a \f[B]reference\f[R].
1113 Any changes to the array in the function are reflected, when the
1114 function returns, to the array that was passed in.
1115 .PP
1116 Other than this, all function arguments are passed by value.
1117 .PP
1118 This is a \f[B]non-portable extension\f[R].
1119 .SH LIBRARY
1120 .PP
1121 All of the functions below, including the functions in the extended math
1122 library (see the \f[I]Extended Library\f[R] subsection below), are
1123 available when the \f[B]-l\f[R] or \f[B]\[en]mathlib\f[R] command-line
1124 flags are given, except that the extended math library is not available
1125 when the \f[B]-s\f[R] option, the \f[B]-w\f[R] option, or equivalents
1126 are given.
1127 .SS Standard Library
1128 .PP
1129 The
1130 standard (https://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html)
1131 defines the following functions for the math library:
1132 .TP
1133 \f[B]s(x)\f[R]
1134 Returns the sine of \f[B]x\f[R], which is assumed to be in radians.
1135 .RS
1136 .PP
1137 This is a transcendental function (see the \f[I]Transcendental
1138 Functions\f[R] subsection below).
1139 .RE
1140 .TP
1141 \f[B]c(x)\f[R]
1142 Returns the cosine of \f[B]x\f[R], which is assumed to be in radians.
1143 .RS
1144 .PP
1145 This is a transcendental function (see the \f[I]Transcendental
1146 Functions\f[R] subsection below).
1147 .RE
1148 .TP
1149 \f[B]a(x)\f[R]
1150 Returns the arctangent of \f[B]x\f[R], in radians.
1151 .RS
1152 .PP
1153 This is a transcendental function (see the \f[I]Transcendental
1154 Functions\f[R] subsection below).
1155 .RE
1156 .TP
1157 \f[B]l(x)\f[R]
1158 Returns the natural logarithm of \f[B]x\f[R].
1159 .RS
1160 .PP
1161 This is a transcendental function (see the \f[I]Transcendental
1162 Functions\f[R] subsection below).
1163 .RE
1164 .TP
1165 \f[B]e(x)\f[R]
1166 Returns the mathematical constant \f[B]e\f[R] raised to the power of
1167 \f[B]x\f[R].
1168 .RS
1169 .PP
1170 This is a transcendental function (see the \f[I]Transcendental
1171 Functions\f[R] subsection below).
1172 .RE
1173 .TP
1174 \f[B]j(x, n)\f[R]
1175 Returns the bessel integer order \f[B]n\f[R] (truncated) of \f[B]x\f[R].
1176 .RS
1177 .PP
1178 This is a transcendental function (see the \f[I]Transcendental
1179 Functions\f[R] subsection below).
1180 .RE
1181 .SS Extended Library
1182 .PP
1183 The extended library is \f[I]not\f[R] loaded when the
1184 \f[B]-s\f[R]/\f[B]\[en]standard\f[R] or \f[B]-w\f[R]/\f[B]\[en]warn\f[R]
1185 options are given since they are not part of the library defined by the
1186 standard (https://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html).
1187 .PP
1188 The extended library is a \f[B]non-portable extension\f[R].
1189 .TP
1190 \f[B]p(x, y)\f[R]
1191 Calculates \f[B]x\f[R] to the power of \f[B]y\f[R], even if \f[B]y\f[R]
1192 is not an integer, and returns the result to the current
1193 \f[B]scale\f[R].
1194 .RS
1195 .PP
1196 It is an error if \f[B]y\f[R] is negative and \f[B]x\f[R] is
1197 \f[B]0\f[R].
1198 .PP
1199 This is a transcendental function (see the \f[I]Transcendental
1200 Functions\f[R] subsection below).
1201 .RE
1202 .TP
1203 \f[B]r(x, p)\f[R]
1204 Returns \f[B]x\f[R] rounded to \f[B]p\f[R] decimal places according to
1205 the rounding mode round half away from
1206 \f[B]0\f[R] (https://en.wikipedia.org/wiki/Rounding#Round_half_away_from_zero).
1207 .TP
1208 \f[B]ceil(x, p)\f[R]
1209 Returns \f[B]x\f[R] rounded to \f[B]p\f[R] decimal places according to
1210 the rounding mode round away from
1211 \f[B]0\f[R] (https://en.wikipedia.org/wiki/Rounding#Rounding_away_from_zero).
1212 .TP
1213 \f[B]f(x)\f[R]
1214 Returns the factorial of the truncated absolute value of \f[B]x\f[R].
1215 .TP
1216 \f[B]perm(n, k)\f[R]
1217 Returns the permutation of the truncated absolute value of \f[B]n\f[R]
1218 of the truncated absolute value of \f[B]k\f[R], if \f[B]k <= n\f[R].
1219 If not, it returns \f[B]0\f[R].
1220 .TP
1221 \f[B]comb(n, k)\f[R]
1222 Returns the combination of the truncated absolute value of \f[B]n\f[R]
1223 of the truncated absolute value of \f[B]k\f[R], if \f[B]k <= n\f[R].
1224 If not, it returns \f[B]0\f[R].
1225 .TP
1226 \f[B]l2(x)\f[R]
1227 Returns the logarithm base \f[B]2\f[R] of \f[B]x\f[R].
1228 .RS
1229 .PP
1230 This is a transcendental function (see the \f[I]Transcendental
1231 Functions\f[R] subsection below).
1232 .RE
1233 .TP
1234 \f[B]l10(x)\f[R]
1235 Returns the logarithm base \f[B]10\f[R] of \f[B]x\f[R].
1236 .RS
1237 .PP
1238 This is a transcendental function (see the \f[I]Transcendental
1239 Functions\f[R] subsection below).
1240 .RE
1241 .TP
1242 \f[B]log(x, b)\f[R]
1243 Returns the logarithm base \f[B]b\f[R] of \f[B]x\f[R].
1244 .RS
1245 .PP
1246 This is a transcendental function (see the \f[I]Transcendental
1247 Functions\f[R] subsection below).
1248 .RE
1249 .TP
1250 \f[B]cbrt(x)\f[R]
1251 Returns the cube root of \f[B]x\f[R].
1252 .TP
1253 \f[B]root(x, n)\f[R]
1254 Calculates the truncated value of \f[B]n\f[R], \f[B]r\f[R], and returns
1255 the \f[B]r\f[R]th root of \f[B]x\f[R] to the current \f[B]scale\f[R].
1256 .RS
1257 .PP
1258 If \f[B]r\f[R] is \f[B]0\f[R] or negative, this raises an error and
1259 causes bc(1) to reset (see the \f[B]RESET\f[R] section).
1260 It also raises an error and causes bc(1) to reset if \f[B]r\f[R] is even
1261 and \f[B]x\f[R] is negative.
1262 .RE
1263 .TP
1264 \f[B]pi(p)\f[R]
1265 Returns \f[B]pi\f[R] to \f[B]p\f[R] decimal places.
1266 .RS
1267 .PP
1268 This is a transcendental function (see the \f[I]Transcendental
1269 Functions\f[R] subsection below).
1270 .RE
1271 .TP
1272 \f[B]t(x)\f[R]
1273 Returns the tangent of \f[B]x\f[R], which is assumed to be in radians.
1274 .RS
1275 .PP
1276 This is a transcendental function (see the \f[I]Transcendental
1277 Functions\f[R] subsection below).
1278 .RE
1279 .TP
1280 \f[B]a2(y, x)\f[R]
1281 Returns the arctangent of \f[B]y/x\f[R], in radians.
1282 If both \f[B]y\f[R] and \f[B]x\f[R] are equal to \f[B]0\f[R], it raises
1283 an error and causes bc(1) to reset (see the \f[B]RESET\f[R] section).
1284 Otherwise, if \f[B]x\f[R] is greater than \f[B]0\f[R], it returns
1285 \f[B]a(y/x)\f[R].
1286 If \f[B]x\f[R] is less than \f[B]0\f[R], and \f[B]y\f[R] is greater than
1287 or equal to \f[B]0\f[R], it returns \f[B]a(y/x)+pi\f[R].
1288 If \f[B]x\f[R] is less than \f[B]0\f[R], and \f[B]y\f[R] is less than
1289 \f[B]0\f[R], it returns \f[B]a(y/x)-pi\f[R].
1290 If \f[B]x\f[R] is equal to \f[B]0\f[R], and \f[B]y\f[R] is greater than
1291 \f[B]0\f[R], it returns \f[B]pi/2\f[R].
1292 If \f[B]x\f[R] is equal to \f[B]0\f[R], and \f[B]y\f[R] is less than
1293 \f[B]0\f[R], it returns \f[B]-pi/2\f[R].
1294 .RS
1295 .PP
1296 This function is the same as the \f[B]atan2()\f[R] function in many
1297 programming languages.
1298 .PP
1299 This is a transcendental function (see the \f[I]Transcendental
1300 Functions\f[R] subsection below).
1301 .RE
1302 .TP
1303 \f[B]sin(x)\f[R]
1304 Returns the sine of \f[B]x\f[R], which is assumed to be in radians.
1305 .RS
1306 .PP
1307 This is an alias of \f[B]s(x)\f[R].
1308 .PP
1309 This is a transcendental function (see the \f[I]Transcendental
1310 Functions\f[R] subsection below).
1311 .RE
1312 .TP
1313 \f[B]cos(x)\f[R]
1314 Returns the cosine of \f[B]x\f[R], which is assumed to be in radians.
1315 .RS
1316 .PP
1317 This is an alias of \f[B]c(x)\f[R].
1318 .PP
1319 This is a transcendental function (see the \f[I]Transcendental
1320 Functions\f[R] subsection below).
1321 .RE
1322 .TP
1323 \f[B]tan(x)\f[R]
1324 Returns the tangent of \f[B]x\f[R], which is assumed to be in radians.
1325 .RS
1326 .PP
1327 If \f[B]x\f[R] is equal to \f[B]1\f[R] or \f[B]-1\f[R], this raises an
1328 error and causes bc(1) to reset (see the \f[B]RESET\f[R] section).
1329 .PP
1330 This is an alias of \f[B]t(x)\f[R].
1331 .PP
1332 This is a transcendental function (see the \f[I]Transcendental
1333 Functions\f[R] subsection below).
1334 .RE
1335 .TP
1336 \f[B]atan(x)\f[R]
1337 Returns the arctangent of \f[B]x\f[R], in radians.
1338 .RS
1339 .PP
1340 This is an alias of \f[B]a(x)\f[R].
1341 .PP
1342 This is a transcendental function (see the \f[I]Transcendental
1343 Functions\f[R] subsection below).
1344 .RE
1345 .TP
1346 \f[B]atan2(y, x)\f[R]
1347 Returns the arctangent of \f[B]y/x\f[R], in radians.
1348 If both \f[B]y\f[R] and \f[B]x\f[R] are equal to \f[B]0\f[R], it raises
1349 an error and causes bc(1) to reset (see the \f[B]RESET\f[R] section).
1350 Otherwise, if \f[B]x\f[R] is greater than \f[B]0\f[R], it returns
1351 \f[B]a(y/x)\f[R].
1352 If \f[B]x\f[R] is less than \f[B]0\f[R], and \f[B]y\f[R] is greater than
1353 or equal to \f[B]0\f[R], it returns \f[B]a(y/x)+pi\f[R].
1354 If \f[B]x\f[R] is less than \f[B]0\f[R], and \f[B]y\f[R] is less than
1355 \f[B]0\f[R], it returns \f[B]a(y/x)-pi\f[R].
1356 If \f[B]x\f[R] is equal to \f[B]0\f[R], and \f[B]y\f[R] is greater than
1357 \f[B]0\f[R], it returns \f[B]pi/2\f[R].
1358 If \f[B]x\f[R] is equal to \f[B]0\f[R], and \f[B]y\f[R] is less than
1359 \f[B]0\f[R], it returns \f[B]-pi/2\f[R].
1360 .RS
1361 .PP
1362 This function is the same as the \f[B]atan2()\f[R] function in many
1363 programming languages.
1364 .PP
1365 This is an alias of \f[B]a2(y, x)\f[R].
1366 .PP
1367 This is a transcendental function (see the \f[I]Transcendental
1368 Functions\f[R] subsection below).
1369 .RE
1370 .TP
1371 \f[B]r2d(x)\f[R]
1372 Converts \f[B]x\f[R] from radians to degrees and returns the result.
1373 .RS
1374 .PP
1375 This is a transcendental function (see the \f[I]Transcendental
1376 Functions\f[R] subsection below).
1377 .RE
1378 .TP
1379 \f[B]d2r(x)\f[R]
1380 Converts \f[B]x\f[R] from degrees to radians and returns the result.
1381 .RS
1382 .PP
1383 This is a transcendental function (see the \f[I]Transcendental
1384 Functions\f[R] subsection below).
1385 .RE
1386 .TP
1387 \f[B]frand(p)\f[R]
1388 Generates a pseudo-random number between \f[B]0\f[R] (inclusive) and
1389 \f[B]1\f[R] (exclusive) with the number of decimal digits after the
1390 decimal point equal to the truncated absolute value of \f[B]p\f[R].
1391 If \f[B]p\f[R] is not \f[B]0\f[R], then calling this function will
1392 change the value of \f[B]seed\f[R].
1393 If \f[B]p\f[R] is \f[B]0\f[R], then \f[B]0\f[R] is returned, and
1394 \f[B]seed\f[R] is \f[I]not\f[R] changed.
1395 .TP
1396 \f[B]ifrand(i, p)\f[R]
1397 Generates a pseudo-random number that is between \f[B]0\f[R] (inclusive)
1398 and the truncated absolute value of \f[B]i\f[R] (exclusive) with the
1399 number of decimal digits after the decimal point equal to the truncated
1400 absolute value of \f[B]p\f[R].
1401 If the absolute value of \f[B]i\f[R] is greater than or equal to
1402 \f[B]2\f[R], and \f[B]p\f[R] is not \f[B]0\f[R], then calling this
1403 function will change the value of \f[B]seed\f[R]; otherwise, \f[B]0\f[R]
1404 is returned and \f[B]seed\f[R] is not changed.
1405 .TP
1406 \f[B]srand(x)\f[R]
1407 Returns \f[B]x\f[R] with its sign flipped with probability
1408 \f[B]0.5\f[R].
1409 In other words, it randomizes the sign of \f[B]x\f[R].
1410 .TP
1411 \f[B]brand()\f[R]
1412 Returns a random boolean value (either \f[B]0\f[R] or \f[B]1\f[R]).
1413 .TP
1414 \f[B]ubytes(x)\f[R]
1415 Returns the numbers of unsigned integer bytes required to hold the
1416 truncated absolute value of \f[B]x\f[R].
1417 .TP
1418 \f[B]sbytes(x)\f[R]
1419 Returns the numbers of signed, two\[cq]s-complement integer bytes
1420 required to hold the truncated value of \f[B]x\f[R].
1421 .TP
1422 \f[B]hex(x)\f[R]
1423 Outputs the hexadecimal (base \f[B]16\f[R]) representation of
1424 \f[B]x\f[R].
1425 .RS
1426 .PP
1427 This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R]
1428 subsection of the \f[B]FUNCTIONS\f[R] section).
1429 .RE
1430 .TP
1431 \f[B]binary(x)\f[R]
1432 Outputs the binary (base \f[B]2\f[R]) representation of \f[B]x\f[R].
1433 .RS
1434 .PP
1435 This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R]
1436 subsection of the \f[B]FUNCTIONS\f[R] section).
1437 .RE
1438 .TP
1439 \f[B]output(x, b)\f[R]
1440 Outputs the base \f[B]b\f[R] representation of \f[B]x\f[R].
1441 .RS
1442 .PP
1443 This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R]
1444 subsection of the \f[B]FUNCTIONS\f[R] section).
1445 .RE
1446 .TP
1447 \f[B]uint(x)\f[R]
1448 Outputs the representation, in binary and hexadecimal, of \f[B]x\f[R] as
1449 an unsigned integer in as few power of two bytes as possible.
1450 Both outputs are split into bytes separated by spaces.
1451 .RS
1452 .PP
1453 If \f[B]x\f[R] is not an integer or is negative, an error message is
1454 printed instead, but bc(1) is not reset (see the \f[B]RESET\f[R]
1455 section).
1456 .PP
1457 This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R]
1458 subsection of the \f[B]FUNCTIONS\f[R] section).
1459 .RE
1460 .TP
1461 \f[B]int(x)\f[R]
1462 Outputs the representation, in binary and hexadecimal, of \f[B]x\f[R] as
1463 a signed, two\[cq]s-complement integer in as few power of two bytes as
1464 possible.
1465 Both outputs are split into bytes separated by spaces.
1466 .RS
1467 .PP
1468 If \f[B]x\f[R] is not an integer, an error message is printed instead,
1469 but bc(1) is not reset (see the \f[B]RESET\f[R] section).
1470 .PP
1471 This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R]
1472 subsection of the \f[B]FUNCTIONS\f[R] section).
1473 .RE
1474 .TP
1475 \f[B]uintn(x, n)\f[R]
1476 Outputs the representation, in binary and hexadecimal, of \f[B]x\f[R] as
1477 an unsigned integer in \f[B]n\f[R] bytes.
1478 Both outputs are split into bytes separated by spaces.
1479 .RS
1480 .PP
1481 If \f[B]x\f[R] is not an integer, is negative, or cannot fit into
1482 \f[B]n\f[R] bytes, an error message is printed instead, but bc(1) is not
1483 reset (see the \f[B]RESET\f[R] section).
1484 .PP
1485 This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R]
1486 subsection of the \f[B]FUNCTIONS\f[R] section).
1487 .RE
1488 .TP
1489 \f[B]intn(x, n)\f[R]
1490 Outputs the representation, in binary and hexadecimal, of \f[B]x\f[R] as
1491 a signed, two\[cq]s-complement integer in \f[B]n\f[R] bytes.
1492 Both outputs are split into bytes separated by spaces.
1493 .RS
1494 .PP
1495 If \f[B]x\f[R] is not an integer or cannot fit into \f[B]n\f[R] bytes,
1496 an error message is printed instead, but bc(1) is not reset (see the
1497 \f[B]RESET\f[R] section).
1498 .PP
1499 This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R]
1500 subsection of the \f[B]FUNCTIONS\f[R] section).
1501 .RE
1502 .TP
1503 \f[B]uint8(x)\f[R]
1504 Outputs the representation, in binary and hexadecimal, of \f[B]x\f[R] as
1505 an unsigned integer in \f[B]1\f[R] byte.
1506 Both outputs are split into bytes separated by spaces.
1507 .RS
1508 .PP
1509 If \f[B]x\f[R] is not an integer, is negative, or cannot fit into
1510 \f[B]1\f[R] byte, an error message is printed instead, but bc(1) is not
1511 reset (see the \f[B]RESET\f[R] section).
1512 .PP
1513 This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R]
1514 subsection of the \f[B]FUNCTIONS\f[R] section).
1515 .RE
1516 .TP
1517 \f[B]int8(x)\f[R]
1518 Outputs the representation, in binary and hexadecimal, of \f[B]x\f[R] as
1519 a signed, two\[cq]s-complement integer in \f[B]1\f[R] byte.
1520 Both outputs are split into bytes separated by spaces.
1521 .RS
1522 .PP
1523 If \f[B]x\f[R] is not an integer or cannot fit into \f[B]1\f[R] byte, an
1524 error message is printed instead, but bc(1) is not reset (see the
1525 \f[B]RESET\f[R] section).
1526 .PP
1527 This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R]
1528 subsection of the \f[B]FUNCTIONS\f[R] section).
1529 .RE
1530 .TP
1531 \f[B]uint16(x)\f[R]
1532 Outputs the representation, in binary and hexadecimal, of \f[B]x\f[R] as
1533 an unsigned integer in \f[B]2\f[R] bytes.
1534 Both outputs are split into bytes separated by spaces.
1535 .RS
1536 .PP
1537 If \f[B]x\f[R] is not an integer, is negative, or cannot fit into
1538 \f[B]2\f[R] bytes, an error message is printed instead, but bc(1) is not
1539 reset (see the \f[B]RESET\f[R] section).
1540 .PP
1541 This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R]
1542 subsection of the \f[B]FUNCTIONS\f[R] section).
1543 .RE
1544 .TP
1545 \f[B]int16(x)\f[R]
1546 Outputs the representation, in binary and hexadecimal, of \f[B]x\f[R] as
1547 a signed, two\[cq]s-complement integer in \f[B]2\f[R] bytes.
1548 Both outputs are split into bytes separated by spaces.
1549 .RS
1550 .PP
1551 If \f[B]x\f[R] is not an integer or cannot fit into \f[B]2\f[R] bytes,
1552 an error message is printed instead, but bc(1) is not reset (see the
1553 \f[B]RESET\f[R] section).
1554 .PP
1555 This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R]
1556 subsection of the \f[B]FUNCTIONS\f[R] section).
1557 .RE
1558 .TP
1559 \f[B]uint32(x)\f[R]
1560 Outputs the representation, in binary and hexadecimal, of \f[B]x\f[R] as
1561 an unsigned integer in \f[B]4\f[R] bytes.
1562 Both outputs are split into bytes separated by spaces.
1563 .RS
1564 .PP
1565 If \f[B]x\f[R] is not an integer, is negative, or cannot fit into
1566 \f[B]4\f[R] bytes, an error message is printed instead, but bc(1) is not
1567 reset (see the \f[B]RESET\f[R] section).
1568 .PP
1569 This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R]
1570 subsection of the \f[B]FUNCTIONS\f[R] section).
1571 .RE
1572 .TP
1573 \f[B]int32(x)\f[R]
1574 Outputs the representation, in binary and hexadecimal, of \f[B]x\f[R] as
1575 a signed, two\[cq]s-complement integer in \f[B]4\f[R] bytes.
1576 Both outputs are split into bytes separated by spaces.
1577 .RS
1578 .PP
1579 If \f[B]x\f[R] is not an integer or cannot fit into \f[B]4\f[R] bytes,
1580 an error message is printed instead, but bc(1) is not reset (see the
1581 \f[B]RESET\f[R] section).
1582 .PP
1583 This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R]
1584 subsection of the \f[B]FUNCTIONS\f[R] section).
1585 .RE
1586 .TP
1587 \f[B]uint64(x)\f[R]
1588 Outputs the representation, in binary and hexadecimal, of \f[B]x\f[R] as
1589 an unsigned integer in \f[B]8\f[R] bytes.
1590 Both outputs are split into bytes separated by spaces.
1591 .RS
1592 .PP
1593 If \f[B]x\f[R] is not an integer, is negative, or cannot fit into
1594 \f[B]8\f[R] bytes, an error message is printed instead, but bc(1) is not
1595 reset (see the \f[B]RESET\f[R] section).
1596 .PP
1597 This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R]
1598 subsection of the \f[B]FUNCTIONS\f[R] section).
1599 .RE
1600 .TP
1601 \f[B]int64(x)\f[R]
1602 Outputs the representation, in binary and hexadecimal, of \f[B]x\f[R] as
1603 a signed, two\[cq]s-complement integer in \f[B]8\f[R] bytes.
1604 Both outputs are split into bytes separated by spaces.
1605 .RS
1606 .PP
1607 If \f[B]x\f[R] is not an integer or cannot fit into \f[B]8\f[R] bytes,
1608 an error message is printed instead, but bc(1) is not reset (see the
1609 \f[B]RESET\f[R] section).
1610 .PP
1611 This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R]
1612 subsection of the \f[B]FUNCTIONS\f[R] section).
1613 .RE
1614 .TP
1615 \f[B]hex_uint(x, n)\f[R]
1616 Outputs the representation of the truncated absolute value of
1617 \f[B]x\f[R] as an unsigned integer in hexadecimal using \f[B]n\f[R]
1618 bytes.
1619 Not all of the value will be output if \f[B]n\f[R] is too small.
1620 .RS
1621 .PP
1622 This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R]
1623 subsection of the \f[B]FUNCTIONS\f[R] section).
1624 .RE
1625 .TP
1626 \f[B]binary_uint(x, n)\f[R]
1627 Outputs the representation of the truncated absolute value of
1628 \f[B]x\f[R] as an unsigned integer in binary using \f[B]n\f[R] bytes.
1629 Not all of the value will be output if \f[B]n\f[R] is too small.
1630 .RS
1631 .PP
1632 This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R]
1633 subsection of the \f[B]FUNCTIONS\f[R] section).
1634 .RE
1635 .TP
1636 \f[B]output_uint(x, n)\f[R]
1637 Outputs the representation of the truncated absolute value of
1638 \f[B]x\f[R] as an unsigned integer in the current \f[B]obase\f[R] (see
1639 the \f[B]SYNTAX\f[R] section) using \f[B]n\f[R] bytes.
1640 Not all of the value will be output if \f[B]n\f[R] is too small.
1641 .RS
1642 .PP
1643 This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R]
1644 subsection of the \f[B]FUNCTIONS\f[R] section).
1645 .RE
1646 .TP
1647 \f[B]output_byte(x, i)\f[R]
1648 Outputs byte \f[B]i\f[R] of the truncated absolute value of \f[B]x\f[R],
1649 where \f[B]0\f[R] is the least significant byte and \f[B]number_of_bytes
1650 - 1\f[R] is the most significant byte.
1651 .RS
1652 .PP
1653 This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R]
1654 subsection of the \f[B]FUNCTIONS\f[R] section).
1655 .RE
1656 .SS Transcendental Functions
1657 .PP
1658 All transcendental functions can return slightly inaccurate results (up
1659 to 1 ULP (https://en.wikipedia.org/wiki/Unit_in_the_last_place)).
1660 This is unavoidable, and this
1661 article (https://people.eecs.berkeley.edu/~wkahan/LOG10HAF.TXT) explains
1662 why it is impossible and unnecessary to calculate exact results for the
1663 transcendental functions.
1664 .PP
1665 Because of the possible inaccuracy, I recommend that users call those
1666 functions with the precision (\f[B]scale\f[R]) set to at least 1 higher
1667 than is necessary.
1668 If exact results are \f[I]absolutely\f[R] required, users can double the
1669 precision (\f[B]scale\f[R]) and then truncate.
1670 .PP
1671 The transcendental functions in the standard math library are:
1672 .IP \[bu] 2
1673 \f[B]s(x)\f[R]
1674 .IP \[bu] 2
1675 \f[B]c(x)\f[R]
1676 .IP \[bu] 2
1677 \f[B]a(x)\f[R]
1678 .IP \[bu] 2
1679 \f[B]l(x)\f[R]
1680 .IP \[bu] 2
1681 \f[B]e(x)\f[R]
1682 .IP \[bu] 2
1683 \f[B]j(x, n)\f[R]
1684 .PP
1685 The transcendental functions in the extended math library are:
1686 .IP \[bu] 2
1687 \f[B]l2(x)\f[R]
1688 .IP \[bu] 2
1689 \f[B]l10(x)\f[R]
1690 .IP \[bu] 2
1691 \f[B]log(x, b)\f[R]
1692 .IP \[bu] 2
1693 \f[B]pi(p)\f[R]
1694 .IP \[bu] 2
1695 \f[B]t(x)\f[R]
1696 .IP \[bu] 2
1697 \f[B]a2(y, x)\f[R]
1698 .IP \[bu] 2
1699 \f[B]sin(x)\f[R]
1700 .IP \[bu] 2
1701 \f[B]cos(x)\f[R]
1702 .IP \[bu] 2
1703 \f[B]tan(x)\f[R]
1704 .IP \[bu] 2
1705 \f[B]atan(x)\f[R]
1706 .IP \[bu] 2
1707 \f[B]atan2(y, x)\f[R]
1708 .IP \[bu] 2
1709 \f[B]r2d(x)\f[R]
1710 .IP \[bu] 2
1711 \f[B]d2r(x)\f[R]
1712 .SH RESET
1713 .PP
1714 When bc(1) encounters an error or a signal that it has a non-default
1715 handler for, it resets.
1716 This means that several things happen.
1717 .PP
1718 First, any functions that are executing are stopped and popped off the
1719 stack.
1720 The behavior is not unlike that of exceptions in programming languages.
1721 Then the execution point is set so that any code waiting to execute
1722 (after all functions returned) is skipped.
1723 .PP
1724 Thus, when bc(1) resets, it skips any remaining code waiting to be
1725 executed.
1726 Then, if it is interactive mode, and the error was not a fatal error
1727 (see the \f[B]EXIT STATUS\f[R] section), it asks for more input;
1728 otherwise, it exits with the appropriate return code.
1729 .PP
1730 Note that this reset behavior is different from the GNU bc(1), which
1731 attempts to start executing the statement right after the one that
1732 caused an error.
1733 .SH PERFORMANCE
1734 .PP
1735 Most bc(1) implementations use \f[B]char\f[R] types to calculate the
1736 value of \f[B]1\f[R] decimal digit at a time, but that can be slow.
1737 This bc(1) does something different.
1738 .PP
1739 It uses large integers to calculate more than \f[B]1\f[R] decimal digit
1740 at a time.
1741 If built in a environment where \f[B]BC_LONG_BIT\f[R] (see the
1742 \f[B]LIMITS\f[R] section) is \f[B]64\f[R], then each integer has
1743 \f[B]9\f[R] decimal digits.
1744 If built in an environment where \f[B]BC_LONG_BIT\f[R] is \f[B]32\f[R]
1745 then each integer has \f[B]4\f[R] decimal digits.
1746 This value (the number of decimal digits per large integer) is called
1747 \f[B]BC_BASE_DIGS\f[R].
1748 .PP
1749 The actual values of \f[B]BC_LONG_BIT\f[R] and \f[B]BC_BASE_DIGS\f[R]
1750 can be queried with the \f[B]limits\f[R] statement.
1751 .PP
1752 In addition, this bc(1) uses an even larger integer for overflow
1753 checking.
1754 This integer type depends on the value of \f[B]BC_LONG_BIT\f[R], but is
1755 always at least twice as large as the integer type used to store digits.
1756 .SH LIMITS
1757 .PP
1758 The following are the limits on bc(1):
1759 .TP
1760 \f[B]BC_LONG_BIT\f[R]
1761 The number of bits in the \f[B]long\f[R] type in the environment where
1762 bc(1) was built.
1763 This determines how many decimal digits can be stored in a single large
1764 integer (see the \f[B]PERFORMANCE\f[R] section).
1765 .TP
1766 \f[B]BC_BASE_DIGS\f[R]
1767 The number of decimal digits per large integer (see the
1768 \f[B]PERFORMANCE\f[R] section).
1769 Depends on \f[B]BC_LONG_BIT\f[R].
1770 .TP
1771 \f[B]BC_BASE_POW\f[R]
1772 The max decimal number that each large integer can store (see
1773 \f[B]BC_BASE_DIGS\f[R]) plus \f[B]1\f[R].
1774 Depends on \f[B]BC_BASE_DIGS\f[R].
1775 .TP
1776 \f[B]BC_OVERFLOW_MAX\f[R]
1777 The max number that the overflow type (see the \f[B]PERFORMANCE\f[R]
1778 section) can hold.
1779 Depends on \f[B]BC_LONG_BIT\f[R].
1780 .TP
1781 \f[B]BC_BASE_MAX\f[R]
1782 The maximum output base.
1783 Set at \f[B]BC_BASE_POW\f[R].
1784 .TP
1785 \f[B]BC_DIM_MAX\f[R]
1786 The maximum size of arrays.
1787 Set at \f[B]SIZE_MAX-1\f[R].
1788 .TP
1789 \f[B]BC_SCALE_MAX\f[R]
1790 The maximum \f[B]scale\f[R].
1791 Set at \f[B]BC_OVERFLOW_MAX-1\f[R].
1792 .TP
1793 \f[B]BC_STRING_MAX\f[R]
1794 The maximum length of strings.
1795 Set at \f[B]BC_OVERFLOW_MAX-1\f[R].
1796 .TP
1797 \f[B]BC_NAME_MAX\f[R]
1798 The maximum length of identifiers.
1799 Set at \f[B]BC_OVERFLOW_MAX-1\f[R].
1800 .TP
1801 \f[B]BC_NUM_MAX\f[R]
1802 The maximum length of a number (in decimal digits), which includes
1803 digits after the decimal point.
1804 Set at \f[B]BC_OVERFLOW_MAX-1\f[R].
1805 .TP
1806 \f[B]BC_RAND_MAX\f[R]
1807 The maximum integer (inclusive) returned by the \f[B]rand()\f[R]
1808 operand.
1809 Set at \f[B]2\[ha]BC_LONG_BIT-1\f[R].
1810 .TP
1811 Exponent
1812 The maximum allowable exponent (positive or negative).
1813 Set at \f[B]BC_OVERFLOW_MAX\f[R].
1814 .TP
1815 Number of vars
1816 The maximum number of vars/arrays.
1817 Set at \f[B]SIZE_MAX-1\f[R].
1818 .PP
1819 The actual values can be queried with the \f[B]limits\f[R] statement.
1820 .PP
1821 These limits are meant to be effectively non-existent; the limits are so
1822 large (at least on 64-bit machines) that there should not be any point
1823 at which they become a problem.
1824 In fact, memory should be exhausted before these limits should be hit.
1825 .SH ENVIRONMENT VARIABLES
1826 .PP
1827 bc(1) recognizes the following environment variables:
1828 .TP
1829 \f[B]POSIXLY_CORRECT\f[R]
1830 If this variable exists (no matter the contents), bc(1) behaves as if
1831 the \f[B]-s\f[R] option was given.
1832 .TP
1833 \f[B]BC_ENV_ARGS\f[R]
1834 This is another way to give command-line arguments to bc(1).
1835 They should be in the same format as all other command-line arguments.
1836 These are always processed first, so any files given in
1837 \f[B]BC_ENV_ARGS\f[R] will be processed before arguments and files given
1838 on the command-line.
1839 This gives the user the ability to set up \[lq]standard\[rq] options and
1840 files to be used at every invocation.
1841 The most useful thing for such files to contain would be useful
1842 functions that the user might want every time bc(1) runs.
1843 .RS
1844 .PP
1845 The code that parses \f[B]BC_ENV_ARGS\f[R] will correctly handle quoted
1846 arguments, but it does not understand escape sequences.
1847 For example, the string \f[B]\[lq]/home/gavin/some bc file.bc\[rq]\f[R]
1848 will be correctly parsed, but the string \f[B]\[lq]/home/gavin/some
1849 \[dq]bc\[dq] file.bc\[rq]\f[R] will include the backslashes.
1850 .PP
1851 The quote parsing will handle either kind of quotes, \f[B]\[cq]\f[R] or
1852 \f[B]\[lq]\f[R]. Thus, if you have a file with any number of single
1853 quotes in the name, you can use double quotes as the outside quotes, as
1854 in \f[B]\[rq]some `bc' file.bc\[dq]\f[R], and vice versa if you have a
1855 file with double quotes.
1856 However, handling a file with both kinds of quotes in
1857 \f[B]BC_ENV_ARGS\f[R] is not supported due to the complexity of the
1858 parsing, though such files are still supported on the command-line where
1859 the parsing is done by the shell.
1860 .RE
1861 .TP
1862 \f[B]BC_LINE_LENGTH\f[R]
1863 If this environment variable exists and contains an integer that is
1864 greater than \f[B]1\f[R] and is less than \f[B]UINT16_MAX\f[R]
1865 (\f[B]2\[ha]16-1\f[R]), bc(1) will output lines to that length,
1866 including the backslash (\f[B]\[rs]\f[R]).
1867 The default line length is \f[B]70\f[R].
1868 .SH EXIT STATUS
1869 .PP
1870 bc(1) returns the following exit statuses:
1871 .TP
1872 \f[B]0\f[R]
1873 No error.
1874 .TP
1875 \f[B]1\f[R]
1876 A math error occurred.
1877 This follows standard practice of using \f[B]1\f[R] for expected errors,
1878 since math errors will happen in the process of normal execution.
1879 .RS
1880 .PP
1881 Math errors include divide by \f[B]0\f[R], taking the square root of a
1882 negative number, using a negative number as a bound for the
1883 pseudo-random number generator, attempting to convert a negative number
1884 to a hardware integer, overflow when converting a number to a hardware
1885 integer, and attempting to use a non-integer where an integer is
1886 required.
1887 .PP
1888 Converting to a hardware integer happens for the second operand of the
1889 power (\f[B]\[ha]\f[R]), places (\f[B]\[at]\f[R]), left shift
1890 (\f[B]<<\f[R]), and right shift (\f[B]>>\f[R]) operators and their
1891 corresponding assignment operators.
1892 .RE
1893 .TP
1894 \f[B]2\f[R]
1895 A parse error occurred.
1896 .RS
1897 .PP
1898 Parse errors include unexpected \f[B]EOF\f[R], using an invalid
1899 character, failing to find the end of a string or comment, using a token
1900 where it is invalid, giving an invalid expression, giving an invalid
1901 print statement, giving an invalid function definition, attempting to
1902 assign to an expression that is not a named expression (see the
1903 \f[I]Named Expressions\f[R] subsection of the \f[B]SYNTAX\f[R] section),
1904 giving an invalid \f[B]auto\f[R] list, having a duplicate
1905 \f[B]auto\f[R]/function parameter, failing to find the end of a code
1906 block, attempting to return a value from a \f[B]void\f[R] function,
1907 attempting to use a variable as a reference, and using any extensions
1908 when the option \f[B]-s\f[R] or any equivalents were given.
1909 .RE
1910 .TP
1911 \f[B]3\f[R]
1912 A runtime error occurred.
1913 .RS
1914 .PP
1915 Runtime errors include assigning an invalid number to \f[B]ibase\f[R],
1916 \f[B]obase\f[R], or \f[B]scale\f[R]; give a bad expression to a
1917 \f[B]read()\f[R] call, calling \f[B]read()\f[R] inside of a
1918 \f[B]read()\f[R] call, type errors, passing the wrong number of
1919 arguments to functions, attempting to call an undefined function, and
1920 attempting to use a \f[B]void\f[R] function call as a value in an
1921 expression.
1922 .RE
1923 .TP
1924 \f[B]4\f[R]
1925 A fatal error occurred.
1926 .RS
1927 .PP
1928 Fatal errors include memory allocation errors, I/O errors, failing to
1929 open files, attempting to use files that do not have only ASCII
1930 characters (bc(1) only accepts ASCII characters), attempting to open a
1931 directory as a file, and giving invalid command-line options.
1932 .RE
1933 .PP
1934 The exit status \f[B]4\f[R] is special; when a fatal error occurs, bc(1)
1935 always exits and returns \f[B]4\f[R], no matter what mode bc(1) is in.
1936 .PP
1937 The other statuses will only be returned when bc(1) is not in
1938 interactive mode (see the \f[B]INTERACTIVE MODE\f[R] section), since
1939 bc(1) resets its state (see the \f[B]RESET\f[R] section) and accepts
1940 more input when one of those errors occurs in interactive mode.
1941 This is also the case when interactive mode is forced by the
1942 \f[B]-i\f[R] flag or \f[B]\[en]interactive\f[R] option.
1943 .PP
1944 These exit statuses allow bc(1) to be used in shell scripting with error
1945 checking, and its normal behavior can be forced by using the
1946 \f[B]-i\f[R] flag or \f[B]\[en]interactive\f[R] option.
1947 .SH INTERACTIVE MODE
1948 .PP
1949 Per the
1950 standard (https://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html),
1951 bc(1) has an interactive mode and a non-interactive mode.
1952 Interactive mode is turned on automatically when both \f[B]stdin\f[R]
1953 and \f[B]stdout\f[R] are hooked to a terminal, but the \f[B]-i\f[R] flag
1954 and \f[B]\[en]interactive\f[R] option can turn it on in other cases.
1955 .PP
1956 In interactive mode, bc(1) attempts to recover from errors (see the
1957 \f[B]RESET\f[R] section), and in normal execution, flushes
1958 \f[B]stdout\f[R] as soon as execution is done for the current input.
1959 .SH TTY MODE
1960 .PP
1961 If \f[B]stdin\f[R], \f[B]stdout\f[R], and \f[B]stderr\f[R] are all
1962 connected to a TTY, bc(1) turns on \[lq]TTY mode.\[rq]
1963 .PP
1964 The prompt is enabled in TTY mode.
1965 .PP
1966 TTY mode is different from interactive mode because interactive mode is
1967 required in the bc(1)
1968 specification (https://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html),
1969 and interactive mode requires only \f[B]stdin\f[R] and \f[B]stdout\f[R]
1970 to be connected to a terminal.
1971 .SH SIGNAL HANDLING
1972 .PP
1973 Sending a \f[B]SIGINT\f[R] will cause bc(1) to stop execution of the
1974 current input.
1975 If bc(1) is in TTY mode (see the \f[B]TTY MODE\f[R] section), it will
1976 reset (see the \f[B]RESET\f[R] section).
1977 Otherwise, it will clean up and exit.
1978 .PP
1979 Note that \[lq]current input\[rq] can mean one of two things.
1980 If bc(1) is processing input from \f[B]stdin\f[R] in TTY mode, it will
1981 ask for more input.
1982 If bc(1) is processing input from a file in TTY mode, it will stop
1983 processing the file and start processing the next file, if one exists,
1984 or ask for input from \f[B]stdin\f[R] if no other file exists.
1985 .PP
1986 This means that if a \f[B]SIGINT\f[R] is sent to bc(1) as it is
1987 executing a file, it can seem as though bc(1) did not respond to the
1988 signal since it will immediately start executing the next file.
1989 This is by design; most files that users execute when interacting with
1990 bc(1) have function definitions, which are quick to parse.
1991 If a file takes a long time to execute, there may be a bug in that file.
1992 The rest of the files could still be executed without problem, allowing
1993 the user to continue.
1994 .PP
1995 \f[B]SIGTERM\f[R] and \f[B]SIGQUIT\f[R] cause bc(1) to clean up and
1996 exit, and it uses the default handler for all other signals.
1997 .SH SEE ALSO
1998 .PP
1999 dc(1)
2000 .SH STANDARDS
2001 .PP
2002 bc(1) is compliant with the IEEE Std 1003.1-2017
2003 (\[lq]POSIX.1-2017\[rq]) (https://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html)
2004 specification.
2005 The flags \f[B]-efghiqsvVw\f[R], all long options, and the extensions
2006 noted above are extensions to that specification.
2007 .PP
2008 Note that the specification explicitly says that bc(1) only accepts
2009 numbers that use a period (\f[B].\f[R]) as a radix point, regardless of
2010 the value of \f[B]LC_NUMERIC\f[R].
2011 .SH BUGS
2012 .PP
2013 None are known.
2014 Report bugs at https://git.yzena.com/gavin/bc.
2015 .SH AUTHORS
2016 .PP
2017 Gavin D.
2018 Howard <gavin@yzena.com> and contributors.