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