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