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