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