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