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