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