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