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