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