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