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