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