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