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