]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/bc/manuals/bc/N.1
contrib/bc: merge version 5.1.0 from vendor branch
[FreeBSD/FreeBSD.git] / contrib / bc / manuals / bc / N.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" "June 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]-ghilPqRsvVw\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]--no-read-prompt\f[R]] [\f[B]--quiet\f[R]]
37 [\f[B]--standard\f[R]] [\f[B]--warn\f[R]] [\f[B]--version\f[R]]
38 [\f[B]-e\f[R] \f[I]expr\f[R]]
39 [\f[B]--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 .PP
55 This bc(1) is a drop-in replacement for \f[I]any\f[R] bc(1), including
56 (and especially) the GNU bc(1).
57 It also has many extensions and extra features beyond other
58 implementations.
59 .PP
60 \f[B]Note\f[R]: If running this bc(1) on \f[I]any\f[R] script meant for
61 another bc(1) gives a parse error, it is probably because a word this
62 bc(1) reserves as a keyword is used as the name of a function, variable,
63 or array.
64 To fix that, use the command-line option \f[B]-r\f[R] \f[I]keyword\f[R],
65 where \f[I]keyword\f[R] is the keyword that is used as a name in the
66 script.
67 For more information, see the \f[B]OPTIONS\f[R] section.
68 .PP
69 If parsing scripts meant for other bc(1) implementations still does not
70 work, that is a bug and should be reported.
71 See the \f[B]BUGS\f[R] section.
72 .SH OPTIONS
73 .PP
74 The following are the options that bc(1) accepts.
75 .TP
76 \f[B]-g\f[R], \f[B]--global-stacks\f[R]
77 Turns the globals \f[B]ibase\f[R], \f[B]obase\f[R], \f[B]scale\f[R], and
78 \f[B]seed\f[R] into stacks.
79 .RS
80 .PP
81 This has the effect that a copy of the current value of all four are
82 pushed onto a stack for every function call, as well as popped when
83 every function returns.
84 This means that functions can assign to any and all of those globals
85 without worrying that the change will affect other functions.
86 Thus, a hypothetical function named \f[B]output(x,b)\f[R] that simply
87 printed \f[B]x\f[R] in base \f[B]b\f[R] could be written like this:
88 .IP
89 .nf
90 \f[C]
91 define void output(x, b) {
92     obase=b
93     x
94 }
95 \f[R]
96 .fi
97 .PP
98 instead of like this:
99 .IP
100 .nf
101 \f[C]
102 define void output(x, b) {
103     auto c
104     c=obase
105     obase=b
106     x
107     obase=c
108 }
109 \f[R]
110 .fi
111 .PP
112 This makes writing functions much easier.
113 .PP
114 (\f[B]Note\f[R]: the function \f[B]output(x,b)\f[R] exists in the
115 extended math library.
116 See the \f[B]LIBRARY\f[R] section.)
117 .PP
118 However, since using this flag means that functions cannot set
119 \f[B]ibase\f[R], \f[B]obase\f[R], \f[B]scale\f[R], or \f[B]seed\f[R]
120 globally, functions that are made to do so cannot work anymore.
121 There are two possible use cases for that, and each has a solution.
122 .PP
123 First, if a function is called on startup to turn bc(1) into a number
124 converter, it is possible to replace that capability with various shell
125 aliases.
126 Examples:
127 .IP
128 .nf
129 \f[C]
130 alias d2o=\[dq]bc -e ibase=A -e obase=8\[dq]
131 alias h2b=\[dq]bc -e ibase=G -e obase=2\[dq]
132 \f[R]
133 .fi
134 .PP
135 Second, if the purpose of a function is to set \f[B]ibase\f[R],
136 \f[B]obase\f[R], \f[B]scale\f[R], or \f[B]seed\f[R] globally for any
137 other purpose, it could be split into one to four functions (based on
138 how many globals it sets) and each of those functions could return the
139 desired value for a global.
140 .PP
141 For functions that set \f[B]seed\f[R], the value assigned to
142 \f[B]seed\f[R] is not propagated to parent functions.
143 This means that the sequence of pseudo-random numbers that they see will
144 not be the same sequence of pseudo-random numbers that any parent sees.
145 This is only the case once \f[B]seed\f[R] has been set.
146 .PP
147 If a function desires to not affect the sequence of pseudo-random
148 numbers of its parents, but wants to use the same \f[B]seed\f[R], it can
149 use the following line:
150 .IP
151 .nf
152 \f[C]
153 seed = seed
154 \f[R]
155 .fi
156 .PP
157 If the behavior of this option is desired for every run of bc(1), then
158 users could make sure to define \f[B]BC_ENV_ARGS\f[R] and include this
159 option (see the \f[B]ENVIRONMENT VARIABLES\f[R] section for more
160 details).
161 .PP
162 If \f[B]-s\f[R], \f[B]-w\f[R], or any equivalents are used, this option
163 is ignored.
164 .PP
165 This is a \f[B]non-portable extension\f[R].
166 .RE
167 .TP
168 \f[B]-h\f[R], \f[B]--help\f[R]
169 Prints a usage message and quits.
170 .TP
171 \f[B]-i\f[R], \f[B]--interactive\f[R]
172 Forces interactive mode.
173 (See the \f[B]INTERACTIVE MODE\f[R] section.)
174 .RS
175 .PP
176 This is a \f[B]non-portable extension\f[R].
177 .RE
178 .TP
179 \f[B]-L\f[R], \f[B]--no-line-length\f[R]
180 Disables line length checking and prints numbers without backslashes and
181 newlines.
182 In other words, this option sets \f[B]BC_LINE_LENGTH\f[R] to \f[B]0\f[R]
183 (see the \f[B]ENVIRONMENT VARIABLES\f[R] section).
184 .RS
185 .PP
186 This is a \f[B]non-portable extension\f[R].
187 .RE
188 .TP
189 \f[B]-l\f[R], \f[B]--mathlib\f[R]
190 Sets \f[B]scale\f[R] (see the \f[B]SYNTAX\f[R] section) to \f[B]20\f[R]
191 and loads the included math library and the extended math library before
192 running any code, including any expressions or files specified on the
193 command line.
194 .RS
195 .PP
196 To learn what is in the libraries, see the \f[B]LIBRARY\f[R] section.
197 .RE
198 .TP
199 \f[B]-P\f[R], \f[B]--no-prompt\f[R]
200 Disables the prompt in TTY mode.
201 (The prompt is only enabled in TTY mode.
202 See the \f[B]TTY MODE\f[R] section.) This is mostly for those users that
203 do not want a prompt or are not used to having them in bc(1).
204 Most of those users would want to put this option in
205 \f[B]BC_ENV_ARGS\f[R] (see the \f[B]ENVIRONMENT VARIABLES\f[R] section).
206 .RS
207 .PP
208 These options override the \f[B]BC_PROMPT\f[R] and \f[B]BC_TTY_MODE\f[R]
209 environment variables (see the \f[B]ENVIRONMENT VARIABLES\f[R] section).
210 .PP
211 This is a \f[B]non-portable extension\f[R].
212 .RE
213 .TP
214 \f[B]-R\f[R], \f[B]--no-read-prompt\f[R]
215 Disables the read prompt in TTY mode.
216 (The read prompt is only enabled in TTY mode.
217 See the \f[B]TTY MODE\f[R] section.) This is mostly for those users that
218 do not want a read prompt or are not used to having them in bc(1).
219 Most of those users would want to put this option in
220 \f[B]BC_ENV_ARGS\f[R] (see the \f[B]ENVIRONMENT VARIABLES\f[R] section).
221 This option is also useful in hash bang lines of bc(1) scripts that
222 prompt for user input.
223 .RS
224 .PP
225 This option does not disable the regular prompt because the read prompt
226 is only used when the \f[B]read()\f[R] built-in function is called.
227 .PP
228 These options \f[I]do\f[R] override the \f[B]BC_PROMPT\f[R] and
229 \f[B]BC_TTY_MODE\f[R] environment variables (see the \f[B]ENVIRONMENT
230 VARIABLES\f[R] section), but only for the read prompt.
231 .PP
232 This is a \f[B]non-portable extension\f[R].
233 .RE
234 .TP
235 \f[B]-r\f[R] \f[I]keyword\f[R], \f[B]--redefine\f[R]=\f[I]keyword\f[R]
236 Redefines \f[I]keyword\f[R] in order to allow it to be used as a
237 function, variable, or array name.
238 This is useful when this bc(1) gives parse errors when parsing scripts
239 meant for other bc(1) implementations.
240 .RS
241 .PP
242 The keywords this bc(1) allows to be redefined are:
243 .IP \[bu] 2
244 \f[B]abs\f[R]
245 .IP \[bu] 2
246 \f[B]asciify\f[R]
247 .IP \[bu] 2
248 \f[B]continue\f[R]
249 .IP \[bu] 2
250 \f[B]divmod\f[R]
251 .IP \[bu] 2
252 \f[B]else\f[R]
253 .IP \[bu] 2
254 \f[B]halt\f[R]
255 .IP \[bu] 2
256 \f[B]irand\f[R]
257 .IP \[bu] 2
258 \f[B]last\f[R]
259 .IP \[bu] 2
260 \f[B]limits\f[R]
261 .IP \[bu] 2
262 \f[B]maxibase\f[R]
263 .IP \[bu] 2
264 \f[B]maxobase\f[R]
265 .IP \[bu] 2
266 \f[B]maxrand\f[R]
267 .IP \[bu] 2
268 \f[B]maxscale\f[R]
269 .IP \[bu] 2
270 \f[B]modexp\f[R]
271 .IP \[bu] 2
272 \f[B]print\f[R]
273 .IP \[bu] 2
274 \f[B]rand\f[R]
275 .IP \[bu] 2
276 \f[B]read\f[R]
277 .IP \[bu] 2
278 \f[B]seed\f[R]
279 .IP \[bu] 2
280 \f[B]stream\f[R]
281 .PP
282 If any of those keywords are used as a function, variable, or array name
283 in a script, use this option with the keyword as the argument.
284 If multiple are used, use this option for all of them; it can be used
285 multiple times.
286 .PP
287 Keywords are \f[I]not\f[R] redefined when parsing the builtin math
288 library (see the \f[B]LIBRARY\f[R] section).
289 .PP
290 It is a fatal error to redefine keywords mandated by the POSIX standard.
291 It is a fatal error to attempt to redefine words that this bc(1) does
292 not reserve as keywords.
293 .RE
294 .TP
295 \f[B]-q\f[R], \f[B]--quiet\f[R]
296 This option is for compatibility with the GNU
297 bc(1) (https://www.gnu.org/software/bc/); it is a no-op.
298 Without this option, GNU bc(1) prints a copyright header.
299 This bc(1) only prints the copyright header if one or more of the
300 \f[B]-v\f[R], \f[B]-V\f[R], or \f[B]--version\f[R] options are given.
301 .RS
302 .PP
303 This is a \f[B]non-portable extension\f[R].
304 .RE
305 .TP
306 \f[B]-s\f[R], \f[B]--standard\f[R]
307 Process exactly the language defined by the
308 standard (https://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html)
309 and error if any extensions are used.
310 .RS
311 .PP
312 This is a \f[B]non-portable extension\f[R].
313 .RE
314 .TP
315 \f[B]-v\f[R], \f[B]-V\f[R], \f[B]--version\f[R]
316 Print the version information (copyright header) and exit.
317 .RS
318 .PP
319 This is a \f[B]non-portable extension\f[R].
320 .RE
321 .TP
322 \f[B]-w\f[R], \f[B]--warn\f[R]
323 Like \f[B]-s\f[R] and \f[B]--standard\f[R], except that warnings (and
324 not errors) are printed for non-standard extensions and execution
325 continues normally.
326 .RS
327 .PP
328 This is a \f[B]non-portable extension\f[R].
329 .RE
330 .TP
331 \f[B]-z\f[R], \f[B]--leading-zeroes\f[R]
332 Makes bc(1) print all numbers greater than \f[B]-1\f[R] and less than
333 \f[B]1\f[R], and not equal to \f[B]0\f[R], with a leading zero.
334 .RS
335 .PP
336 This can be set for individual numbers with the \f[B]plz(x)\f[R],
337 plznl(x)**, \f[B]pnlz(x)\f[R], and \f[B]pnlznl(x)\f[R] functions in the
338 extended math library (see the \f[B]LIBRARY\f[R] section).
339 .PP
340 This is a \f[B]non-portable extension\f[R].
341 .RE
342 .TP
343 \f[B]-e\f[R] \f[I]expr\f[R], \f[B]--expression\f[R]=\f[I]expr\f[R]
344 Evaluates \f[I]expr\f[R].
345 If multiple expressions are given, they are evaluated in order.
346 If files are given as well (see below), the expressions and files are
347 evaluated in the order given.
348 This means that if a file is given before an expression, the file is
349 read in and evaluated first.
350 .RS
351 .PP
352 If this option is given on the command-line (i.e., not in
353 \f[B]BC_ENV_ARGS\f[R], see the \f[B]ENVIRONMENT VARIABLES\f[R] section),
354 then after processing all expressions and files, bc(1) will exit, unless
355 \f[B]-\f[R] (\f[B]stdin\f[R]) was given as an argument at least once to
356 \f[B]-f\f[R] or \f[B]--file\f[R], whether on the command-line or in
357 \f[B]BC_ENV_ARGS\f[R].
358 However, if any other \f[B]-e\f[R], \f[B]--expression\f[R],
359 \f[B]-f\f[R], or \f[B]--file\f[R] arguments are given after
360 \f[B]-f-\f[R] or equivalent is given, bc(1) will give a fatal error and
361 exit.
362 .PP
363 This is a \f[B]non-portable extension\f[R].
364 .RE
365 .TP
366 \f[B]-f\f[R] \f[I]file\f[R], \f[B]--file\f[R]=\f[I]file\f[R]
367 Reads in \f[I]file\f[R] and evaluates it, line by line, as though it
368 were read through \f[B]stdin\f[R].
369 If expressions are also given (see above), the expressions are evaluated
370 in the order given.
371 .RS
372 .PP
373 If this option is given on the command-line (i.e., not in
374 \f[B]BC_ENV_ARGS\f[R], see the \f[B]ENVIRONMENT VARIABLES\f[R] section),
375 then after processing all expressions and files, bc(1) will exit, unless
376 \f[B]-\f[R] (\f[B]stdin\f[R]) was given as an argument at least once to
377 \f[B]-f\f[R] or \f[B]--file\f[R].
378 However, if any other \f[B]-e\f[R], \f[B]--expression\f[R],
379 \f[B]-f\f[R], or \f[B]--file\f[R] arguments are given after
380 \f[B]-f-\f[R] or equivalent is given, bc(1) will give a fatal error and
381 exit.
382 .PP
383 This is a \f[B]non-portable extension\f[R].
384 .RE
385 .PP
386 All long options are \f[B]non-portable extensions\f[R].
387 .SH STDIN
388 .PP
389 If no files or expressions are given by the \f[B]-f\f[R],
390 \f[B]--file\f[R], \f[B]-e\f[R], or \f[B]--expression\f[R] options, then
391 bc(1) read from \f[B]stdin\f[R].
392 .PP
393 However, there are a few caveats to this.
394 .PP
395 First, \f[B]stdin\f[R] is evaluated a line at a time.
396 The only exception to this is if the parse cannot complete.
397 That means that starting a string without ending it or starting a
398 function, \f[B]if\f[R] statement, or loop without ending it will also
399 cause bc(1) to not execute.
400 .PP
401 Second, after an \f[B]if\f[R] statement, bc(1) doesn\[cq]t know if an
402 \f[B]else\f[R] statement will follow, so it will not execute until it
403 knows there will not be an \f[B]else\f[R] statement.
404 .SH STDOUT
405 .PP
406 Any non-error output is written to \f[B]stdout\f[R].
407 In addition, if history (see the \f[B]HISTORY\f[R] section) and the
408 prompt (see the \f[B]TTY MODE\f[R] section) are enabled, both are output
409 to \f[B]stdout\f[R].
410 .PP
411 \f[B]Note\f[R]: Unlike other bc(1) implementations, this bc(1) will
412 issue a fatal error (see the \f[B]EXIT STATUS\f[R] section) if it cannot
413 write to \f[B]stdout\f[R], so if \f[B]stdout\f[R] is closed, as in
414 \f[B]bc >&-\f[R], it will quit with an error.
415 This is done so that bc(1) can report problems when \f[B]stdout\f[R] is
416 redirected to a file.
417 .PP
418 If there are scripts that depend on the behavior of other bc(1)
419 implementations, it is recommended that those scripts be changed to
420 redirect \f[B]stdout\f[R] to \f[B]/dev/null\f[R].
421 .SH STDERR
422 .PP
423 Any error output is written to \f[B]stderr\f[R].
424 .PP
425 \f[B]Note\f[R]: Unlike other bc(1) implementations, this bc(1) will
426 issue a fatal error (see the \f[B]EXIT STATUS\f[R] section) if it cannot
427 write to \f[B]stderr\f[R], so if \f[B]stderr\f[R] is closed, as in
428 \f[B]bc 2>&-\f[R], it will quit with an error.
429 This is done so that bc(1) can exit with an error code when
430 \f[B]stderr\f[R] is redirected to a file.
431 .PP
432 If there are scripts that depend on the behavior of other bc(1)
433 implementations, it is recommended that those scripts be changed to
434 redirect \f[B]stderr\f[R] to \f[B]/dev/null\f[R].
435 .SH SYNTAX
436 .PP
437 The syntax for bc(1) programs is mostly C-like, with some differences.
438 This bc(1) follows the POSIX
439 standard (https://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html),
440 which is a much more thorough resource for the language this bc(1)
441 accepts.
442 This section is meant to be a summary and a listing of all the
443 extensions to the standard.
444 .PP
445 In the sections below, \f[B]E\f[R] means expression, \f[B]S\f[R] means
446 statement, and \f[B]I\f[R] means identifier.
447 .PP
448 Identifiers (\f[B]I\f[R]) start with a lowercase letter and can be
449 followed by any number (up to \f[B]BC_NAME_MAX-1\f[R]) of lowercase
450 letters (\f[B]a-z\f[R]), digits (\f[B]0-9\f[R]), and underscores
451 (\f[B]_\f[R]).
452 The regex is \f[B][a-z][a-z0-9_]*\f[R].
453 Identifiers with more than one character (letter) are a
454 \f[B]non-portable extension\f[R].
455 .PP
456 \f[B]ibase\f[R] is a global variable determining how to interpret
457 constant numbers.
458 It is the \[lq]input\[rq] base, or the number base used for interpreting
459 input numbers.
460 \f[B]ibase\f[R] is initially \f[B]10\f[R].
461 If the \f[B]-s\f[R] (\f[B]--standard\f[R]) and \f[B]-w\f[R]
462 (\f[B]--warn\f[R]) flags were not given on the command line, the max
463 allowable value for \f[B]ibase\f[R] is \f[B]36\f[R].
464 Otherwise, it is \f[B]16\f[R].
465 The min allowable value for \f[B]ibase\f[R] is \f[B]2\f[R].
466 The max allowable value for \f[B]ibase\f[R] can be queried in bc(1)
467 programs with the \f[B]maxibase()\f[R] built-in function.
468 .PP
469 \f[B]obase\f[R] is a global variable determining how to output results.
470 It is the \[lq]output\[rq] base, or the number base used for outputting
471 numbers.
472 \f[B]obase\f[R] is initially \f[B]10\f[R].
473 The max allowable value for \f[B]obase\f[R] is \f[B]BC_BASE_MAX\f[R] and
474 can be queried in bc(1) programs with the \f[B]maxobase()\f[R] built-in
475 function.
476 The min allowable value for \f[B]obase\f[R] is \f[B]0\f[R].
477 If \f[B]obase\f[R] is \f[B]0\f[R], values are output in scientific
478 notation, and if \f[B]obase\f[R] is \f[B]1\f[R], values are output in
479 engineering notation.
480 Otherwise, values are output in the specified base.
481 .PP
482 Outputting in scientific and engineering notations are \f[B]non-portable
483 extensions\f[R].
484 .PP
485 The \f[I]scale\f[R] of an expression is the number of digits in the
486 result of the expression right of the decimal point, and \f[B]scale\f[R]
487 is a global variable that sets the precision of any operations, with
488 exceptions.
489 \f[B]scale\f[R] is initially \f[B]0\f[R].
490 \f[B]scale\f[R] cannot be negative.
491 The max allowable value for \f[B]scale\f[R] is \f[B]BC_SCALE_MAX\f[R]
492 and can be queried in bc(1) programs with the \f[B]maxscale()\f[R]
493 built-in function.
494 .PP
495 bc(1) has both \f[I]global\f[R] variables and \f[I]local\f[R] variables.
496 All \f[I]local\f[R] variables are local to the function; they are
497 parameters or are introduced in the \f[B]auto\f[R] list of a function
498 (see the \f[B]FUNCTIONS\f[R] section).
499 If a variable is accessed which is not a parameter or in the
500 \f[B]auto\f[R] list, it is assumed to be \f[I]global\f[R].
501 If a parent function has a \f[I]local\f[R] variable version of a
502 variable that a child function considers \f[I]global\f[R], the value of
503 that \f[I]global\f[R] variable in the child function is the value of the
504 variable in the parent function, not the value of the actual
505 \f[I]global\f[R] variable.
506 .PP
507 All of the above applies to arrays as well.
508 .PP
509 The value of a statement that is an expression (i.e., any of the named
510 expressions or operands) is printed unless the lowest precedence
511 operator is an assignment operator \f[I]and\f[R] the expression is
512 notsurrounded by parentheses.
513 .PP
514 The value that is printed is also assigned to the special variable
515 \f[B]last\f[R].
516 A single dot (\f[B].\f[R]) may also be used as a synonym for
517 \f[B]last\f[R].
518 These are \f[B]non-portable extensions\f[R].
519 .PP
520 Either semicolons or newlines may separate statements.
521 .SS Comments
522 .PP
523 There are two kinds of comments:
524 .IP "1." 3
525 Block comments are enclosed in \f[B]/*\f[R] and \f[B]*/\f[R].
526 .IP "2." 3
527 Line comments go from \f[B]#\f[R] until, and not including, the next
528 newline.
529 This is a \f[B]non-portable extension\f[R].
530 .SS Named Expressions
531 .PP
532 The following are named expressions in bc(1):
533 .IP "1." 3
534 Variables: \f[B]I\f[R]
535 .IP "2." 3
536 Array Elements: \f[B]I[E]\f[R]
537 .IP "3." 3
538 \f[B]ibase\f[R]
539 .IP "4." 3
540 \f[B]obase\f[R]
541 .IP "5." 3
542 \f[B]scale\f[R]
543 .IP "6." 3
544 \f[B]seed\f[R]
545 .IP "7." 3
546 \f[B]last\f[R] or a single dot (\f[B].\f[R])
547 .PP
548 Numbers 6 and 7 are \f[B]non-portable extensions\f[R].
549 .PP
550 The meaning of \f[B]seed\f[R] is dependent on the current pseudo-random
551 number generator but is guaranteed to not change except for new major
552 versions.
553 .PP
554 The \f[I]scale\f[R] and sign of the value may be significant.
555 .PP
556 If a previously used \f[B]seed\f[R] value is assigned to \f[B]seed\f[R]
557 and used again, the pseudo-random number generator is guaranteed to
558 produce the same sequence of pseudo-random numbers as it did when the
559 \f[B]seed\f[R] value was previously used.
560 .PP
561 The exact value assigned to \f[B]seed\f[R] is not guaranteed to be
562 returned if \f[B]seed\f[R] is queried again immediately.
563 However, if \f[B]seed\f[R] \f[I]does\f[R] return a different value, both
564 values, when assigned to \f[B]seed\f[R], are guaranteed to produce the
565 same sequence of pseudo-random numbers.
566 This means that certain values assigned to \f[B]seed\f[R] will
567 \f[I]not\f[R] produce unique sequences of pseudo-random numbers.
568 The value of \f[B]seed\f[R] will change after any use of the
569 \f[B]rand()\f[R] and \f[B]irand(E)\f[R] operands (see the
570 \f[I]Operands\f[R] subsection below), except if the parameter passed to
571 \f[B]irand(E)\f[R] is \f[B]0\f[R], \f[B]1\f[R], or negative.
572 .PP
573 There is no limit to the length (number of significant decimal digits)
574 or \f[I]scale\f[R] of the value that can be assigned to \f[B]seed\f[R].
575 .PP
576 Variables and arrays do not interfere; users can have arrays named the
577 same as variables.
578 This also applies to functions (see the \f[B]FUNCTIONS\f[R] section), so
579 a user can have a variable, array, and function that all have the same
580 name, and they will not shadow each other, whether inside of functions
581 or not.
582 .PP
583 Named expressions are required as the operand of
584 \f[B]increment\f[R]/\f[B]decrement\f[R] operators and as the left side
585 of \f[B]assignment\f[R] operators (see the \f[I]Operators\f[R]
586 subsection).
587 .SS Operands
588 .PP
589 The following are valid operands in bc(1):
590 .IP " 1." 4
591 Numbers (see the \f[I]Numbers\f[R] subsection below).
592 .IP " 2." 4
593 Array indices (\f[B]I[E]\f[R]).
594 .IP " 3." 4
595 \f[B](E)\f[R]: The value of \f[B]E\f[R] (used to change precedence).
596 .IP " 4." 4
597 \f[B]sqrt(E)\f[R]: The square root of \f[B]E\f[R].
598 \f[B]E\f[R] must be non-negative.
599 .IP " 5." 4
600 \f[B]length(E)\f[R]: The number of significant decimal digits in
601 \f[B]E\f[R].
602 Returns \f[B]1\f[R] for \f[B]0\f[R] with no decimal places.
603 If given a string, the length of the string is returned.
604 Passing a string to \f[B]length(E)\f[R] is a \f[B]non-portable
605 extension\f[R].
606 .IP " 6." 4
607 \f[B]length(I[])\f[R]: The number of elements in the array \f[B]I\f[R].
608 This is a \f[B]non-portable extension\f[R].
609 .IP " 7." 4
610 \f[B]scale(E)\f[R]: The \f[I]scale\f[R] of \f[B]E\f[R].
611 .IP " 8." 4
612 \f[B]abs(E)\f[R]: The absolute value of \f[B]E\f[R].
613 This is a \f[B]non-portable extension\f[R].
614 .IP " 9." 4
615 \f[B]modexp(E, E, E)\f[R]: Modular exponentiation, where the first
616 expression is the base, the second is the exponent, and the third is the
617 modulus.
618 All three values must be integers.
619 The second argument must be non-negative.
620 The third argument must be non-zero.
621 This is a \f[B]non-portable extension\f[R].
622 .IP "10." 4
623 \f[B]divmod(E, E, I[])\f[R]: Division and modulus in one operation.
624 This is for optimization.
625 The first expression is the dividend, and the second is the divisor,
626 which must be non-zero.
627 The return value is the quotient, and the modulus is stored in index
628 \f[B]0\f[R] of the provided array (the last argument).
629 This is a \f[B]non-portable extension\f[R].
630 .IP "11." 4
631 \f[B]asciify(E)\f[R]: If \f[B]E\f[R] is a string, returns a string that
632 is the first letter of its argument.
633 If it is a number, calculates the number mod \f[B]256\f[R] and returns
634 that number as a one-character string.
635 This is a \f[B]non-portable extension\f[R].
636 .IP "12." 4
637 \f[B]I()\f[R], \f[B]I(E)\f[R], \f[B]I(E, E)\f[R], and so on, where
638 \f[B]I\f[R] is an identifier for a non-\f[B]void\f[R] function (see the
639 \f[I]Void Functions\f[R] subsection of the \f[B]FUNCTIONS\f[R] section).
640 The \f[B]E\f[R] argument(s) may also be arrays of the form
641 \f[B]I[]\f[R], which will automatically be turned into array references
642 (see the \f[I]Array References\f[R] subsection of the
643 \f[B]FUNCTIONS\f[R] section) if the corresponding parameter in the
644 function definition is an array reference.
645 .IP "13." 4
646 \f[B]read()\f[R]: Reads a line from \f[B]stdin\f[R] and uses that as an
647 expression.
648 The result of that expression is the result of the \f[B]read()\f[R]
649 operand.
650 This is a \f[B]non-portable extension\f[R].
651 .IP "14." 4
652 \f[B]maxibase()\f[R]: The max allowable \f[B]ibase\f[R].
653 This is a \f[B]non-portable extension\f[R].
654 .IP "15." 4
655 \f[B]maxobase()\f[R]: The max allowable \f[B]obase\f[R].
656 This is a \f[B]non-portable extension\f[R].
657 .IP "16." 4
658 \f[B]maxscale()\f[R]: The max allowable \f[B]scale\f[R].
659 This is a \f[B]non-portable extension\f[R].
660 .IP "17." 4
661 \f[B]line_length()\f[R]: The line length set with
662 \f[B]BC_LINE_LENGTH\f[R] (see the \f[B]ENVIRONMENT VARIABLES\f[R]
663 section).
664 This is a \f[B]non-portable extension\f[R].
665 .IP "18." 4
666 \f[B]global_stacks()\f[R]: \f[B]0\f[R] if global stacks are not enabled
667 with the \f[B]-g\f[R] or \f[B]--global-stacks\f[R] options, non-zero
668 otherwise.
669 See the \f[B]OPTIONS\f[R] section.
670 This is a \f[B]non-portable extension\f[R].
671 .IP "19." 4
672 \f[B]leading_zero()\f[R]: \f[B]0\f[R] if leading zeroes are not enabled
673 with the \f[B]-z\f[R] or \f[B]\[en]leading-zeroes\f[R] options, non-zero
674 otherwise.
675 See the \f[B]OPTIONS\f[R] section.
676 This is a \f[B]non-portable extension\f[R].
677 .IP "20." 4
678 \f[B]rand()\f[R]: A pseudo-random integer between \f[B]0\f[R]
679 (inclusive) and \f[B]BC_RAND_MAX\f[R] (inclusive).
680 Using this operand will change the value of \f[B]seed\f[R].
681 This is a \f[B]non-portable extension\f[R].
682 .IP "21." 4
683 \f[B]irand(E)\f[R]: A pseudo-random integer between \f[B]0\f[R]
684 (inclusive) and the value of \f[B]E\f[R] (exclusive).
685 If \f[B]E\f[R] is negative or is a non-integer (\f[B]E\f[R]\[cq]s
686 \f[I]scale\f[R] is not \f[B]0\f[R]), an error is raised, and bc(1)
687 resets (see the \f[B]RESET\f[R] section) while \f[B]seed\f[R] remains
688 unchanged.
689 If \f[B]E\f[R] is larger than \f[B]BC_RAND_MAX\f[R], the higher bound is
690 honored by generating several pseudo-random integers, multiplying them
691 by appropriate powers of \f[B]BC_RAND_MAX+1\f[R], and adding them
692 together.
693 Thus, the size of integer that can be generated with this operand is
694 unbounded.
695 Using this operand will change the value of \f[B]seed\f[R], unless the
696 value of \f[B]E\f[R] is \f[B]0\f[R] or \f[B]1\f[R].
697 In that case, \f[B]0\f[R] is returned, and \f[B]seed\f[R] is
698 \f[I]not\f[R] changed.
699 This is a \f[B]non-portable extension\f[R].
700 .IP "22." 4
701 \f[B]maxrand()\f[R]: The max integer returned by \f[B]rand()\f[R].
702 This is a \f[B]non-portable extension\f[R].
703 .PP
704 The integers generated by \f[B]rand()\f[R] and \f[B]irand(E)\f[R] are
705 guaranteed to be as unbiased as possible, subject to the limitations of
706 the pseudo-random number generator.
707 .PP
708 \f[B]Note\f[R]: The values returned by the pseudo-random number
709 generator with \f[B]rand()\f[R] and \f[B]irand(E)\f[R] are guaranteed to
710 \f[I]NOT\f[R] be cryptographically secure.
711 This is a consequence of using a seeded pseudo-random number generator.
712 However, they \f[I]are\f[R] guaranteed to be reproducible with identical
713 \f[B]seed\f[R] values.
714 This means that the pseudo-random values from bc(1) should only be used
715 where a reproducible stream of pseudo-random numbers is
716 \f[I]ESSENTIAL\f[R].
717 In any other case, use a non-seeded pseudo-random number generator.
718 .SS Numbers
719 .PP
720 Numbers are strings made up of digits, uppercase letters, and at most
721 \f[B]1\f[R] period for a radix.
722 Numbers can have up to \f[B]BC_NUM_MAX\f[R] digits.
723 Uppercase letters are equal to \f[B]9\f[R] + their position in the
724 alphabet (i.e., \f[B]A\f[R] equals \f[B]10\f[R], or \f[B]9+1\f[R]).
725 If a digit or letter makes no sense with the current value of
726 \f[B]ibase\f[R], they are set to the value of the highest valid digit in
727 \f[B]ibase\f[R].
728 .PP
729 Single-character numbers (i.e., \f[B]A\f[R] alone) take the value that
730 they would have if they were valid digits, regardless of the value of
731 \f[B]ibase\f[R].
732 This means that \f[B]A\f[R] alone always equals decimal \f[B]10\f[R] and
733 \f[B]Z\f[R] alone always equals decimal \f[B]35\f[R].
734 .PP
735 In addition, bc(1) accepts numbers in scientific notation.
736 These have the form \f[B]<number>e<integer>\f[R].
737 The exponent (the portion after the \f[B]e\f[R]) must be an integer.
738 An example is \f[B]1.89237e9\f[R], which is equal to
739 \f[B]1892370000\f[R].
740 Negative exponents are also allowed, so \f[B]4.2890e-3\f[R] is equal to
741 \f[B]0.0042890\f[R].
742 .PP
743 Using scientific notation is an error or warning if the \f[B]-s\f[R] or
744 \f[B]-w\f[R], respectively, command-line options (or equivalents) are
745 given.
746 .PP
747 \f[B]WARNING\f[R]: Both the number and the exponent in scientific
748 notation are interpreted according to the current \f[B]ibase\f[R], but
749 the number is still multiplied by \f[B]10\[ha]exponent\f[R] regardless
750 of the current \f[B]ibase\f[R].
751 For example, if \f[B]ibase\f[R] is \f[B]16\f[R] and bc(1) is given the
752 number string \f[B]FFeA\f[R], the resulting decimal number will be
753 \f[B]2550000000000\f[R], and if bc(1) is given the number string
754 \f[B]10e-4\f[R], the resulting decimal number will be \f[B]0.0016\f[R].
755 .PP
756 Accepting input as scientific notation is a \f[B]non-portable
757 extension\f[R].
758 .SS Operators
759 .PP
760 The following arithmetic and logical operators can be used.
761 They are listed in order of decreasing precedence.
762 Operators in the same group have the same precedence.
763 .TP
764 \f[B]++\f[R] \f[B]--\f[R]
765 Type: Prefix and Postfix
766 .RS
767 .PP
768 Associativity: None
769 .PP
770 Description: \f[B]increment\f[R], \f[B]decrement\f[R]
771 .RE
772 .TP
773 \f[B]-\f[R] \f[B]!\f[R]
774 Type: Prefix
775 .RS
776 .PP
777 Associativity: None
778 .PP
779 Description: \f[B]negation\f[R], \f[B]boolean not\f[R]
780 .RE
781 .TP
782 \f[B]$\f[R]
783 Type: Postfix
784 .RS
785 .PP
786 Associativity: None
787 .PP
788 Description: \f[B]truncation\f[R]
789 .RE
790 .TP
791 \f[B]\[at]\f[R]
792 Type: Binary
793 .RS
794 .PP
795 Associativity: Right
796 .PP
797 Description: \f[B]set precision\f[R]
798 .RE
799 .TP
800 \f[B]\[ha]\f[R]
801 Type: Binary
802 .RS
803 .PP
804 Associativity: Right
805 .PP
806 Description: \f[B]power\f[R]
807 .RE
808 .TP
809 \f[B]*\f[R] \f[B]/\f[R] \f[B]%\f[R]
810 Type: Binary
811 .RS
812 .PP
813 Associativity: Left
814 .PP
815 Description: \f[B]multiply\f[R], \f[B]divide\f[R], \f[B]modulus\f[R]
816 .RE
817 .TP
818 \f[B]+\f[R] \f[B]-\f[R]
819 Type: Binary
820 .RS
821 .PP
822 Associativity: Left
823 .PP
824 Description: \f[B]add\f[R], \f[B]subtract\f[R]
825 .RE
826 .TP
827 \f[B]<<\f[R] \f[B]>>\f[R]
828 Type: Binary
829 .RS
830 .PP
831 Associativity: Left
832 .PP
833 Description: \f[B]shift left\f[R], \f[B]shift right\f[R]
834 .RE
835 .TP
836 \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]
837 Type: Binary
838 .RS
839 .PP
840 Associativity: Right
841 .PP
842 Description: \f[B]assignment\f[R]
843 .RE
844 .TP
845 \f[B]==\f[R] \f[B]<=\f[R] \f[B]>=\f[R] \f[B]!=\f[R] \f[B]<\f[R] \f[B]>\f[R]
846 Type: Binary
847 .RS
848 .PP
849 Associativity: Left
850 .PP
851 Description: \f[B]relational\f[R]
852 .RE
853 .TP
854 \f[B]&&\f[R]
855 Type: Binary
856 .RS
857 .PP
858 Associativity: Left
859 .PP
860 Description: \f[B]boolean and\f[R]
861 .RE
862 .TP
863 \f[B]||\f[R]
864 Type: Binary
865 .RS
866 .PP
867 Associativity: Left
868 .PP
869 Description: \f[B]boolean or\f[R]
870 .RE
871 .PP
872 The operators will be described in more detail below.
873 .TP
874 \f[B]++\f[R] \f[B]--\f[R]
875 The prefix and postfix \f[B]increment\f[R] and \f[B]decrement\f[R]
876 operators behave exactly like they would in C.
877 They require a named expression (see the \f[I]Named Expressions\f[R]
878 subsection) as an operand.
879 .RS
880 .PP
881 The prefix versions of these operators are more efficient; use them
882 where possible.
883 .RE
884 .TP
885 \f[B]-\f[R]
886 The \f[B]negation\f[R] operator returns \f[B]0\f[R] if a user attempts
887 to negate any expression with the value \f[B]0\f[R].
888 Otherwise, a copy of the expression with its sign flipped is returned.
889 .TP
890 \f[B]!\f[R]
891 The \f[B]boolean not\f[R] operator returns \f[B]1\f[R] if the expression
892 is \f[B]0\f[R], or \f[B]0\f[R] otherwise.
893 .RS
894 .PP
895 This is a \f[B]non-portable extension\f[R].
896 .RE
897 .TP
898 \f[B]$\f[R]
899 The \f[B]truncation\f[R] operator returns a copy of the given expression
900 with all of its \f[I]scale\f[R] removed.
901 .RS
902 .PP
903 This is a \f[B]non-portable extension\f[R].
904 .RE
905 .TP
906 \f[B]\[at]\f[R]
907 The \f[B]set precision\f[R] operator takes two expressions and returns a
908 copy of the first with its \f[I]scale\f[R] equal to the value of the
909 second expression.
910 That could either mean that the number is returned without change (if
911 the \f[I]scale\f[R] of the first expression matches the value of the
912 second expression), extended (if it is less), or truncated (if it is
913 more).
914 .RS
915 .PP
916 The second expression must be an integer (no \f[I]scale\f[R]) and
917 non-negative.
918 .PP
919 This is a \f[B]non-portable extension\f[R].
920 .RE
921 .TP
922 \f[B]\[ha]\f[R]
923 The \f[B]power\f[R] operator (not the \f[B]exclusive or\f[R] operator,
924 as it would be in C) takes two expressions and raises the first to the
925 power of the value of the second.
926 The \f[I]scale\f[R] of the result is equal to \f[B]scale\f[R].
927 .RS
928 .PP
929 The second expression must be an integer (no \f[I]scale\f[R]), and if it
930 is negative, the first value must be non-zero.
931 .RE
932 .TP
933 \f[B]*\f[R]
934 The \f[B]multiply\f[R] operator takes two expressions, multiplies them,
935 and returns the product.
936 If \f[B]a\f[R] is the \f[I]scale\f[R] of the first expression and
937 \f[B]b\f[R] is the \f[I]scale\f[R] of the second expression, the
938 \f[I]scale\f[R] of the result is equal to
939 \f[B]min(a+b,max(scale,a,b))\f[R] where \f[B]min()\f[R] and
940 \f[B]max()\f[R] return the obvious values.
941 .TP
942 \f[B]/\f[R]
943 The \f[B]divide\f[R] operator takes two expressions, divides them, and
944 returns the quotient.
945 The \f[I]scale\f[R] of the result shall be the value of \f[B]scale\f[R].
946 .RS
947 .PP
948 The second expression must be non-zero.
949 .RE
950 .TP
951 \f[B]%\f[R]
952 The \f[B]modulus\f[R] operator takes two expressions, \f[B]a\f[R] and
953 \f[B]b\f[R], and evaluates them by 1) Computing \f[B]a/b\f[R] to current
954 \f[B]scale\f[R] and 2) Using the result of step 1 to calculate
955 \f[B]a-(a/b)*b\f[R] to \f[I]scale\f[R]
956 \f[B]max(scale+scale(b),scale(a))\f[R].
957 .RS
958 .PP
959 The second expression must be non-zero.
960 .RE
961 .TP
962 \f[B]+\f[R]
963 The \f[B]add\f[R] operator takes two expressions, \f[B]a\f[R] and
964 \f[B]b\f[R], and returns the sum, with a \f[I]scale\f[R] equal to the
965 max of the \f[I]scale\f[R]s of \f[B]a\f[R] and \f[B]b\f[R].
966 .TP
967 \f[B]-\f[R]
968 The \f[B]subtract\f[R] operator takes two expressions, \f[B]a\f[R] and
969 \f[B]b\f[R], and returns the difference, with a \f[I]scale\f[R] equal to
970 the max of the \f[I]scale\f[R]s of \f[B]a\f[R] and \f[B]b\f[R].
971 .TP
972 \f[B]<<\f[R]
973 The \f[B]left shift\f[R] operator takes two expressions, \f[B]a\f[R] and
974 \f[B]b\f[R], and returns a copy of the value of \f[B]a\f[R] with its
975 decimal point moved \f[B]b\f[R] places to the right.
976 .RS
977 .PP
978 The second expression must be an integer (no \f[I]scale\f[R]) and
979 non-negative.
980 .PP
981 This is a \f[B]non-portable extension\f[R].
982 .RE
983 .TP
984 \f[B]>>\f[R]
985 The \f[B]right shift\f[R] operator takes two expressions, \f[B]a\f[R]
986 and \f[B]b\f[R], and returns a copy of the value of \f[B]a\f[R] with its
987 decimal point moved \f[B]b\f[R] places to the left.
988 .RS
989 .PP
990 The second expression must be an integer (no \f[I]scale\f[R]) and
991 non-negative.
992 .PP
993 This is a \f[B]non-portable extension\f[R].
994 .RE
995 .TP
996 \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]
997 The \f[B]assignment\f[R] operators take two expressions, \f[B]a\f[R] and
998 \f[B]b\f[R] where \f[B]a\f[R] is a named expression (see the \f[I]Named
999 Expressions\f[R] subsection).
1000 .RS
1001 .PP
1002 For \f[B]=\f[R], \f[B]b\f[R] is copied and the result is assigned to
1003 \f[B]a\f[R].
1004 For all others, \f[B]a\f[R] and \f[B]b\f[R] are applied as operands to
1005 the corresponding arithmetic operator and the result is assigned to
1006 \f[B]a\f[R].
1007 .PP
1008 The \f[B]assignment\f[R] operators that correspond to operators that are
1009 extensions are themselves \f[B]non-portable extensions\f[R].
1010 .RE
1011 .TP
1012 \f[B]==\f[R] \f[B]<=\f[R] \f[B]>=\f[R] \f[B]!=\f[R] \f[B]<\f[R] \f[B]>\f[R]
1013 The \f[B]relational\f[R] operators compare two expressions, \f[B]a\f[R]
1014 and \f[B]b\f[R], and if the relation holds, according to C language
1015 semantics, the result is \f[B]1\f[R].
1016 Otherwise, it is \f[B]0\f[R].
1017 .RS
1018 .PP
1019 Note that unlike in C, these operators have a lower precedence than the
1020 \f[B]assignment\f[R] operators, which means that \f[B]a=b>c\f[R] is
1021 interpreted as \f[B](a=b)>c\f[R].
1022 .PP
1023 Also, unlike the
1024 standard (https://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html)
1025 requires, these operators can appear anywhere any other expressions can
1026 be used.
1027 This allowance is a \f[B]non-portable extension\f[R].
1028 .RE
1029 .TP
1030 \f[B]&&\f[R]
1031 The \f[B]boolean and\f[R] operator takes two expressions and returns
1032 \f[B]1\f[R] if both expressions are non-zero, \f[B]0\f[R] otherwise.
1033 .RS
1034 .PP
1035 This is \f[I]not\f[R] a short-circuit operator.
1036 .PP
1037 This is a \f[B]non-portable extension\f[R].
1038 .RE
1039 .TP
1040 \f[B]||\f[R]
1041 The \f[B]boolean or\f[R] operator takes two expressions and returns
1042 \f[B]1\f[R] if one of the expressions is non-zero, \f[B]0\f[R]
1043 otherwise.
1044 .RS
1045 .PP
1046 This is \f[I]not\f[R] a short-circuit operator.
1047 .PP
1048 This is a \f[B]non-portable extension\f[R].
1049 .RE
1050 .SS Statements
1051 .PP
1052 The following items are statements:
1053 .IP " 1." 4
1054 \f[B]E\f[R]
1055 .IP " 2." 4
1056 \f[B]{\f[R] \f[B]S\f[R] \f[B];\f[R] \&... \f[B];\f[R] \f[B]S\f[R]
1057 \f[B]}\f[R]
1058 .IP " 3." 4
1059 \f[B]if\f[R] \f[B](\f[R] \f[B]E\f[R] \f[B])\f[R] \f[B]S\f[R]
1060 .IP " 4." 4
1061 \f[B]if\f[R] \f[B](\f[R] \f[B]E\f[R] \f[B])\f[R] \f[B]S\f[R]
1062 \f[B]else\f[R] \f[B]S\f[R]
1063 .IP " 5." 4
1064 \f[B]while\f[R] \f[B](\f[R] \f[B]E\f[R] \f[B])\f[R] \f[B]S\f[R]
1065 .IP " 6." 4
1066 \f[B]for\f[R] \f[B](\f[R] \f[B]E\f[R] \f[B];\f[R] \f[B]E\f[R]
1067 \f[B];\f[R] \f[B]E\f[R] \f[B])\f[R] \f[B]S\f[R]
1068 .IP " 7." 4
1069 An empty statement
1070 .IP " 8." 4
1071 \f[B]break\f[R]
1072 .IP " 9." 4
1073 \f[B]continue\f[R]
1074 .IP "10." 4
1075 \f[B]quit\f[R]
1076 .IP "11." 4
1077 \f[B]halt\f[R]
1078 .IP "12." 4
1079 \f[B]limits\f[R]
1080 .IP "13." 4
1081 A string of characters, enclosed in double quotes
1082 .IP "14." 4
1083 \f[B]print\f[R] \f[B]E\f[R] \f[B],\f[R] \&... \f[B],\f[R] \f[B]E\f[R]
1084 .IP "15." 4
1085 \f[B]stream\f[R] \f[B]E\f[R] \f[B],\f[R] \&... \f[B],\f[R] \f[B]E\f[R]
1086 .IP "16." 4
1087 \f[B]I()\f[R], \f[B]I(E)\f[R], \f[B]I(E, E)\f[R], and so on, where
1088 \f[B]I\f[R] is an identifier for a \f[B]void\f[R] function (see the
1089 \f[I]Void Functions\f[R] subsection of the \f[B]FUNCTIONS\f[R] section).
1090 The \f[B]E\f[R] argument(s) may also be arrays of the form
1091 \f[B]I[]\f[R], which will automatically be turned into array references
1092 (see the \f[I]Array References\f[R] subsection of the
1093 \f[B]FUNCTIONS\f[R] section) if the corresponding parameter in the
1094 function definition is an array reference.
1095 .PP
1096 Numbers 4, 9, 11, 12, 14, 15, and 16 are \f[B]non-portable
1097 extensions\f[R].
1098 .PP
1099 Also, as a \f[B]non-portable extension\f[R], any or all of the
1100 expressions in the header of a for loop may be omitted.
1101 If the condition (second expression) is omitted, it is assumed to be a
1102 constant \f[B]1\f[R].
1103 .PP
1104 The \f[B]break\f[R] statement causes a loop to stop iterating and resume
1105 execution immediately following a loop.
1106 This is only allowed in loops.
1107 .PP
1108 The \f[B]continue\f[R] statement causes a loop iteration to stop early
1109 and returns to the start of the loop, including testing the loop
1110 condition.
1111 This is only allowed in loops.
1112 .PP
1113 The \f[B]if\f[R] \f[B]else\f[R] statement does the same thing as in C.
1114 .PP
1115 The \f[B]quit\f[R] statement causes bc(1) to quit, even if it is on a
1116 branch that will not be executed (it is a compile-time command).
1117 .PP
1118 The \f[B]halt\f[R] statement causes bc(1) to quit, if it is executed.
1119 (Unlike \f[B]quit\f[R] if it is on a branch of an \f[B]if\f[R] statement
1120 that is not executed, bc(1) does not quit.)
1121 .PP
1122 The \f[B]limits\f[R] statement prints the limits that this bc(1) is
1123 subject to.
1124 This is like the \f[B]quit\f[R] statement in that it is a compile-time
1125 command.
1126 .PP
1127 An expression by itself is evaluated and printed, followed by a newline.
1128 .PP
1129 Both scientific notation and engineering notation are available for
1130 printing the results of expressions.
1131 Scientific notation is activated by assigning \f[B]0\f[R] to
1132 \f[B]obase\f[R], and engineering notation is activated by assigning
1133 \f[B]1\f[R] to \f[B]obase\f[R].
1134 To deactivate them, just assign a different value to \f[B]obase\f[R].
1135 .PP
1136 Scientific notation and engineering notation are disabled if bc(1) is
1137 run with either the \f[B]-s\f[R] or \f[B]-w\f[R] command-line options
1138 (or equivalents).
1139 .PP
1140 Printing numbers in scientific notation and/or engineering notation is a
1141 \f[B]non-portable extension\f[R].
1142 .SS Strings
1143 .PP
1144 If strings appear as a statement by themselves, they are printed without
1145 a trailing newline.
1146 .PP
1147 In addition to appearing as a lone statement by themselves, strings can
1148 be assigned to variables and array elements.
1149 They can also be passed to functions in variable parameters.
1150 .PP
1151 If any statement that expects a string is given a variable that had a
1152 string assigned to it, the statement acts as though it had received a
1153 string.
1154 .PP
1155 If any math operation is attempted on a string or a variable or array
1156 element that has been assigned a string, an error is raised, and bc(1)
1157 resets (see the \f[B]RESET\f[R] section).
1158 .PP
1159 Assigning strings to variables and array elements and passing them to
1160 functions are \f[B]non-portable extensions\f[R].
1161 .SS Print Statement
1162 .PP
1163 The \[lq]expressions\[rq] in a \f[B]print\f[R] statement may also be
1164 strings.
1165 If they are, there are backslash escape sequences that are interpreted
1166 specially.
1167 What those sequences are, and what they cause to be printed, are shown
1168 below:
1169 .PP
1170 \f[B]\[rs]a\f[R]: \f[B]\[rs]a\f[R]
1171 .PP
1172 \f[B]\[rs]b\f[R]: \f[B]\[rs]b\f[R]
1173 .PP
1174 \f[B]\[rs]\[rs]\f[R]: \f[B]\[rs]\f[R]
1175 .PP
1176 \f[B]\[rs]e\f[R]: \f[B]\[rs]\f[R]
1177 .PP
1178 \f[B]\[rs]f\f[R]: \f[B]\[rs]f\f[R]
1179 .PP
1180 \f[B]\[rs]n\f[R]: \f[B]\[rs]n\f[R]
1181 .PP
1182 \f[B]\[rs]q\f[R]: \f[B]\[lq]\f[R]
1183 .PP
1184 \f[B]\[rs]r\f[R]: \f[B]\[rs]r\f[R]
1185 .PP
1186 \f[B]\[rs]t\f[R]: \f[B]\[rs]t\f[R]
1187 .PP
1188 Any other character following a backslash causes the backslash and
1189 character to be printed as-is.
1190 .PP
1191 Any non-string expression in a print statement shall be assigned to
1192 \f[B]last\f[R], like any other expression that is printed.
1193 .SS Stream Statement
1194 .PP
1195 The \[lq]expressions in a \f[B]stream\f[R] statement may also be
1196 strings.
1197 .PP
1198 If a \f[B]stream\f[R] statement is given a string, it prints the string
1199 as though the string had appeared as its own statement.
1200 In other words, the \f[B]stream\f[R] statement prints strings normally,
1201 without a newline.
1202 .PP
1203 If a \f[B]stream\f[R] statement is given a number, a copy of it is
1204 truncated and its absolute value is calculated.
1205 The result is then printed as though \f[B]obase\f[R] is \f[B]256\f[R]
1206 and each digit is interpreted as an 8-bit ASCII character, making it a
1207 byte stream.
1208 .SS Order of Evaluation
1209 .PP
1210 All expressions in a statment are evaluated left to right, except as
1211 necessary to maintain order of operations.
1212 This means, for example, assuming that \f[B]i\f[R] is equal to
1213 \f[B]0\f[R], in the expression
1214 .IP
1215 .nf
1216 \f[C]
1217 a[i++] = i++
1218 \f[R]
1219 .fi
1220 .PP
1221 the first (or 0th) element of \f[B]a\f[R] is set to \f[B]1\f[R], and
1222 \f[B]i\f[R] is equal to \f[B]2\f[R] at the end of the expression.
1223 .PP
1224 This includes function arguments.
1225 Thus, assuming \f[B]i\f[R] is equal to \f[B]0\f[R], this means that in
1226 the expression
1227 .IP
1228 .nf
1229 \f[C]
1230 x(i++, i++)
1231 \f[R]
1232 .fi
1233 .PP
1234 the first argument passed to \f[B]x()\f[R] is \f[B]0\f[R], and the
1235 second argument is \f[B]1\f[R], while \f[B]i\f[R] is equal to
1236 \f[B]2\f[R] before the function starts executing.
1237 .SH FUNCTIONS
1238 .PP
1239 Function definitions are as follows:
1240 .IP
1241 .nf
1242 \f[C]
1243 define I(I,...,I){
1244     auto I,...,I
1245     S;...;S
1246     return(E)
1247 }
1248 \f[R]
1249 .fi
1250 .PP
1251 Any \f[B]I\f[R] in the parameter list or \f[B]auto\f[R] list may be
1252 replaced with \f[B]I[]\f[R] to make a parameter or \f[B]auto\f[R] var an
1253 array, and any \f[B]I\f[R] in the parameter list may be replaced with
1254 \f[B]*I[]\f[R] to make a parameter an array reference.
1255 Callers of functions that take array references should not put an
1256 asterisk in the call; they must be called with just \f[B]I[]\f[R] like
1257 normal array parameters and will be automatically converted into
1258 references.
1259 .PP
1260 As a \f[B]non-portable extension\f[R], the opening brace of a
1261 \f[B]define\f[R] statement may appear on the next line.
1262 .PP
1263 As a \f[B]non-portable extension\f[R], the return statement may also be
1264 in one of the following forms:
1265 .IP "1." 3
1266 \f[B]return\f[R]
1267 .IP "2." 3
1268 \f[B]return\f[R] \f[B](\f[R] \f[B])\f[R]
1269 .IP "3." 3
1270 \f[B]return\f[R] \f[B]E\f[R]
1271 .PP
1272 The first two, or not specifying a \f[B]return\f[R] statement, is
1273 equivalent to \f[B]return (0)\f[R], unless the function is a
1274 \f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection
1275 below).
1276 .SS Void Functions
1277 .PP
1278 Functions can also be \f[B]void\f[R] functions, defined as follows:
1279 .IP
1280 .nf
1281 \f[C]
1282 define void I(I,...,I){
1283     auto I,...,I
1284     S;...;S
1285     return
1286 }
1287 \f[R]
1288 .fi
1289 .PP
1290 They can only be used as standalone expressions, where such an
1291 expression would be printed alone, except in a print statement.
1292 .PP
1293 Void functions can only use the first two \f[B]return\f[R] statements
1294 listed above.
1295 They can also omit the return statement entirely.
1296 .PP
1297 The word \[lq]void\[rq] is not treated as a keyword; it is still
1298 possible to have variables, arrays, and functions named \f[B]void\f[R].
1299 The word \[lq]void\[rq] is only treated specially right after the
1300 \f[B]define\f[R] keyword.
1301 .PP
1302 This is a \f[B]non-portable extension\f[R].
1303 .SS Array References
1304 .PP
1305 For any array in the parameter list, if the array is declared in the
1306 form
1307 .IP
1308 .nf
1309 \f[C]
1310 *I[]
1311 \f[R]
1312 .fi
1313 .PP
1314 it is a \f[B]reference\f[R].
1315 Any changes to the array in the function are reflected, when the
1316 function returns, to the array that was passed in.
1317 .PP
1318 Other than this, all function arguments are passed by value.
1319 .PP
1320 This is a \f[B]non-portable extension\f[R].
1321 .SH LIBRARY
1322 .PP
1323 All of the functions below, including the functions in the extended math
1324 library (see the \f[I]Extended Library\f[R] subsection below), are
1325 available when the \f[B]-l\f[R] or \f[B]--mathlib\f[R] command-line
1326 flags are given, except that the extended math library is not available
1327 when the \f[B]-s\f[R] option, the \f[B]-w\f[R] option, or equivalents
1328 are given.
1329 .SS Standard Library
1330 .PP
1331 The
1332 standard (https://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html)
1333 defines the following functions for the math library:
1334 .TP
1335 \f[B]s(x)\f[R]
1336 Returns the sine of \f[B]x\f[R], which is assumed to be in radians.
1337 .RS
1338 .PP
1339 This is a transcendental function (see the \f[I]Transcendental
1340 Functions\f[R] subsection below).
1341 .RE
1342 .TP
1343 \f[B]c(x)\f[R]
1344 Returns the cosine of \f[B]x\f[R], which is assumed to be in radians.
1345 .RS
1346 .PP
1347 This is a transcendental function (see the \f[I]Transcendental
1348 Functions\f[R] subsection below).
1349 .RE
1350 .TP
1351 \f[B]a(x)\f[R]
1352 Returns the arctangent of \f[B]x\f[R], in radians.
1353 .RS
1354 .PP
1355 This is a transcendental function (see the \f[I]Transcendental
1356 Functions\f[R] subsection below).
1357 .RE
1358 .TP
1359 \f[B]l(x)\f[R]
1360 Returns the natural logarithm of \f[B]x\f[R].
1361 .RS
1362 .PP
1363 This is a transcendental function (see the \f[I]Transcendental
1364 Functions\f[R] subsection below).
1365 .RE
1366 .TP
1367 \f[B]e(x)\f[R]
1368 Returns the mathematical constant \f[B]e\f[R] raised to the power of
1369 \f[B]x\f[R].
1370 .RS
1371 .PP
1372 This is a transcendental function (see the \f[I]Transcendental
1373 Functions\f[R] subsection below).
1374 .RE
1375 .TP
1376 \f[B]j(x, n)\f[R]
1377 Returns the bessel integer order \f[B]n\f[R] (truncated) of \f[B]x\f[R].
1378 .RS
1379 .PP
1380 This is a transcendental function (see the \f[I]Transcendental
1381 Functions\f[R] subsection below).
1382 .RE
1383 .SS Extended Library
1384 .PP
1385 The extended library is \f[I]not\f[R] loaded when the
1386 \f[B]-s\f[R]/\f[B]--standard\f[R] or \f[B]-w\f[R]/\f[B]--warn\f[R]
1387 options are given since they are not part of the library defined by the
1388 standard (https://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html).
1389 .PP
1390 The extended library is a \f[B]non-portable extension\f[R].
1391 .TP
1392 \f[B]p(x, y)\f[R]
1393 Calculates \f[B]x\f[R] to the power of \f[B]y\f[R], even if \f[B]y\f[R]
1394 is not an integer, and returns the result to the current
1395 \f[B]scale\f[R].
1396 .RS
1397 .PP
1398 It is an error if \f[B]y\f[R] is negative and \f[B]x\f[R] is
1399 \f[B]0\f[R].
1400 .PP
1401 This is a transcendental function (see the \f[I]Transcendental
1402 Functions\f[R] subsection below).
1403 .RE
1404 .TP
1405 \f[B]r(x, p)\f[R]
1406 Returns \f[B]x\f[R] rounded to \f[B]p\f[R] decimal places according to
1407 the rounding mode round half away from
1408 \f[B]0\f[R] (https://en.wikipedia.org/wiki/Rounding#Round_half_away_from_zero).
1409 .TP
1410 \f[B]ceil(x, p)\f[R]
1411 Returns \f[B]x\f[R] rounded to \f[B]p\f[R] decimal places according to
1412 the rounding mode round away from
1413 \f[B]0\f[R] (https://en.wikipedia.org/wiki/Rounding#Rounding_away_from_zero).
1414 .TP
1415 \f[B]f(x)\f[R]
1416 Returns the factorial of the truncated absolute value of \f[B]x\f[R].
1417 .TP
1418 \f[B]perm(n, k)\f[R]
1419 Returns the permutation of the truncated absolute value of \f[B]n\f[R]
1420 of the truncated absolute value of \f[B]k\f[R], if \f[B]k <= n\f[R].
1421 If not, it returns \f[B]0\f[R].
1422 .TP
1423 \f[B]comb(n, k)\f[R]
1424 Returns the combination of the truncated absolute value of \f[B]n\f[R]
1425 of the truncated absolute value of \f[B]k\f[R], if \f[B]k <= n\f[R].
1426 If not, it returns \f[B]0\f[R].
1427 .TP
1428 \f[B]l2(x)\f[R]
1429 Returns the logarithm base \f[B]2\f[R] of \f[B]x\f[R].
1430 .RS
1431 .PP
1432 This is a transcendental function (see the \f[I]Transcendental
1433 Functions\f[R] subsection below).
1434 .RE
1435 .TP
1436 \f[B]l10(x)\f[R]
1437 Returns the logarithm base \f[B]10\f[R] of \f[B]x\f[R].
1438 .RS
1439 .PP
1440 This is a transcendental function (see the \f[I]Transcendental
1441 Functions\f[R] subsection below).
1442 .RE
1443 .TP
1444 \f[B]log(x, b)\f[R]
1445 Returns the logarithm base \f[B]b\f[R] of \f[B]x\f[R].
1446 .RS
1447 .PP
1448 This is a transcendental function (see the \f[I]Transcendental
1449 Functions\f[R] subsection below).
1450 .RE
1451 .TP
1452 \f[B]cbrt(x)\f[R]
1453 Returns the cube root of \f[B]x\f[R].
1454 .TP
1455 \f[B]root(x, n)\f[R]
1456 Calculates the truncated value of \f[B]n\f[R], \f[B]r\f[R], and returns
1457 the \f[B]r\f[R]th root of \f[B]x\f[R] to the current \f[B]scale\f[R].
1458 .RS
1459 .PP
1460 If \f[B]r\f[R] is \f[B]0\f[R] or negative, this raises an error and
1461 causes bc(1) to reset (see the \f[B]RESET\f[R] section).
1462 It also raises an error and causes bc(1) to reset if \f[B]r\f[R] is even
1463 and \f[B]x\f[R] is negative.
1464 .RE
1465 .TP
1466 \f[B]gcd(a, b)\f[R]
1467 Returns the greatest common divisor (factor) of the truncated absolute
1468 value of \f[B]a\f[R] and the truncated absolute value of \f[B]b\f[R].
1469 .TP
1470 \f[B]lcm(a, b)\f[R]
1471 Returns the least common multiple of the truncated absolute value of
1472 \f[B]a\f[R] and the truncated absolute value of \f[B]b\f[R].
1473 .TP
1474 \f[B]pi(p)\f[R]
1475 Returns \f[B]pi\f[R] to \f[B]p\f[R] decimal places.
1476 .RS
1477 .PP
1478 This is a transcendental function (see the \f[I]Transcendental
1479 Functions\f[R] subsection below).
1480 .RE
1481 .TP
1482 \f[B]t(x)\f[R]
1483 Returns the tangent of \f[B]x\f[R], which is assumed to be in radians.
1484 .RS
1485 .PP
1486 This is a transcendental function (see the \f[I]Transcendental
1487 Functions\f[R] subsection below).
1488 .RE
1489 .TP
1490 \f[B]a2(y, x)\f[R]
1491 Returns the arctangent of \f[B]y/x\f[R], in radians.
1492 If both \f[B]y\f[R] and \f[B]x\f[R] are equal to \f[B]0\f[R], it raises
1493 an error and causes bc(1) to reset (see the \f[B]RESET\f[R] section).
1494 Otherwise, if \f[B]x\f[R] is greater than \f[B]0\f[R], it returns
1495 \f[B]a(y/x)\f[R].
1496 If \f[B]x\f[R] is less than \f[B]0\f[R], and \f[B]y\f[R] is greater than
1497 or equal to \f[B]0\f[R], it returns \f[B]a(y/x)+pi\f[R].
1498 If \f[B]x\f[R] is less than \f[B]0\f[R], and \f[B]y\f[R] is less than
1499 \f[B]0\f[R], it returns \f[B]a(y/x)-pi\f[R].
1500 If \f[B]x\f[R] is equal to \f[B]0\f[R], and \f[B]y\f[R] is greater than
1501 \f[B]0\f[R], it returns \f[B]pi/2\f[R].
1502 If \f[B]x\f[R] is equal to \f[B]0\f[R], and \f[B]y\f[R] is less than
1503 \f[B]0\f[R], it returns \f[B]-pi/2\f[R].
1504 .RS
1505 .PP
1506 This function is the same as the \f[B]atan2()\f[R] function in many
1507 programming languages.
1508 .PP
1509 This is a transcendental function (see the \f[I]Transcendental
1510 Functions\f[R] subsection below).
1511 .RE
1512 .TP
1513 \f[B]sin(x)\f[R]
1514 Returns the sine of \f[B]x\f[R], which is assumed to be in radians.
1515 .RS
1516 .PP
1517 This is an alias of \f[B]s(x)\f[R].
1518 .PP
1519 This is a transcendental function (see the \f[I]Transcendental
1520 Functions\f[R] subsection below).
1521 .RE
1522 .TP
1523 \f[B]cos(x)\f[R]
1524 Returns the cosine of \f[B]x\f[R], which is assumed to be in radians.
1525 .RS
1526 .PP
1527 This is an alias of \f[B]c(x)\f[R].
1528 .PP
1529 This is a transcendental function (see the \f[I]Transcendental
1530 Functions\f[R] subsection below).
1531 .RE
1532 .TP
1533 \f[B]tan(x)\f[R]
1534 Returns the tangent of \f[B]x\f[R], which is assumed to be in radians.
1535 .RS
1536 .PP
1537 If \f[B]x\f[R] is equal to \f[B]1\f[R] or \f[B]-1\f[R], this raises an
1538 error and causes bc(1) to reset (see the \f[B]RESET\f[R] section).
1539 .PP
1540 This is an alias of \f[B]t(x)\f[R].
1541 .PP
1542 This is a transcendental function (see the \f[I]Transcendental
1543 Functions\f[R] subsection below).
1544 .RE
1545 .TP
1546 \f[B]atan(x)\f[R]
1547 Returns the arctangent of \f[B]x\f[R], in radians.
1548 .RS
1549 .PP
1550 This is an alias of \f[B]a(x)\f[R].
1551 .PP
1552 This is a transcendental function (see the \f[I]Transcendental
1553 Functions\f[R] subsection below).
1554 .RE
1555 .TP
1556 \f[B]atan2(y, x)\f[R]
1557 Returns the arctangent of \f[B]y/x\f[R], in radians.
1558 If both \f[B]y\f[R] and \f[B]x\f[R] are equal to \f[B]0\f[R], it raises
1559 an error and causes bc(1) to reset (see the \f[B]RESET\f[R] section).
1560 Otherwise, if \f[B]x\f[R] is greater than \f[B]0\f[R], it returns
1561 \f[B]a(y/x)\f[R].
1562 If \f[B]x\f[R] is less than \f[B]0\f[R], and \f[B]y\f[R] is greater than
1563 or equal to \f[B]0\f[R], it returns \f[B]a(y/x)+pi\f[R].
1564 If \f[B]x\f[R] is less than \f[B]0\f[R], and \f[B]y\f[R] is less than
1565 \f[B]0\f[R], it returns \f[B]a(y/x)-pi\f[R].
1566 If \f[B]x\f[R] is equal to \f[B]0\f[R], and \f[B]y\f[R] is greater than
1567 \f[B]0\f[R], it returns \f[B]pi/2\f[R].
1568 If \f[B]x\f[R] is equal to \f[B]0\f[R], and \f[B]y\f[R] is less than
1569 \f[B]0\f[R], it returns \f[B]-pi/2\f[R].
1570 .RS
1571 .PP
1572 This function is the same as the \f[B]atan2()\f[R] function in many
1573 programming languages.
1574 .PP
1575 This is an alias of \f[B]a2(y, x)\f[R].
1576 .PP
1577 This is a transcendental function (see the \f[I]Transcendental
1578 Functions\f[R] subsection below).
1579 .RE
1580 .TP
1581 \f[B]r2d(x)\f[R]
1582 Converts \f[B]x\f[R] from radians to degrees and returns the result.
1583 .RS
1584 .PP
1585 This is a transcendental function (see the \f[I]Transcendental
1586 Functions\f[R] subsection below).
1587 .RE
1588 .TP
1589 \f[B]d2r(x)\f[R]
1590 Converts \f[B]x\f[R] from degrees to radians and returns the result.
1591 .RS
1592 .PP
1593 This is a transcendental function (see the \f[I]Transcendental
1594 Functions\f[R] subsection below).
1595 .RE
1596 .TP
1597 \f[B]frand(p)\f[R]
1598 Generates a pseudo-random number between \f[B]0\f[R] (inclusive) and
1599 \f[B]1\f[R] (exclusive) with the number of decimal digits after the
1600 decimal point equal to the truncated absolute value of \f[B]p\f[R].
1601 If \f[B]p\f[R] is not \f[B]0\f[R], then calling this function will
1602 change the value of \f[B]seed\f[R].
1603 If \f[B]p\f[R] is \f[B]0\f[R], then \f[B]0\f[R] is returned, and
1604 \f[B]seed\f[R] is \f[I]not\f[R] changed.
1605 .TP
1606 \f[B]ifrand(i, p)\f[R]
1607 Generates a pseudo-random number that is between \f[B]0\f[R] (inclusive)
1608 and the truncated absolute value of \f[B]i\f[R] (exclusive) with the
1609 number of decimal digits after the decimal point equal to the truncated
1610 absolute value of \f[B]p\f[R].
1611 If the absolute value of \f[B]i\f[R] is greater than or equal to
1612 \f[B]2\f[R], and \f[B]p\f[R] is not \f[B]0\f[R], then calling this
1613 function will change the value of \f[B]seed\f[R]; otherwise, \f[B]0\f[R]
1614 is returned and \f[B]seed\f[R] is not changed.
1615 .TP
1616 \f[B]srand(x)\f[R]
1617 Returns \f[B]x\f[R] with its sign flipped with probability
1618 \f[B]0.5\f[R].
1619 In other words, it randomizes the sign of \f[B]x\f[R].
1620 .TP
1621 \f[B]brand()\f[R]
1622 Returns a random boolean value (either \f[B]0\f[R] or \f[B]1\f[R]).
1623 .TP
1624 \f[B]band(a, b)\f[R]
1625 Takes the truncated absolute value of both \f[B]a\f[R] and \f[B]b\f[R]
1626 and calculates and returns the result of the bitwise \f[B]and\f[R]
1627 operation between them.
1628 .RS
1629 .PP
1630 If you want to use signed two\[cq]s complement arguments, use
1631 \f[B]s2u(x)\f[R] to convert.
1632 .RE
1633 .TP
1634 \f[B]bor(a, b)\f[R]
1635 Takes the truncated absolute value of both \f[B]a\f[R] and \f[B]b\f[R]
1636 and calculates and returns the result of the bitwise \f[B]or\f[R]
1637 operation between them.
1638 .RS
1639 .PP
1640 If you want to use signed two\[cq]s complement arguments, use
1641 \f[B]s2u(x)\f[R] to convert.
1642 .RE
1643 .TP
1644 \f[B]bxor(a, b)\f[R]
1645 Takes the truncated absolute value of both \f[B]a\f[R] and \f[B]b\f[R]
1646 and calculates and returns the result of the bitwise \f[B]xor\f[R]
1647 operation between them.
1648 .RS
1649 .PP
1650 If you want to use signed two\[cq]s complement arguments, use
1651 \f[B]s2u(x)\f[R] to convert.
1652 .RE
1653 .TP
1654 \f[B]bshl(a, b)\f[R]
1655 Takes the truncated absolute value of both \f[B]a\f[R] and \f[B]b\f[R]
1656 and calculates and returns the result of \f[B]a\f[R] bit-shifted left by
1657 \f[B]b\f[R] places.
1658 .RS
1659 .PP
1660 If you want to use signed two\[cq]s complement arguments, use
1661 \f[B]s2u(x)\f[R] to convert.
1662 .RE
1663 .TP
1664 \f[B]bshr(a, b)\f[R]
1665 Takes the truncated absolute value of both \f[B]a\f[R] and \f[B]b\f[R]
1666 and calculates and returns the truncated result of \f[B]a\f[R]
1667 bit-shifted right by \f[B]b\f[R] places.
1668 .RS
1669 .PP
1670 If you want to use signed two\[cq]s complement arguments, use
1671 \f[B]s2u(x)\f[R] to convert.
1672 .RE
1673 .TP
1674 \f[B]bnotn(x, n)\f[R]
1675 Takes the truncated absolute value of \f[B]x\f[R] and does a bitwise not
1676 as though it has the same number of bytes as the truncated absolute
1677 value of \f[B]n\f[R].
1678 .RS
1679 .PP
1680 If you want to a use signed two\[cq]s complement argument, use
1681 \f[B]s2u(x)\f[R] to convert.
1682 .RE
1683 .TP
1684 \f[B]bnot8(x)\f[R]
1685 Does a bitwise not of the truncated absolute value of \f[B]x\f[R] as
1686 though it has \f[B]8\f[R] binary digits (1 unsigned byte).
1687 .RS
1688 .PP
1689 If you want to a use signed two\[cq]s complement argument, use
1690 \f[B]s2u(x)\f[R] to convert.
1691 .RE
1692 .TP
1693 \f[B]bnot16(x)\f[R]
1694 Does a bitwise not of the truncated absolute value of \f[B]x\f[R] as
1695 though it has \f[B]16\f[R] binary digits (2 unsigned bytes).
1696 .RS
1697 .PP
1698 If you want to a use signed two\[cq]s complement argument, use
1699 \f[B]s2u(x)\f[R] to convert.
1700 .RE
1701 .TP
1702 \f[B]bnot32(x)\f[R]
1703 Does a bitwise not of the truncated absolute value of \f[B]x\f[R] as
1704 though it has \f[B]32\f[R] binary digits (4 unsigned bytes).
1705 .RS
1706 .PP
1707 If you want to a use signed two\[cq]s complement argument, use
1708 \f[B]s2u(x)\f[R] to convert.
1709 .RE
1710 .TP
1711 \f[B]bnot64(x)\f[R]
1712 Does a bitwise not of the truncated absolute value of \f[B]x\f[R] as
1713 though it has \f[B]64\f[R] binary digits (8 unsigned bytes).
1714 .RS
1715 .PP
1716 If you want to a use signed two\[cq]s complement argument, use
1717 \f[B]s2u(x)\f[R] to convert.
1718 .RE
1719 .TP
1720 \f[B]bnot(x)\f[R]
1721 Does a bitwise not of the truncated absolute value of \f[B]x\f[R] as
1722 though it has the minimum number of power of two unsigned bytes.
1723 .RS
1724 .PP
1725 If you want to a use signed two\[cq]s complement argument, use
1726 \f[B]s2u(x)\f[R] to convert.
1727 .RE
1728 .TP
1729 \f[B]brevn(x, n)\f[R]
1730 Runs a bit reversal on the truncated absolute value of \f[B]x\f[R] as
1731 though it has the same number of 8-bit bytes as the truncated absolute
1732 value of \f[B]n\f[R].
1733 .RS
1734 .PP
1735 If you want to a use signed two\[cq]s complement argument, use
1736 \f[B]s2u(x)\f[R] to convert.
1737 .RE
1738 .TP
1739 \f[B]brev8(x)\f[R]
1740 Runs a bit reversal on the truncated absolute value of \f[B]x\f[R] as
1741 though it has 8 binary digits (1 unsigned byte).
1742 .RS
1743 .PP
1744 If you want to a use signed two\[cq]s complement argument, use
1745 \f[B]s2u(x)\f[R] to convert.
1746 .RE
1747 .TP
1748 \f[B]brev16(x)\f[R]
1749 Runs a bit reversal on the truncated absolute value of \f[B]x\f[R] as
1750 though it has 16 binary digits (2 unsigned bytes).
1751 .RS
1752 .PP
1753 If you want to a use signed two\[cq]s complement argument, use
1754 \f[B]s2u(x)\f[R] to convert.
1755 .RE
1756 .TP
1757 \f[B]brev32(x)\f[R]
1758 Runs a bit reversal on the truncated absolute value of \f[B]x\f[R] as
1759 though it has 32 binary digits (4 unsigned bytes).
1760 .RS
1761 .PP
1762 If you want to a use signed two\[cq]s complement argument, use
1763 \f[B]s2u(x)\f[R] to convert.
1764 .RE
1765 .TP
1766 \f[B]brev64(x)\f[R]
1767 Runs a bit reversal on the truncated absolute value of \f[B]x\f[R] as
1768 though it has 64 binary digits (8 unsigned bytes).
1769 .RS
1770 .PP
1771 If you want to a use signed two\[cq]s complement argument, use
1772 \f[B]s2u(x)\f[R] to convert.
1773 .RE
1774 .TP
1775 \f[B]brev(x)\f[R]
1776 Runs a bit reversal on the truncated absolute value of \f[B]x\f[R] as
1777 though it has the minimum number of power of two unsigned bytes.
1778 .RS
1779 .PP
1780 If you want to a use signed two\[cq]s complement argument, use
1781 \f[B]s2u(x)\f[R] to convert.
1782 .RE
1783 .TP
1784 \f[B]broln(x, p, n)\f[R]
1785 Does a left bitwise rotatation of the truncated absolute value of
1786 \f[B]x\f[R], as though it has the same number of unsigned 8-bit bytes as
1787 the truncated absolute value of \f[B]n\f[R], by the number of places
1788 equal to the truncated absolute value of \f[B]p\f[R] modded by the
1789 \f[B]2\f[R] to the power of the number of binary digits in \f[B]n\f[R]
1790 8-bit bytes.
1791 .RS
1792 .PP
1793 If you want to a use signed two\[cq]s complement argument, use
1794 \f[B]s2u(x)\f[R] to convert.
1795 .RE
1796 .TP
1797 \f[B]brol8(x, p)\f[R]
1798 Does a left bitwise rotatation of the truncated absolute value of
1799 \f[B]x\f[R], as though it has \f[B]8\f[R] binary digits (\f[B]1\f[R]
1800 unsigned byte), by the number of places equal to the truncated absolute
1801 value of \f[B]p\f[R] modded by \f[B]2\f[R] to the power of \f[B]8\f[R].
1802 .RS
1803 .PP
1804 If you want to a use signed two\[cq]s complement argument, use
1805 \f[B]s2u(x)\f[R] to convert.
1806 .RE
1807 .TP
1808 \f[B]brol16(x, p)\f[R]
1809 Does a left bitwise rotatation of the truncated absolute value of
1810 \f[B]x\f[R], as though it has \f[B]16\f[R] binary digits (\f[B]2\f[R]
1811 unsigned bytes), by the number of places equal to the truncated absolute
1812 value of \f[B]p\f[R] modded by \f[B]2\f[R] to the power of \f[B]16\f[R].
1813 .RS
1814 .PP
1815 If you want to a use signed two\[cq]s complement argument, use
1816 \f[B]s2u(x)\f[R] to convert.
1817 .RE
1818 .TP
1819 \f[B]brol32(x, p)\f[R]
1820 Does a left bitwise rotatation of the truncated absolute value of
1821 \f[B]x\f[R], as though it has \f[B]32\f[R] binary digits (\f[B]2\f[R]
1822 unsigned bytes), by the number of places equal to the truncated absolute
1823 value of \f[B]p\f[R] modded by \f[B]2\f[R] to the power of \f[B]32\f[R].
1824 .RS
1825 .PP
1826 If you want to a use signed two\[cq]s complement argument, use
1827 \f[B]s2u(x)\f[R] to convert.
1828 .RE
1829 .TP
1830 \f[B]brol64(x, p)\f[R]
1831 Does a left bitwise rotatation of the truncated absolute value of
1832 \f[B]x\f[R], as though it has \f[B]64\f[R] binary digits (\f[B]2\f[R]
1833 unsigned bytes), by the number of places equal to the truncated absolute
1834 value of \f[B]p\f[R] modded by \f[B]2\f[R] to the power of \f[B]64\f[R].
1835 .RS
1836 .PP
1837 If you want to a use signed two\[cq]s complement argument, use
1838 \f[B]s2u(x)\f[R] to convert.
1839 .RE
1840 .TP
1841 \f[B]brol(x, p)\f[R]
1842 Does a left bitwise rotatation of the truncated absolute value of
1843 \f[B]x\f[R], as though it has the minimum number of power of two
1844 unsigned 8-bit bytes, by the number of places equal to the truncated
1845 absolute value of \f[B]p\f[R] modded by 2 to the power of the number of
1846 binary digits in the minimum number of 8-bit bytes.
1847 .RS
1848 .PP
1849 If you want to a use signed two\[cq]s complement argument, use
1850 \f[B]s2u(x)\f[R] to convert.
1851 .RE
1852 .TP
1853 \f[B]brorn(x, p, n)\f[R]
1854 Does a right bitwise rotatation of the truncated absolute value of
1855 \f[B]x\f[R], as though it has the same number of unsigned 8-bit bytes as
1856 the truncated absolute value of \f[B]n\f[R], by the number of places
1857 equal to the truncated absolute value of \f[B]p\f[R] modded by the
1858 \f[B]2\f[R] to the power of the number of binary digits in \f[B]n\f[R]
1859 8-bit bytes.
1860 .RS
1861 .PP
1862 If you want to a use signed two\[cq]s complement argument, use
1863 \f[B]s2u(x)\f[R] to convert.
1864 .RE
1865 .TP
1866 \f[B]bror8(x, p)\f[R]
1867 Does a right bitwise rotatation of the truncated absolute value of
1868 \f[B]x\f[R], as though it has \f[B]8\f[R] binary digits (\f[B]1\f[R]
1869 unsigned byte), by the number of places equal to the truncated absolute
1870 value of \f[B]p\f[R] modded by \f[B]2\f[R] to the power of \f[B]8\f[R].
1871 .RS
1872 .PP
1873 If you want to a use signed two\[cq]s complement argument, use
1874 \f[B]s2u(x)\f[R] to convert.
1875 .RE
1876 .TP
1877 \f[B]bror16(x, p)\f[R]
1878 Does a right bitwise rotatation of the truncated absolute value of
1879 \f[B]x\f[R], as though it has \f[B]16\f[R] binary digits (\f[B]2\f[R]
1880 unsigned bytes), by the number of places equal to the truncated absolute
1881 value of \f[B]p\f[R] modded by \f[B]2\f[R] to the power of \f[B]16\f[R].
1882 .RS
1883 .PP
1884 If you want to a use signed two\[cq]s complement argument, use
1885 \f[B]s2u(x)\f[R] to convert.
1886 .RE
1887 .TP
1888 \f[B]bror32(x, p)\f[R]
1889 Does a right bitwise rotatation of the truncated absolute value of
1890 \f[B]x\f[R], as though it has \f[B]32\f[R] binary digits (\f[B]2\f[R]
1891 unsigned bytes), by the number of places equal to the truncated absolute
1892 value of \f[B]p\f[R] modded by \f[B]2\f[R] to the power of \f[B]32\f[R].
1893 .RS
1894 .PP
1895 If you want to a use signed two\[cq]s complement argument, use
1896 \f[B]s2u(x)\f[R] to convert.
1897 .RE
1898 .TP
1899 \f[B]bror64(x, p)\f[R]
1900 Does a right bitwise rotatation of the truncated absolute value of
1901 \f[B]x\f[R], as though it has \f[B]64\f[R] binary digits (\f[B]2\f[R]
1902 unsigned bytes), by the number of places equal to the truncated absolute
1903 value of \f[B]p\f[R] modded by \f[B]2\f[R] to the power of \f[B]64\f[R].
1904 .RS
1905 .PP
1906 If you want to a use signed two\[cq]s complement argument, use
1907 \f[B]s2u(x)\f[R] to convert.
1908 .RE
1909 .TP
1910 \f[B]bror(x, p)\f[R]
1911 Does a right bitwise rotatation of the truncated absolute value of
1912 \f[B]x\f[R], as though it has the minimum number of power of two
1913 unsigned 8-bit bytes, by the number of places equal to the truncated
1914 absolute value of \f[B]p\f[R] modded by 2 to the power of the number of
1915 binary digits in the minimum number of 8-bit bytes.
1916 .RS
1917 .PP
1918 If you want to a use signed two\[cq]s complement argument, use
1919 \f[B]s2u(x)\f[R] to convert.
1920 .RE
1921 .TP
1922 \f[B]bmodn(x, n)\f[R]
1923 Returns the modulus of the truncated absolute value of \f[B]x\f[R] by
1924 \f[B]2\f[R] to the power of the multiplication of the truncated absolute
1925 value of \f[B]n\f[R] and \f[B]8\f[R].
1926 .RS
1927 .PP
1928 If you want to a use signed two\[cq]s complement argument, use
1929 \f[B]s2u(x)\f[R] to convert.
1930 .RE
1931 .TP
1932 \f[B]bmod8(x, n)\f[R]
1933 Returns the modulus of the truncated absolute value of \f[B]x\f[R] by
1934 \f[B]2\f[R] to the power of \f[B]8\f[R].
1935 .RS
1936 .PP
1937 If you want to a use signed two\[cq]s complement argument, use
1938 \f[B]s2u(x)\f[R] to convert.
1939 .RE
1940 .TP
1941 \f[B]bmod16(x, n)\f[R]
1942 Returns the modulus of the truncated absolute value of \f[B]x\f[R] by
1943 \f[B]2\f[R] to the power of \f[B]16\f[R].
1944 .RS
1945 .PP
1946 If you want to a use signed two\[cq]s complement argument, use
1947 \f[B]s2u(x)\f[R] to convert.
1948 .RE
1949 .TP
1950 \f[B]bmod32(x, n)\f[R]
1951 Returns the modulus of the truncated absolute value of \f[B]x\f[R] by
1952 \f[B]2\f[R] to the power of \f[B]32\f[R].
1953 .RS
1954 .PP
1955 If you want to a use signed two\[cq]s complement argument, use
1956 \f[B]s2u(x)\f[R] to convert.
1957 .RE
1958 .TP
1959 \f[B]bmod64(x, n)\f[R]
1960 Returns the modulus of the truncated absolute value of \f[B]x\f[R] by
1961 \f[B]2\f[R] to the power of \f[B]64\f[R].
1962 .RS
1963 .PP
1964 If you want to a use signed two\[cq]s complement argument, use
1965 \f[B]s2u(x)\f[R] to convert.
1966 .RE
1967 .TP
1968 \f[B]bunrev(t)\f[R]
1969 Assumes \f[B]t\f[R] is a bitwise-reversed number with an extra set bit
1970 one place more significant than the real most significant bit (which was
1971 the least significant bit in the original number).
1972 This number is reversed and returned without the extra set bit.
1973 .RS
1974 .PP
1975 This function is used to implement other bitwise functions; it is not
1976 meant to be used by users, but it can be.
1977 .RE
1978 .TP
1979 \f[B]plz(x)\f[R]
1980 If \f[B]x\f[R] is not equal to \f[B]0\f[R] and greater that \f[B]-1\f[R]
1981 and less than \f[B]1\f[R], it is printed with a leading zero, regardless
1982 of the use of the \f[B]-z\f[R] option (see the \f[B]OPTIONS\f[R]
1983 section) and without a trailing newline.
1984 .RS
1985 .PP
1986 Otherwise, \f[B]x\f[R] is printed normally, without a trailing newline.
1987 .RE
1988 .TP
1989 \f[B]plznl(x)\f[R]
1990 If \f[B]x\f[R] is not equal to \f[B]0\f[R] and greater that \f[B]-1\f[R]
1991 and less than \f[B]1\f[R], it is printed with a leading zero, regardless
1992 of the use of the \f[B]-z\f[R] option (see the \f[B]OPTIONS\f[R]
1993 section) and with a trailing newline.
1994 .RS
1995 .PP
1996 Otherwise, \f[B]x\f[R] is printed normally, with a trailing newline.
1997 .RE
1998 .TP
1999 \f[B]pnlz(x)\f[R]
2000 If \f[B]x\f[R] is not equal to \f[B]0\f[R] and greater that \f[B]-1\f[R]
2001 and less than \f[B]1\f[R], it is printed without a leading zero,
2002 regardless of the use of the \f[B]-z\f[R] option (see the
2003 \f[B]OPTIONS\f[R] section) and without a trailing newline.
2004 .RS
2005 .PP
2006 Otherwise, \f[B]x\f[R] is printed normally, without a trailing newline.
2007 .RE
2008 .TP
2009 \f[B]pnlznl(x)\f[R]
2010 If \f[B]x\f[R] is not equal to \f[B]0\f[R] and greater that \f[B]-1\f[R]
2011 and less than \f[B]1\f[R], it is printed without a leading zero,
2012 regardless of the use of the \f[B]-z\f[R] option (see the
2013 \f[B]OPTIONS\f[R] section) and with a trailing newline.
2014 .RS
2015 .PP
2016 Otherwise, \f[B]x\f[R] is printed normally, with a trailing newline.
2017 .RE
2018 .TP
2019 \f[B]ubytes(x)\f[R]
2020 Returns the numbers of unsigned integer bytes required to hold the
2021 truncated absolute value of \f[B]x\f[R].
2022 .TP
2023 \f[B]sbytes(x)\f[R]
2024 Returns the numbers of signed, two\[cq]s-complement integer bytes
2025 required to hold the truncated value of \f[B]x\f[R].
2026 .TP
2027 \f[B]s2u(x)\f[R]
2028 Returns \f[B]x\f[R] if it is non-negative.
2029 If it \f[I]is\f[R] negative, then it calculates what \f[B]x\f[R] would
2030 be as a 2\[cq]s-complement signed integer and returns the non-negative
2031 integer that would have the same representation in binary.
2032 .TP
2033 \f[B]s2un(x,n)\f[R]
2034 Returns \f[B]x\f[R] if it is non-negative.
2035 If it \f[I]is\f[R] negative, then it calculates what \f[B]x\f[R] would
2036 be as a 2\[cq]s-complement signed integer with \f[B]n\f[R] bytes and
2037 returns the non-negative integer that would have the same representation
2038 in binary.
2039 If \f[B]x\f[R] cannot fit into \f[B]n\f[R] 2\[cq]s-complement signed
2040 bytes, it is truncated to fit.
2041 .TP
2042 \f[B]hex(x)\f[R]
2043 Outputs the hexadecimal (base \f[B]16\f[R]) representation of
2044 \f[B]x\f[R].
2045 .RS
2046 .PP
2047 This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R]
2048 subsection of the \f[B]FUNCTIONS\f[R] section).
2049 .RE
2050 .TP
2051 \f[B]binary(x)\f[R]
2052 Outputs the binary (base \f[B]2\f[R]) representation of \f[B]x\f[R].
2053 .RS
2054 .PP
2055 This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R]
2056 subsection of the \f[B]FUNCTIONS\f[R] section).
2057 .RE
2058 .TP
2059 \f[B]output(x, b)\f[R]
2060 Outputs the base \f[B]b\f[R] representation of \f[B]x\f[R].
2061 .RS
2062 .PP
2063 This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R]
2064 subsection of the \f[B]FUNCTIONS\f[R] section).
2065 .RE
2066 .TP
2067 \f[B]uint(x)\f[R]
2068 Outputs the representation, in binary and hexadecimal, of \f[B]x\f[R] as
2069 an unsigned integer in as few power of two bytes as possible.
2070 Both outputs are split into bytes separated by spaces.
2071 .RS
2072 .PP
2073 If \f[B]x\f[R] is not an integer or is negative, an error message is
2074 printed instead, but bc(1) is not reset (see the \f[B]RESET\f[R]
2075 section).
2076 .PP
2077 This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R]
2078 subsection of the \f[B]FUNCTIONS\f[R] section).
2079 .RE
2080 .TP
2081 \f[B]int(x)\f[R]
2082 Outputs the representation, in binary and hexadecimal, of \f[B]x\f[R] as
2083 a signed, two\[cq]s-complement integer in as few power of two bytes as
2084 possible.
2085 Both outputs are split into bytes separated by spaces.
2086 .RS
2087 .PP
2088 If \f[B]x\f[R] is not an integer, an error message is printed instead,
2089 but bc(1) is not reset (see the \f[B]RESET\f[R] section).
2090 .PP
2091 This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R]
2092 subsection of the \f[B]FUNCTIONS\f[R] section).
2093 .RE
2094 .TP
2095 \f[B]uintn(x, n)\f[R]
2096 Outputs the representation, in binary and hexadecimal, of \f[B]x\f[R] as
2097 an unsigned integer in \f[B]n\f[R] bytes.
2098 Both outputs are split into bytes separated by spaces.
2099 .RS
2100 .PP
2101 If \f[B]x\f[R] is not an integer, is negative, or cannot fit into
2102 \f[B]n\f[R] bytes, an error message is printed instead, but bc(1) is not
2103 reset (see the \f[B]RESET\f[R] section).
2104 .PP
2105 This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R]
2106 subsection of the \f[B]FUNCTIONS\f[R] section).
2107 .RE
2108 .TP
2109 \f[B]intn(x, n)\f[R]
2110 Outputs the representation, in binary and hexadecimal, of \f[B]x\f[R] as
2111 a signed, two\[cq]s-complement integer in \f[B]n\f[R] bytes.
2112 Both outputs are split into bytes separated by spaces.
2113 .RS
2114 .PP
2115 If \f[B]x\f[R] is not an integer or cannot fit into \f[B]n\f[R] bytes,
2116 an error message is printed instead, but bc(1) is not reset (see the
2117 \f[B]RESET\f[R] section).
2118 .PP
2119 This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R]
2120 subsection of the \f[B]FUNCTIONS\f[R] section).
2121 .RE
2122 .TP
2123 \f[B]uint8(x)\f[R]
2124 Outputs the representation, in binary and hexadecimal, of \f[B]x\f[R] as
2125 an unsigned integer in \f[B]1\f[R] byte.
2126 Both outputs are split into bytes separated by spaces.
2127 .RS
2128 .PP
2129 If \f[B]x\f[R] is not an integer, is negative, or cannot fit into
2130 \f[B]1\f[R] byte, an error message is printed instead, but bc(1) is not
2131 reset (see the \f[B]RESET\f[R] section).
2132 .PP
2133 This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R]
2134 subsection of the \f[B]FUNCTIONS\f[R] section).
2135 .RE
2136 .TP
2137 \f[B]int8(x)\f[R]
2138 Outputs the representation, in binary and hexadecimal, of \f[B]x\f[R] as
2139 a signed, two\[cq]s-complement integer in \f[B]1\f[R] byte.
2140 Both outputs are split into bytes separated by spaces.
2141 .RS
2142 .PP
2143 If \f[B]x\f[R] is not an integer or cannot fit into \f[B]1\f[R] byte, an
2144 error message is printed instead, but bc(1) is not reset (see the
2145 \f[B]RESET\f[R] section).
2146 .PP
2147 This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R]
2148 subsection of the \f[B]FUNCTIONS\f[R] section).
2149 .RE
2150 .TP
2151 \f[B]uint16(x)\f[R]
2152 Outputs the representation, in binary and hexadecimal, of \f[B]x\f[R] as
2153 an unsigned integer in \f[B]2\f[R] bytes.
2154 Both outputs are split into bytes separated by spaces.
2155 .RS
2156 .PP
2157 If \f[B]x\f[R] is not an integer, is negative, or cannot fit into
2158 \f[B]2\f[R] bytes, an error message is printed instead, but bc(1) is not
2159 reset (see the \f[B]RESET\f[R] section).
2160 .PP
2161 This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R]
2162 subsection of the \f[B]FUNCTIONS\f[R] section).
2163 .RE
2164 .TP
2165 \f[B]int16(x)\f[R]
2166 Outputs the representation, in binary and hexadecimal, of \f[B]x\f[R] as
2167 a signed, two\[cq]s-complement integer in \f[B]2\f[R] bytes.
2168 Both outputs are split into bytes separated by spaces.
2169 .RS
2170 .PP
2171 If \f[B]x\f[R] is not an integer or cannot fit into \f[B]2\f[R] bytes,
2172 an error message is printed instead, but bc(1) is not reset (see the
2173 \f[B]RESET\f[R] section).
2174 .PP
2175 This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R]
2176 subsection of the \f[B]FUNCTIONS\f[R] section).
2177 .RE
2178 .TP
2179 \f[B]uint32(x)\f[R]
2180 Outputs the representation, in binary and hexadecimal, of \f[B]x\f[R] as
2181 an unsigned integer in \f[B]4\f[R] bytes.
2182 Both outputs are split into bytes separated by spaces.
2183 .RS
2184 .PP
2185 If \f[B]x\f[R] is not an integer, is negative, or cannot fit into
2186 \f[B]4\f[R] bytes, an error message is printed instead, but bc(1) is not
2187 reset (see the \f[B]RESET\f[R] section).
2188 .PP
2189 This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R]
2190 subsection of the \f[B]FUNCTIONS\f[R] section).
2191 .RE
2192 .TP
2193 \f[B]int32(x)\f[R]
2194 Outputs the representation, in binary and hexadecimal, of \f[B]x\f[R] as
2195 a signed, two\[cq]s-complement integer in \f[B]4\f[R] bytes.
2196 Both outputs are split into bytes separated by spaces.
2197 .RS
2198 .PP
2199 If \f[B]x\f[R] is not an integer or cannot fit into \f[B]4\f[R] bytes,
2200 an error message is printed instead, but bc(1) is not reset (see the
2201 \f[B]RESET\f[R] section).
2202 .PP
2203 This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R]
2204 subsection of the \f[B]FUNCTIONS\f[R] section).
2205 .RE
2206 .TP
2207 \f[B]uint64(x)\f[R]
2208 Outputs the representation, in binary and hexadecimal, of \f[B]x\f[R] as
2209 an unsigned integer in \f[B]8\f[R] bytes.
2210 Both outputs are split into bytes separated by spaces.
2211 .RS
2212 .PP
2213 If \f[B]x\f[R] is not an integer, is negative, or cannot fit into
2214 \f[B]8\f[R] bytes, an error message is printed instead, but bc(1) is not
2215 reset (see the \f[B]RESET\f[R] section).
2216 .PP
2217 This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R]
2218 subsection of the \f[B]FUNCTIONS\f[R] section).
2219 .RE
2220 .TP
2221 \f[B]int64(x)\f[R]
2222 Outputs the representation, in binary and hexadecimal, of \f[B]x\f[R] as
2223 a signed, two\[cq]s-complement integer in \f[B]8\f[R] bytes.
2224 Both outputs are split into bytes separated by spaces.
2225 .RS
2226 .PP
2227 If \f[B]x\f[R] is not an integer or cannot fit into \f[B]8\f[R] bytes,
2228 an error message is printed instead, but bc(1) is not reset (see the
2229 \f[B]RESET\f[R] section).
2230 .PP
2231 This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R]
2232 subsection of the \f[B]FUNCTIONS\f[R] section).
2233 .RE
2234 .TP
2235 \f[B]hex_uint(x, n)\f[R]
2236 Outputs the representation of the truncated absolute value of
2237 \f[B]x\f[R] as an unsigned integer in hexadecimal using \f[B]n\f[R]
2238 bytes.
2239 Not all of the value will be output if \f[B]n\f[R] is too small.
2240 .RS
2241 .PP
2242 This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R]
2243 subsection of the \f[B]FUNCTIONS\f[R] section).
2244 .RE
2245 .TP
2246 \f[B]binary_uint(x, n)\f[R]
2247 Outputs the representation of the truncated absolute value of
2248 \f[B]x\f[R] as an unsigned integer in binary using \f[B]n\f[R] bytes.
2249 Not all of the value will be output if \f[B]n\f[R] is too small.
2250 .RS
2251 .PP
2252 This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R]
2253 subsection of the \f[B]FUNCTIONS\f[R] section).
2254 .RE
2255 .TP
2256 \f[B]output_uint(x, n)\f[R]
2257 Outputs the representation of the truncated absolute value of
2258 \f[B]x\f[R] as an unsigned integer in the current \f[B]obase\f[R] (see
2259 the \f[B]SYNTAX\f[R] section) using \f[B]n\f[R] bytes.
2260 Not all of the value will be output if \f[B]n\f[R] is too small.
2261 .RS
2262 .PP
2263 This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R]
2264 subsection of the \f[B]FUNCTIONS\f[R] section).
2265 .RE
2266 .TP
2267 \f[B]output_byte(x, i)\f[R]
2268 Outputs byte \f[B]i\f[R] of the truncated absolute value of \f[B]x\f[R],
2269 where \f[B]0\f[R] is the least significant byte and \f[B]number_of_bytes
2270 - 1\f[R] is the most significant byte.
2271 .RS
2272 .PP
2273 This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R]
2274 subsection of the \f[B]FUNCTIONS\f[R] section).
2275 .RE
2276 .SS Transcendental Functions
2277 .PP
2278 All transcendental functions can return slightly inaccurate results (up
2279 to 1 ULP (https://en.wikipedia.org/wiki/Unit_in_the_last_place)).
2280 This is unavoidable, and this
2281 article (https://people.eecs.berkeley.edu/~wkahan/LOG10HAF.TXT) explains
2282 why it is impossible and unnecessary to calculate exact results for the
2283 transcendental functions.
2284 .PP
2285 Because of the possible inaccuracy, I recommend that users call those
2286 functions with the precision (\f[B]scale\f[R]) set to at least 1 higher
2287 than is necessary.
2288 If exact results are \f[I]absolutely\f[R] required, users can double the
2289 precision (\f[B]scale\f[R]) and then truncate.
2290 .PP
2291 The transcendental functions in the standard math library are:
2292 .IP \[bu] 2
2293 \f[B]s(x)\f[R]
2294 .IP \[bu] 2
2295 \f[B]c(x)\f[R]
2296 .IP \[bu] 2
2297 \f[B]a(x)\f[R]
2298 .IP \[bu] 2
2299 \f[B]l(x)\f[R]
2300 .IP \[bu] 2
2301 \f[B]e(x)\f[R]
2302 .IP \[bu] 2
2303 \f[B]j(x, n)\f[R]
2304 .PP
2305 The transcendental functions in the extended math library are:
2306 .IP \[bu] 2
2307 \f[B]l2(x)\f[R]
2308 .IP \[bu] 2
2309 \f[B]l10(x)\f[R]
2310 .IP \[bu] 2
2311 \f[B]log(x, b)\f[R]
2312 .IP \[bu] 2
2313 \f[B]pi(p)\f[R]
2314 .IP \[bu] 2
2315 \f[B]t(x)\f[R]
2316 .IP \[bu] 2
2317 \f[B]a2(y, x)\f[R]
2318 .IP \[bu] 2
2319 \f[B]sin(x)\f[R]
2320 .IP \[bu] 2
2321 \f[B]cos(x)\f[R]
2322 .IP \[bu] 2
2323 \f[B]tan(x)\f[R]
2324 .IP \[bu] 2
2325 \f[B]atan(x)\f[R]
2326 .IP \[bu] 2
2327 \f[B]atan2(y, x)\f[R]
2328 .IP \[bu] 2
2329 \f[B]r2d(x)\f[R]
2330 .IP \[bu] 2
2331 \f[B]d2r(x)\f[R]
2332 .SH RESET
2333 .PP
2334 When bc(1) encounters an error or a signal that it has a non-default
2335 handler for, it resets.
2336 This means that several things happen.
2337 .PP
2338 First, any functions that are executing are stopped and popped off the
2339 stack.
2340 The behavior is not unlike that of exceptions in programming languages.
2341 Then the execution point is set so that any code waiting to execute
2342 (after all functions returned) is skipped.
2343 .PP
2344 Thus, when bc(1) resets, it skips any remaining code waiting to be
2345 executed.
2346 Then, if it is interactive mode, and the error was not a fatal error
2347 (see the \f[B]EXIT STATUS\f[R] section), it asks for more input;
2348 otherwise, it exits with the appropriate return code.
2349 .PP
2350 Note that this reset behavior is different from the GNU bc(1), which
2351 attempts to start executing the statement right after the one that
2352 caused an error.
2353 .SH PERFORMANCE
2354 .PP
2355 Most bc(1) implementations use \f[B]char\f[R] types to calculate the
2356 value of \f[B]1\f[R] decimal digit at a time, but that can be slow.
2357 This bc(1) does something different.
2358 .PP
2359 It uses large integers to calculate more than \f[B]1\f[R] decimal digit
2360 at a time.
2361 If built in a environment where \f[B]BC_LONG_BIT\f[R] (see the
2362 \f[B]LIMITS\f[R] section) is \f[B]64\f[R], then each integer has
2363 \f[B]9\f[R] decimal digits.
2364 If built in an environment where \f[B]BC_LONG_BIT\f[R] is \f[B]32\f[R]
2365 then each integer has \f[B]4\f[R] decimal digits.
2366 This value (the number of decimal digits per large integer) is called
2367 \f[B]BC_BASE_DIGS\f[R].
2368 .PP
2369 The actual values of \f[B]BC_LONG_BIT\f[R] and \f[B]BC_BASE_DIGS\f[R]
2370 can be queried with the \f[B]limits\f[R] statement.
2371 .PP
2372 In addition, this bc(1) uses an even larger integer for overflow
2373 checking.
2374 This integer type depends on the value of \f[B]BC_LONG_BIT\f[R], but is
2375 always at least twice as large as the integer type used to store digits.
2376 .SH LIMITS
2377 .PP
2378 The following are the limits on bc(1):
2379 .TP
2380 \f[B]BC_LONG_BIT\f[R]
2381 The number of bits in the \f[B]long\f[R] type in the environment where
2382 bc(1) was built.
2383 This determines how many decimal digits can be stored in a single large
2384 integer (see the \f[B]PERFORMANCE\f[R] section).
2385 .TP
2386 \f[B]BC_BASE_DIGS\f[R]
2387 The number of decimal digits per large integer (see the
2388 \f[B]PERFORMANCE\f[R] section).
2389 Depends on \f[B]BC_LONG_BIT\f[R].
2390 .TP
2391 \f[B]BC_BASE_POW\f[R]
2392 The max decimal number that each large integer can store (see
2393 \f[B]BC_BASE_DIGS\f[R]) plus \f[B]1\f[R].
2394 Depends on \f[B]BC_BASE_DIGS\f[R].
2395 .TP
2396 \f[B]BC_OVERFLOW_MAX\f[R]
2397 The max number that the overflow type (see the \f[B]PERFORMANCE\f[R]
2398 section) can hold.
2399 Depends on \f[B]BC_LONG_BIT\f[R].
2400 .TP
2401 \f[B]BC_BASE_MAX\f[R]
2402 The maximum output base.
2403 Set at \f[B]BC_BASE_POW\f[R].
2404 .TP
2405 \f[B]BC_DIM_MAX\f[R]
2406 The maximum size of arrays.
2407 Set at \f[B]SIZE_MAX-1\f[R].
2408 .TP
2409 \f[B]BC_SCALE_MAX\f[R]
2410 The maximum \f[B]scale\f[R].
2411 Set at \f[B]BC_OVERFLOW_MAX-1\f[R].
2412 .TP
2413 \f[B]BC_STRING_MAX\f[R]
2414 The maximum length of strings.
2415 Set at \f[B]BC_OVERFLOW_MAX-1\f[R].
2416 .TP
2417 \f[B]BC_NAME_MAX\f[R]
2418 The maximum length of identifiers.
2419 Set at \f[B]BC_OVERFLOW_MAX-1\f[R].
2420 .TP
2421 \f[B]BC_NUM_MAX\f[R]
2422 The maximum length of a number (in decimal digits), which includes
2423 digits after the decimal point.
2424 Set at \f[B]BC_OVERFLOW_MAX-1\f[R].
2425 .TP
2426 \f[B]BC_RAND_MAX\f[R]
2427 The maximum integer (inclusive) returned by the \f[B]rand()\f[R]
2428 operand.
2429 Set at \f[B]2\[ha]BC_LONG_BIT-1\f[R].
2430 .TP
2431 Exponent
2432 The maximum allowable exponent (positive or negative).
2433 Set at \f[B]BC_OVERFLOW_MAX\f[R].
2434 .TP
2435 Number of vars
2436 The maximum number of vars/arrays.
2437 Set at \f[B]SIZE_MAX-1\f[R].
2438 .PP
2439 The actual values can be queried with the \f[B]limits\f[R] statement.
2440 .PP
2441 These limits are meant to be effectively non-existent; the limits are so
2442 large (at least on 64-bit machines) that there should not be any point
2443 at which they become a problem.
2444 In fact, memory should be exhausted before these limits should be hit.
2445 .SH ENVIRONMENT VARIABLES
2446 .PP
2447 bc(1) recognizes the following environment variables:
2448 .TP
2449 \f[B]POSIXLY_CORRECT\f[R]
2450 If this variable exists (no matter the contents), bc(1) behaves as if
2451 the \f[B]-s\f[R] option was given.
2452 .TP
2453 \f[B]BC_ENV_ARGS\f[R]
2454 This is another way to give command-line arguments to bc(1).
2455 They should be in the same format as all other command-line arguments.
2456 These are always processed first, so any files given in
2457 \f[B]BC_ENV_ARGS\f[R] will be processed before arguments and files given
2458 on the command-line.
2459 This gives the user the ability to set up \[lq]standard\[rq] options and
2460 files to be used at every invocation.
2461 The most useful thing for such files to contain would be useful
2462 functions that the user might want every time bc(1) runs.
2463 .RS
2464 .PP
2465 The code that parses \f[B]BC_ENV_ARGS\f[R] will correctly handle quoted
2466 arguments, but it does not understand escape sequences.
2467 For example, the string \f[B]\[lq]/home/gavin/some bc file.bc\[rq]\f[R]
2468 will be correctly parsed, but the string \f[B]\[lq]/home/gavin/some
2469 \[dq]bc\[dq] file.bc\[rq]\f[R] will include the backslashes.
2470 .PP
2471 The quote parsing will handle either kind of quotes, \f[B]\[cq]\f[R] or
2472 \f[B]\[lq]\f[R].
2473 Thus, if you have a file with any number of single quotes in the name,
2474 you can use double quotes as the outside quotes, as in \f[B]\[lq]some
2475 `bc' file.bc\[rq]\f[R], and vice versa if you have a file with double
2476 quotes.
2477 However, handling a file with both kinds of quotes in
2478 \f[B]BC_ENV_ARGS\f[R] is not supported due to the complexity of the
2479 parsing, though such files are still supported on the command-line where
2480 the parsing is done by the shell.
2481 .RE
2482 .TP
2483 \f[B]BC_LINE_LENGTH\f[R]
2484 If this environment variable exists and contains an integer that is
2485 greater than \f[B]1\f[R] and is less than \f[B]UINT16_MAX\f[R]
2486 (\f[B]2\[ha]16-1\f[R]), bc(1) will output lines to that length,
2487 including the backslash (\f[B]\[rs]\f[R]).
2488 The default line length is \f[B]70\f[R].
2489 .RS
2490 .PP
2491 The special value of \f[B]0\f[R] will disable line length checking and
2492 print numbers without regard to line length and without backslashes and
2493 newlines.
2494 .RE
2495 .TP
2496 \f[B]BC_BANNER\f[R]
2497 If this environment variable exists and contains an integer, then a
2498 non-zero value activates the copyright banner when bc(1) is in
2499 interactive mode, while zero deactivates it.
2500 .RS
2501 .PP
2502 If bc(1) is not in interactive mode (see the \f[B]INTERACTIVE MODE\f[R]
2503 section), then this environment variable has no effect because bc(1)
2504 does not print the banner when not in interactive mode.
2505 .PP
2506 This environment variable overrides the default, which can be queried
2507 with the \f[B]-h\f[R] or \f[B]--help\f[R] options.
2508 .RE
2509 .TP
2510 \f[B]BC_SIGINT_RESET\f[R]
2511 If bc(1) is not in interactive mode (see the \f[B]INTERACTIVE MODE\f[R]
2512 section), then this environment variable has no effect because bc(1)
2513 exits on \f[B]SIGINT\f[R] when not in interactive mode.
2514 .RS
2515 .PP
2516 However, when bc(1) is in interactive mode, then if this environment
2517 variable exists and contains an integer, a non-zero value makes bc(1)
2518 reset on \f[B]SIGINT\f[R], rather than exit, and zero makes bc(1) exit.
2519 If this environment variable exists and is \f[I]not\f[R] an integer,
2520 then bc(1) will exit on \f[B]SIGINT\f[R].
2521 .PP
2522 This environment variable overrides the default, which can be queried
2523 with the \f[B]-h\f[R] or \f[B]--help\f[R] options.
2524 .RE
2525 .TP
2526 \f[B]BC_TTY_MODE\f[R]
2527 If TTY mode is \f[I]not\f[R] available (see the \f[B]TTY MODE\f[R]
2528 section), then this environment variable has no effect.
2529 .RS
2530 .PP
2531 However, when TTY mode is available, then if this environment variable
2532 exists and contains an integer, then a non-zero value makes bc(1) use
2533 TTY mode, and zero makes bc(1) not use TTY mode.
2534 .PP
2535 This environment variable overrides the default, which can be queried
2536 with the \f[B]-h\f[R] or \f[B]--help\f[R] options.
2537 .RE
2538 .TP
2539 \f[B]BC_PROMPT\f[R]
2540 If TTY mode is \f[I]not\f[R] available (see the \f[B]TTY MODE\f[R]
2541 section), then this environment variable has no effect.
2542 .RS
2543 .PP
2544 However, when TTY mode is available, then if this environment variable
2545 exists and contains an integer, a non-zero value makes bc(1) use a
2546 prompt, and zero or a non-integer makes bc(1) not use a prompt.
2547 If this environment variable does not exist and \f[B]BC_TTY_MODE\f[R]
2548 does, then the value of the \f[B]BC_TTY_MODE\f[R] environment variable
2549 is used.
2550 .PP
2551 This environment variable and the \f[B]BC_TTY_MODE\f[R] environment
2552 variable override the default, which can be queried with the
2553 \f[B]-h\f[R] or \f[B]--help\f[R] options.
2554 .RE
2555 .SH EXIT STATUS
2556 .PP
2557 bc(1) returns the following exit statuses:
2558 .TP
2559 \f[B]0\f[R]
2560 No error.
2561 .TP
2562 \f[B]1\f[R]
2563 A math error occurred.
2564 This follows standard practice of using \f[B]1\f[R] for expected errors,
2565 since math errors will happen in the process of normal execution.
2566 .RS
2567 .PP
2568 Math errors include divide by \f[B]0\f[R], taking the square root of a
2569 negative number, using a negative number as a bound for the
2570 pseudo-random number generator, attempting to convert a negative number
2571 to a hardware integer, overflow when converting a number to a hardware
2572 integer, overflow when calculating the size of a number, and attempting
2573 to use a non-integer where an integer is required.
2574 .PP
2575 Converting to a hardware integer happens for the second operand of the
2576 power (\f[B]\[ha]\f[R]), places (\f[B]\[at]\f[R]), left shift
2577 (\f[B]<<\f[R]), and right shift (\f[B]>>\f[R]) operators and their
2578 corresponding assignment operators.
2579 .RE
2580 .TP
2581 \f[B]2\f[R]
2582 A parse error occurred.
2583 .RS
2584 .PP
2585 Parse errors include unexpected \f[B]EOF\f[R], using an invalid
2586 character, failing to find the end of a string or comment, using a token
2587 where it is invalid, giving an invalid expression, giving an invalid
2588 print statement, giving an invalid function definition, attempting to
2589 assign to an expression that is not a named expression (see the
2590 \f[I]Named Expressions\f[R] subsection of the \f[B]SYNTAX\f[R] section),
2591 giving an invalid \f[B]auto\f[R] list, having a duplicate
2592 \f[B]auto\f[R]/function parameter, failing to find the end of a code
2593 block, attempting to return a value from a \f[B]void\f[R] function,
2594 attempting to use a variable as a reference, and using any extensions
2595 when the option \f[B]-s\f[R] or any equivalents were given.
2596 .RE
2597 .TP
2598 \f[B]3\f[R]
2599 A runtime error occurred.
2600 .RS
2601 .PP
2602 Runtime errors include assigning an invalid number to any global
2603 (\f[B]ibase\f[R], \f[B]obase\f[R], or \f[B]scale\f[R]), giving a bad
2604 expression to a \f[B]read()\f[R] call, calling \f[B]read()\f[R] inside
2605 of a \f[B]read()\f[R] call, type errors, passing the wrong number of
2606 arguments to functions, attempting to call an undefined function, and
2607 attempting to use a \f[B]void\f[R] function call as a value in an
2608 expression.
2609 .RE
2610 .TP
2611 \f[B]4\f[R]
2612 A fatal error occurred.
2613 .RS
2614 .PP
2615 Fatal errors include memory allocation errors, I/O errors, failing to
2616 open files, attempting to use files that do not have only ASCII
2617 characters (bc(1) only accepts ASCII characters), attempting to open a
2618 directory as a file, and giving invalid command-line options.
2619 .RE
2620 .PP
2621 The exit status \f[B]4\f[R] is special; when a fatal error occurs, bc(1)
2622 always exits and returns \f[B]4\f[R], no matter what mode bc(1) is in.
2623 .PP
2624 The other statuses will only be returned when bc(1) is not in
2625 interactive mode (see the \f[B]INTERACTIVE MODE\f[R] section), since
2626 bc(1) resets its state (see the \f[B]RESET\f[R] section) and accepts
2627 more input when one of those errors occurs in interactive mode.
2628 This is also the case when interactive mode is forced by the
2629 \f[B]-i\f[R] flag or \f[B]--interactive\f[R] option.
2630 .PP
2631 These exit statuses allow bc(1) to be used in shell scripting with error
2632 checking, and its normal behavior can be forced by using the
2633 \f[B]-i\f[R] flag or \f[B]--interactive\f[R] option.
2634 .SH INTERACTIVE MODE
2635 .PP
2636 Per the
2637 standard (https://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html),
2638 bc(1) has an interactive mode and a non-interactive mode.
2639 Interactive mode is turned on automatically when both \f[B]stdin\f[R]
2640 and \f[B]stdout\f[R] are hooked to a terminal, but the \f[B]-i\f[R] flag
2641 and \f[B]--interactive\f[R] option can turn it on in other situations.
2642 .PP
2643 In interactive mode, bc(1) attempts to recover from errors (see the
2644 \f[B]RESET\f[R] section), and in normal execution, flushes
2645 \f[B]stdout\f[R] as soon as execution is done for the current input.
2646 bc(1) may also reset on \f[B]SIGINT\f[R] instead of exit, depending on
2647 the contents of, or default for, the \f[B]BC_SIGINT_RESET\f[R]
2648 environment variable (see the \f[B]ENVIRONMENT VARIABLES\f[R] section).
2649 .SH TTY MODE
2650 .PP
2651 If \f[B]stdin\f[R], \f[B]stdout\f[R], and \f[B]stderr\f[R] are all
2652 connected to a TTY, then \[lq]TTY mode\[rq] is considered to be
2653 available, and thus, bc(1) can turn on TTY mode, subject to some
2654 settings.
2655 .PP
2656 If there is the environment variable \f[B]BC_TTY_MODE\f[R] in the
2657 environment (see the \f[B]ENVIRONMENT VARIABLES\f[R] section), then if
2658 that environment variable contains a non-zero integer, bc(1) will turn
2659 on TTY mode when \f[B]stdin\f[R], \f[B]stdout\f[R], and \f[B]stderr\f[R]
2660 are all connected to a TTY.
2661 If the \f[B]BC_TTY_MODE\f[R] environment variable exists but is
2662 \f[I]not\f[R] a non-zero integer, then bc(1) will not turn TTY mode on.
2663 .PP
2664 If the environment variable \f[B]BC_TTY_MODE\f[R] does \f[I]not\f[R]
2665 exist, the default setting is used.
2666 The default setting can be queried with the \f[B]-h\f[R] or
2667 \f[B]--help\f[R] options.
2668 .PP
2669 TTY mode is different from interactive mode because interactive mode is
2670 required in the bc(1)
2671 specification (https://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html),
2672 and interactive mode requires only \f[B]stdin\f[R] and \f[B]stdout\f[R]
2673 to be connected to a terminal.
2674 .SS Command-Line History
2675 .PP
2676 Command-line history is only enabled if TTY mode is, i.e., that
2677 \f[B]stdin\f[R], \f[B]stdout\f[R], and \f[B]stderr\f[R] are connected to
2678 a TTY and the \f[B]BC_TTY_MODE\f[R] environment variable (see the
2679 \f[B]ENVIRONMENT VARIABLES\f[R] section) and its default do not disable
2680 TTY mode.
2681 See the \f[B]COMMAND LINE HISTORY\f[R] section for more information.
2682 .SS Prompt
2683 .PP
2684 If TTY mode is available, then a prompt can be enabled.
2685 Like TTY mode itself, it can be turned on or off with an environment
2686 variable: \f[B]BC_PROMPT\f[R] (see the \f[B]ENVIRONMENT VARIABLES\f[R]
2687 section).
2688 .PP
2689 If the environment variable \f[B]BC_PROMPT\f[R] exists and is a non-zero
2690 integer, then the prompt is turned on when \f[B]stdin\f[R],
2691 \f[B]stdout\f[R], and \f[B]stderr\f[R] are connected to a TTY and the
2692 \f[B]-P\f[R] and \f[B]--no-prompt\f[R] options were not used.
2693 The read prompt will be turned on under the same conditions, except that
2694 the \f[B]-R\f[R] and \f[B]--no-read-prompt\f[R] options must also not be
2695 used.
2696 .PP
2697 However, if \f[B]BC_PROMPT\f[R] does not exist, the prompt can be
2698 enabled or disabled with the \f[B]BC_TTY_MODE\f[R] environment variable,
2699 the \f[B]-P\f[R] and \f[B]--no-prompt\f[R] options, and the \f[B]-R\f[R]
2700 and \f[B]--no-read-prompt\f[R] options.
2701 See the \f[B]ENVIRONMENT VARIABLES\f[R] and \f[B]OPTIONS\f[R] sections
2702 for more details.
2703 .SH SIGNAL HANDLING
2704 .PP
2705 Sending a \f[B]SIGINT\f[R] will cause bc(1) to do one of two things.
2706 .PP
2707 If bc(1) is not in interactive mode (see the \f[B]INTERACTIVE MODE\f[R]
2708 section), or the \f[B]BC_SIGINT_RESET\f[R] environment variable (see the
2709 \f[B]ENVIRONMENT VARIABLES\f[R] section), or its default, is either not
2710 an integer or it is zero, bc(1) will exit.
2711 .PP
2712 However, if bc(1) is in interactive mode, and the
2713 \f[B]BC_SIGINT_RESET\f[R] or its default is an integer and non-zero,
2714 then bc(1) will stop executing the current input and reset (see the
2715 \f[B]RESET\f[R] section) upon receiving a \f[B]SIGINT\f[R].
2716 .PP
2717 Note that \[lq]current input\[rq] can mean one of two things.
2718 If bc(1) is processing input from \f[B]stdin\f[R] in interactive mode,
2719 it will ask for more input.
2720 If bc(1) is processing input from a file in interactive mode, it will
2721 stop processing the file and start processing the next file, if one
2722 exists, or ask for input from \f[B]stdin\f[R] if no other file exists.
2723 .PP
2724 This means that if a \f[B]SIGINT\f[R] is sent to bc(1) as it is
2725 executing a file, it can seem as though bc(1) did not respond to the
2726 signal since it will immediately start executing the next file.
2727 This is by design; most files that users execute when interacting with
2728 bc(1) have function definitions, which are quick to parse.
2729 If a file takes a long time to execute, there may be a bug in that file.
2730 The rest of the files could still be executed without problem, allowing
2731 the user to continue.
2732 .PP
2733 \f[B]SIGTERM\f[R] and \f[B]SIGQUIT\f[R] cause bc(1) to clean up and
2734 exit, and it uses the default handler for all other signals.
2735 The one exception is \f[B]SIGHUP\f[R]; in that case, and only when bc(1)
2736 is in TTY mode (see the \f[B]TTY MODE\f[R] section), a \f[B]SIGHUP\f[R]
2737 will cause bc(1) to clean up and exit.
2738 .SH COMMAND LINE HISTORY
2739 .PP
2740 bc(1) supports interactive command-line editing.
2741 .PP
2742 If bc(1) can be in TTY mode (see the \f[B]TTY MODE\f[R] section),
2743 history can be enabled.
2744 This means that command-line history can only be enabled when
2745 \f[B]stdin\f[R], \f[B]stdout\f[R], and \f[B]stderr\f[R] are all
2746 connected to a TTY.
2747 .PP
2748 Like TTY mode itself, it can be turned on or off with the environment
2749 variable \f[B]BC_TTY_MODE\f[R] (see the \f[B]ENVIRONMENT VARIABLES\f[R]
2750 section).
2751 .PP
2752 If history is enabled, previous lines can be recalled and edited with
2753 the arrow keys.
2754 .PP
2755 \f[B]Note\f[R]: tabs are converted to 8 spaces.
2756 .SH SEE ALSO
2757 .PP
2758 dc(1)
2759 .SH STANDARDS
2760 .PP
2761 bc(1) is compliant with the IEEE Std 1003.1-2017
2762 (\[lq]POSIX.1-2017\[rq]) (https://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html)
2763 specification.
2764 The flags \f[B]-efghiqsvVw\f[R], all long options, and the extensions
2765 noted above are extensions to that specification.
2766 .PP
2767 Note that the specification explicitly says that bc(1) only accepts
2768 numbers that use a period (\f[B].\f[R]) as a radix point, regardless of
2769 the value of \f[B]LC_NUMERIC\f[R].
2770 .SH BUGS
2771 .PP
2772 None are known.
2773 Report bugs at https://git.yzena.com/gavin/bc.
2774 .SH AUTHORS
2775 .PP
2776 Gavin D.
2777 Howard <gavin@yzena.com> and contributors.